struts2自定义拦截器
程序员文章站
2022-05-28 17:06:40
...
package com.interceptot;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class LoginInterceptor extends AbstractInterceptor {
@Override
public String intercept(ActionInvocation invocation) throws Exception {
// 取得请求相关的ActionContext实例
String user=(String)ActionContext.getContext.getSession().get("user");
// 如果没有登陆,都返回重新登陆
if (user != null && user.equals("")) {
return invocation.invoke();
}
ActionContext.put("message", "你还没有登录");
return LOGIN;
}
}
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <package name="authority" extends="struts-default"> <!-- 定义一个拦截器 --> <interceptors> <interceptor name="authority" class="com.LoginInterceptor"> </interceptor> <!-- 拦截器栈 --> <interceptor-stack name="mydefault"> <interceptor-ref name="defaultStack" /> <interceptor-ref name="authority" /> </interceptor-stack> </interceptors> <defalut-interceptor-ref name="mydefault" /> <!-- 定义全局Result --> <global-results> <!-- 当返回login视图名时,转入/login.jsp页面 --> <result name="login">/login.jsp</result> </global-results> <action name="loginform" class="com.LoginFormAction"> <result name="success">/login.jsp</result> </action> <action name="login" class="com.LoginAction"> <result name="success">/welcome.jsp</result> <result name="error">/login.jsp</result> <result name="input">/login.jsp</result> </action> <action name="show" class="com.ShowAction"> <result name="success">/show.jsp</result> <!-- 使用此拦截器 --> <interceptor-ref name="mydefault" /> </action> </package> </struts>
下一篇: pymongo 使用