[转] Struts2 使用 Interceptor 拦截 jsp 页面
From:http://www.iteye.com/topic/264249
由于Struts2 的是基于 Filter 的拦截机制实现的。所以,使用 Struts2 的 Interceptor 解析请求进行处理即可实现拦截 jsp 页面需求
代码转载:
1 import javax.servlet.http.HttpServletRequest; 2 3 import org.apache.struts2.ServletActionContext; 4 5 import com.javasystem.JavaSystemConstants; 6 import com.javasystem.action.TuserCRUDAction; 7 import com.opensymphony.xwork2.ActionContext; 8 import com.opensymphony.xwork2.ActionInvocation; 9 import com.opensymphony.xwork2.interceptor.AbstractInterceptor; 10 11 public class LoginInterceptor extends AbstractInterceptor { 12 13 private static final long serialVersionUID = 8760120133840824493L; 14 public static final String LOGIN_KEY = "LOGIN"; 15 // find <result name="global.login" 16 // type="redirect">sysLogout.action</result> is the forward action . 17 public static final String LOGIN_PAGE = "global.login"; 18 19 @Override 20 public String intercept(ActionInvocation actionInvocation) throws Exception { 21 System.out.println("begin check login interceptor!"); 22 // don't check user login action itself . 23 Object action = actionInvocation.getAction(); 24 if (action instanceof TuserCRUDAction) { 25 System.out 26 .println("exit check login, because this is login action."); 27 return actionInvocation.invoke(); 28 } 29 // check user login session . 30 ActionContext ctx = ActionContext.getContext(); 31 HttpServletRequest request = (HttpServletRequest) ctx 32 .get(ServletActionContext.HTTP_REQUEST); 33 Object userSession = request.getSession().getAttribute( 34 JavaSystemConstants.USER_LOGIN_SESSION_NAME); 35 // get session use session name . 36 if (userSession != null) { 37 // 38 System.out.println("already login!"); 39 return actionInvocation.invoke(); 40 } else { 41 // 42 System.out.println("no login, forward login page!"); 43 return LOGIN_PAGE; 44 45 } 46 47 } 48 49 }
这个地方可以获得的到request了就可以得到一个url。。。
1 <package name="javasys" extends="struts-default"> 2 <interceptors> 3 <interceptor-stack name="authenticationStack"> 4 <interceptor-ref name="fileUpload"> 5 <param name="maximumSize">1024000000</param> 6 </interceptor-ref> 7 <interceptor-ref name="basicStack" /> 8 </interceptor-stack> 9 <interceptor name="login" 10 class="com.javasystem.core.interceptor.LoginInterceptor" /> 11 <interceptor-stack name="teamwareStack"> 12 <interceptor-ref name="i18n" /> 13 <interceptor-ref name="login" /> 14 <interceptor-ref name="basicStack" /> 15 </interceptor-stack> 16 </interceptors> 17 ..........
在下面配置下默认的拦截器就好了。