标签:
用户登录页面login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% request.getSession().setAttribute("user", "has logined"); %> 用户已经登录!
要自定义拦截器需要实现com.opensymphony.xwork2.interceptor.Interceptor接口
package struts2.example.interceptor; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.Interceptor; public class PermissionInterceptor implements Interceptor { //在类被摧毁的时候会执行该方法 @Override public void destroy() { // TODO Auto-generated method stub } //在struts2启动的时候会对拦截器进行实例化 @Override public void init() { // TODO Auto-generated method stub } //当拦截到action的时候会执行该方法 @Override public String intercept(ActionInvocation invocation) throws Exception { //先判断用户是否登录 Object user=ActionContext.getContext().getSession().get("user"); if(user!=null){//user不为空,表示用户已登录 return invocation.invoke();//调用该方法被拦截到的方法就可以执行 } ActionContext.getContext().put("message", "你没有执行该操作的权限"); return "success"; } }登录结果页面showInfo.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> <title>My JSP 'showInfo.jsp' starting page</title> </head> <body> ${message} </body> </html>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.multipart.maxSize" value="10701096" /> <package name="csdn" namespace="/control" extends="struts-default"> <interceptors> <interceptor name="permissionInterceptor" class="struts2.example.interceptor.PermissionInterceptor" /> <interceptor-stack name="permissionInterceptorStack"> <interceptor-ref name="defaultStack" /> <interceptor-ref name="permissionInterceptor" /> </interceptor-stack> </interceptors> <global-results> <result name="success">/WEB-INF/page/showInfo.jsp</result> </global-results> <action name="list_*" class="struts2.example.action.FilterAction" method="{1}"> <interceptor-ref name="permissionInterceptorStack" /> </action> </package> </struts>在action里面注册了自定义拦截器后,struts2很多默认的拦截器会被屏蔽掉,也就无法使用struts2很多默认的功能。为了解决这个问题,可定义拦截器栈。在拦截器栈中,定义在前面的先执行,后定义的后执行。
<interceptor-stack name="defaultStack"> <interceptor-ref name="exception"/> <interceptor-ref name="alias"/> <interceptor-ref name="servletConfig"/> <interceptor-ref name="i18n"/> <interceptor-ref name="prepare"/> <interceptor-ref name="chain"/> <interceptor-ref name="debugging"/> <interceptor-ref name="scopedModelDriven"/> <interceptor-ref name="modelDriven"/> <interceptor-ref name="fileUpload"/> <interceptor-ref name="checkbox"/> <interceptor-ref name="multiselect"/> <interceptor-ref name="staticParams"/> <interceptor-ref name="actionMappingParams"/> <interceptor-ref name="params"> <param name="excludeParams">dojo\..*,^struts\..*</param> </interceptor-ref> <interceptor-ref name="conversionError"/> <interceptor-ref name="validation"> <param name="excludeMethods">input,back,cancel,browse</param> </interceptor-ref> <interceptor-ref name="workflow"> <param name="excludeMethods">input,back,cancel,browse</param> </interceptor-ref> </interceptor-stack>
标签:
原文地址:http://blog.csdn.net/lindonglian/article/details/46580691