zoukankan      html  css  js  c++  java
  • Struts2学习之拦截器栈

    © 版权声明:本文为博主原创文章,转载请注明出处

    拦截器栈:

      - 从结构上看:拦截器栈相当于多个拦截器的组合

      - 从功能上看:拦截器栈也是拦截器

    默认拦截器栈:

      - 在struts-core.jar中的struts-default.xml中自定义了一个default拦截器栈,并且将其指定为默认拦截器栈

      - 只要定义包的过程中继承struts-default包,那么默认defaultStack将是默认的拦截器

      - 当为包中的某个action显示指定了某个拦截器,那么默认的拦截器将不会生效

      - 拦截器栈中的各个拦截器的顺序很重要(一般讲默认拦截器器栈放在前面)

    实例:

    1.项目结构

    2.pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
      
      	<modelVersion>4.0.0</modelVersion>
      	
    	<groupId>org.struts</groupId>
    	<artifactId>Struts2-AuthInterceptor</artifactId>
    	<packaging>war</packaging>
    	<version>0.0.1-SNAPSHOT</version>
    	
    	<properties>
    		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    		<struts.version>2.5.10</struts.version>
    	</properties>
    	
    	<dependencies>
    		<!-- Junit -->
    		<dependency>
    			<groupId>junit</groupId>
    			<artifactId>junit</artifactId>
    			<version>4.12</version>
    			<scope>test</scope>
    		</dependency>
    		<!-- Struts2 -->
    		<dependency>
    		    <groupId>org.apache.struts</groupId>
    		    <artifactId>struts2-core</artifactId>
    		    <version>${struts.version}</version>
    		</dependency>
    	</dependencies>
    	
    	<build>
    		<finalName>Struts2-AuthInterceptor</finalName>
    	</build>
      
    </project>

    3.web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://java.sun.com/xml/ns/javaee"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                          http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
      version="3.0" metadata-complete="true">
    
    	<!-- 首页 -->
    	<welcome-file-list>
    		<welcome-file>login.jsp</welcome-file>
    	</welcome-file-list>
    	
    	<!-- Struts2过滤器 -->
    	<filter>
    		<filter-name>struts2</filter-name>
    		<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
    	</filter>
    	<filter-mapping>
    		<filter-name>struts2</filter-name>
    		<url-pattern>/*</url-pattern>
    	</filter-mapping>
    
    </web-app>

    4.login.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>用户登录</title>
    </head>
    <body>
    	<form action="login" method="post">
    		<font color="red">${loginError }</font><br/>
    		用户名:<input type="text" name="username"/><br/>
    		密   码:<input type="password" name="password"/><br/>
    		<input type="submit" value="登录"/>
    		<input type="reset" value="重置"/>
    	</form>
    </body>
    </html>

    5.manager.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>管理界面</title>
    </head>
    <body>
    	欢迎进入管理页面,该页面只有登录用户才能访问!
    </body>
    </html>

    6.LoginAction.java

    package org.struts.action;
    
    import java.util.Map;
    
    import org.apache.struts2.interceptor.SessionAware;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    public class LoginAction extends ActionSupport implements SessionAware {
    
    	private static final long serialVersionUID = 1L;
    	
    	private String username;// 用户名
    	private String password;// 密码
    	private Map<String, Object> session;// Session
    	
    	
    	@Override
    	public String execute() throws Exception {
    		
    		if ("admin".equals(username) && "123456".equals(password)) {
    			session.put("userInfo", username);
    			session.remove("loginError");
    			return SUCCESS;
    		} else {
    			session.put("loginError", "用户名或密码错误");
    			session.remove("userInfo");
    			return "login";
    		}
    		
    	}
    
    	public void setSession(Map<String, Object> session) {
    		
    		this.session = session;
    		
    	}
    
    	public String getUsername() {
    		return username;
    	}
    
    	public void setUsername(String username) {
    		this.username = username;
    	}
    
    	public String getPassword() {
    		return password;
    	}
    
    	public void setPassword(String password) {
    		this.password = password;
    	}
    
    }

    7.AuthInterceptor.java

    package org.struts.interceptor;
    
    import java.util.Map;
    
    import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ActionInvocation;
    import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
    
    public class AuthInterceptor extends AbstractInterceptor {
    
    	private static final long serialVersionUID = 1L;
    
    	@Override
    	public String intercept(ActionInvocation invocation) throws Exception {
    		
    		// 获取session
    		ActionContext context = ActionContext.getContext();
    		Map<String, Object> session = context.getSession();
    		if (session.get("userInfo") != null) { // 登录成功
    			String result = invocation.invoke();
    			return result;
    		} else {// 未登录
    			session.put("loginError", "请先登录");
    			return "login";
    		}
    		
    	}
    
    }

    8.struts.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">
    <struts>
    
    	<package name="default" extends="struts-default" namespace="/">
    	
    		<!-- 注册拦截器 -->
    		<interceptors>
    			<interceptor name="auth" class="org.struts.interceptor.AuthInterceptor"/>
    			<interceptor-stack name="authStack">
    				<interceptor-ref name="defaultStack"/>
    				<interceptor-ref name="auth"/>
    			</interceptor-stack>
    		</interceptors>
    	
    		<!-- 通过此Action访问后台管理页面 -->
    		<action name="auth">
    			<result>/WEB-INF/view/manager.jsp</result>
    			<result name="login">/login.jsp</result>
    			<interceptor-ref name="authStack"/>
    		</action>
    	
    		<action name="login" class="org.struts.action.LoginAction">
    			<result>/WEB-INF/view/manager.jsp</result>
    			<result name="login">/login.jsp</result>
    		</action>
    	</package>
    
    </struts>

    9.结果预览

      9.1 未登录直接访问

      9.2 登录成功

      9.3 登录成功后访问

    参考:http://www.imooc.com/video/9102

  • 相关阅读:
    Android
    nodejs 中 接受前端的数据请求的处理
    nodejs 文件操作
    nodejs 简单的搭建一个服务器
    angular 的跨域处理
    angular 的配置文件的应用
    angular 语法的应用
    淘宝的公共样式
    web编辑器的快捷键
    scss 用法 及 es6 用法讲解
  • 原文地址:https://www.cnblogs.com/jinjiyese153/p/6924457.html
Copyright © 2011-2022 走看看