在struts2中获取请求action名称或参数

来源:互联网 发布:上海嘉桥数据咨询公司 编辑:程序博客网 时间:2024/06/05 18:22
1.Interceptor配置
Xml代码  收藏代码
  1. <package name="sysManageServices" namespace="/" extends="struts-default">  
  2.         <interceptors>  
  3.             <interceptor name="reqCheck" class="interceptor.RequestInterceptor"/>  
  4.             <interceptor-stack name="teamwareStack">  
  5.                 <interceptor-ref name="requestCheck"/>  
  6.                 <interceptor-ref name="defaultStack"/>  
  7.             </interceptor-stack>  
  8.         </interceptors>  
  9.         <default-interceptor-ref name="teamwareStack"/>  
  10.         <action name="searchUserInfo" class="action.SearchUserInfoAction">  
  11.             <result name="success">/page/userManage.jsp</result>  
  12.             <result name="error">/page/result.jsp</result>  
  13.         </action>  
  14.     </package>  

2.拦截器代码
Java代码  收藏代码
  1. package interceptor.RequestInterceptor;  
  2.   
  3. import java.util.Map;  
  4.   
  5. import javax.servlet.http.HttpSession;  
  6.   
  7. import com.opensymphony.xwork2.Action;  
  8. import com.opensymphony.xwork2.ActionSupport;  
  9. import com.opensymphony.xwork2.ActionInvocation;  
  10. import com.opensymphony.xwork2.interceptor.AbstractInterceptor;  
  11.   
  12. public class RequestInterceptor extends AbstractInterceptor {  
  13.   
  14.     private static final long serialVersionUID = 3244973830196015811L;  
  15.     private HttpSession session;  
  16.   
  17.     public void setSession(Map<String, Object> session) {  
  18.         this.session = (HttpSession) session;  
  19.     }  
  20.   
  21.     public String intercept(ActionInvocation aInvocation) throws Exception {  
  22.           
  23.         // 获取请求的action名称  
  24.         String actionName = aInvocation.getInvocationContext().getName();  
  25.           
  26.         // 获取action后附带参数  
  27.         Map parameters = aInvocation.getInvocationContext().getParameters();  
  28.           
  29.         if (hrefList.contains(requestName)) {  
  30.             String result = actionInvocation.invoke();  
  31.             return result;  
  32.         } else {  
  33.             ActionSupport aSupport =  (ActionSupport) actionInvocation.getAction();  
  34.             aSupport.addActionMessage("不存在该页面!请返回");  
  35.             return Action.ERROR;  
  36.         }     

原创粉丝点击