struts2 interceptor实列

介绍:
首先进入登录界面,输入用户密码进入action信息不匹配进入错误处理界面,信息匹配将用户密码封装成user对象,该action方法不被拦截(已设置),将对象存入值栈,在index界面取出,并再次访问另一个action,该action将被拦截,拦截器检查user是否存在,存在则放行,进入对应界面,错误则进入全局错误处理界面
登录界面:
<html>
<head>
    <title>登录界面</title>
</head>
<body>
    <form action="loginactiontest1" method="post">
        <label>用户名</label><input type="text" name="username"><br>
        <label>密码</label><input type="password" name="password"><br>
        <input type="submit" value="login1">
    </form>
</body>
</html>

不被拦截的action

public class LoginAction extends ActionSupport implements ModelDriven<User>{
    
    
	private User user = new User();
	public String login() throws Exception {
    
    
		// 1. 假定在数据库中查询出的用户名密码如下
		String db_username = "admin";
		String db_password = "admin";
		String result = SUCCESS;
		if(db_username.equals(user.getUsername()) && db_password.equals(user.getPassword())){
    
    
			Map<String, Object> session = ActionContext.getContext().getSession();
			session.put("user", user);
		} else {
    
    
			result = ERROR;
		}
		return result;
	}

	public User getModel() {
    
    
		return user;
	}
	
}

登录成功界面

<html>
<head>
    <title>欢迎界面</title>
</head>
<body>
        欢迎您! ${user.username}
        <a href="MenuAction.action">菜单管理</a>
</body>
</html>

登录失败界面

<html>
<head>
    <title>错误界面</title>
</head>
<body>
<h1>您输入的信息有误</h1>
</body>
</html>

登录成功跳转到即将被拦截的action

public class ShowAction extends ActionSupport {
    
    
	public String show() {
    
    
		System.out.print("show");
		return SUCCESS;
	}	
}

拦截器代码


public class IntercClass  extends MethodFilterInterceptor{
    
    

	@Override
	protected String doIntercept(ActionInvocation invocation) throws Exception {
    
    
		 /*
		    拦截器放行 如果拦截器链没有执行完,则进入下一个拦截器,如果当前
		    拦截器是链上的最后一个拦截器,则进入action
		 */
		String result1 = "to_Login";
		System.out.println("myintercepter invoke");
		Map<String, Object> session = ActionContext.getContext().getSession();
		if(session.containsKey("user") ){
    
    //再访问showaction时刻,如果存在user对象,并且user不为空
			if(session.get("user") != null) {
    
    
				return invocation.invoke();//放行
			}
		}
		return result1;
	}
}

struts.xml配置拦截器

<package name="helloone" namespace="/interceptor" extends="struts-default">
    <interceptors >
        <!-- 注册拦截器-->
        <interceptor name="stu111" class="Intercepter.IntercClass"></interceptor>
        <!-- 注册拦截器栈 -->
        <interceptor-stack name="stack11">
            <interceptor-ref name="stu111">
                <!-- 不拦截login方法 -->
                <param name="excludeMethods">login</param>
            </interceptor-ref>
            <!-- 3.调用全部默认拦截器 -->
            <interceptor-ref name="defaultStack"></interceptor-ref><!-- 先入后出,先经过默认的拦截器 -->
        </interceptor-stack>
    </interceptors>
    <default-interceptor-ref name="stack11"></default-interceptor-ref>
    <!-- 全局结果 -->
    <!-- 不放行 -->
    <global-results>
        <result name="to_Login">/interceptor/login.jsp</result>
    </global-results>
    <action name="loginactiontest1" class="Intercepter.LoginAction" method="login">
        <result name="success" >/interceptor/index.jsp</result>
        <result name="error" >/interceptor/error.jsp</result>
    </action>
    <action name="MenuAction" class="Intercepter.ShowAction" method="show"><!-- {1}第一个*引用通配符代表_的* -->
        <result name="success" type="redirect">/interceptor/index.jsp</result>
    </action>
    </package>

猜你喜欢

转载自blog.csdn.net/weixin_44703894/article/details/111565286