Struts2 2.18 的 interceptor


首先定义我们自己的Interceptor

Java代码
  1. package  com.web.interceptor;  
  2.   
  3. import  javax.servlet.http.HttpServletRequest;  
  4. import  org.apache.struts2.StrutsStatics;  
  5. import  org.apache.struts2.convention.annotation.ParentPackage;  
  6.   
  7. import  com.common.utils.StringUtil;  
  8. import  com.opensymphony.xwork2.ActionContext;  
  9. import  com.opensymphony.xwork2.ActionInvocation;  
  10. import  com.opensymphony.xwork2.interceptor.AbstractInterceptor;  
  11. import  com.opensymphony.xwork2.util.ValueStack;  
  12. import  com.web.action.Constants;  
  13.   
  14. @ParentPackage (value= "struts-default" )  
  15. public   class  LoginInterceptor  extends  AbstractInterceptor  
  16. {  
  17.     /**  
  18.      *   
  19.      */   
  20.     private   static   final   long  serialVersionUID = 1L;  
  21.   
  22.     public   static   final  String USER_SESSION_KEY= "wallet.session.user" ;  
  23.       
  24.     public   void  init()  
  25.     {}  
  26.       
  27.     @SuppressWarnings ( "unchecked" )  
  28.     @Override   
  29.     public  String intercept(ActionInvocation invocation)  throws  Exception  
  30.     {   
  31.         ActionContext actionContext = invocation.getInvocationContext();  
  32.         HttpServletRequest request = (HttpServletRequest) actionContext.get(StrutsStatics.HTTP_REQUEST);  
  33.   
  34.         String uri = request.getRequestURI();  
  35.         if (uri.indexOf( "index.action" )>- 1 )  
  36.         {  
  37.             return  Constants.INPUT;   
  38.         }  
  39.         else   if (uri.indexOf( "login.action" )>- 1 )  
  40.         {  
  41.             ValueStack vt = actionContext.getValueStack();  
  42.             java.util.Map<String, Object> mp = vt.getContext();  
  43.               
  44.             java.util.Map<String, Object> paraMap = (java.util.Map<String,Object>)mp.get(ActionContext.PARAMETERS);  
  45.             String[] name = (String[])paraMap.get("user.name" );  
  46.            
  47.             //String name = (String)vt.findValue("user.name");   
  48.             if (name!= null  && name.length> 0  && !StringUtil.hasContent(name[ 0 ]))  
  49.             {  
  50.                 vt.set("fielderror" "必须输入用户名!" );  
  51.                 return  Constants.INPUT;  
  52.             }  
  53.             else   return  invocation.invoke();  
  54.         }  
  55.         else   
  56.             return  invocation.invoke();  
  57.     }  
  58.   
  59.     public   void  destroy()  
  60.     {}  
  61. }  
package com.web.interceptor;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.StrutsStatics;
import org.apache.struts2.convention.annotation.ParentPackage;

import com.common.utils.StringUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.opensymphony.xwork2.util.ValueStack;
import com.web.action.Constants;

@ParentPackage(value="struts-default")
public class LoginInterceptor extends AbstractInterceptor
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public static final String USER_SESSION_KEY="wallet.session.user";
	
	public void init()
	{}
	
	@SuppressWarnings("unchecked")
	@Override
	public String intercept(ActionInvocation invocation) throws Exception
	{ 
		ActionContext actionContext = invocation.getInvocationContext();
		HttpServletRequest request = (HttpServletRequest) actionContext.get(StrutsStatics.HTTP_REQUEST);

		String uri = request.getRequestURI();
		if(uri.indexOf("index.action")>-1)
		{
			return Constants.INPUT; 
		}
		else if(uri.indexOf("login.action")>-1)
		{
			ValueStack vt = actionContext.getValueStack();
			java.util.Map<String, Object> mp = vt.getContext();
			
			java.util.Map<String, Object> paraMap = (java.util.Map<String,Object>)mp.get(ActionContext.PARAMETERS);
			String[] name = (String[])paraMap.get("user.name");
		 
			//String name = (String)vt.findValue("user.name");
			if(name!=null && name.length>0 && !StringUtil.hasContent(name[0]))
			{
				vt.set("fielderror", "必须输入用户名!");
				return Constants.INPUT;
			}
			else return invocation.invoke();
		}
		else
			return invocation.invoke();
	}

	public void destroy()
	{}
}


将其注册到struts中,修改struts2.xml文件增加如下内容:

Java代码
  1. < package  name= "struts-interceptor"   extends = "struts-default" >   
  2.         <interceptors>   
  3.             <interceptor name="loginInterceptor"   class = "com.web.interceptor.LoginInterceptor" />  
  4.         </interceptors>   
  5.     </package >   
<package name="struts-interceptor" extends="struts-default"> 
		<interceptors> 
		    <interceptor name="loginInterceptor" class="com.web.interceptor.LoginInterceptor"/>
		</interceptors> 
	</package> 



这样我们自己定义的Interceptor已经设置完成,如果想使用则可以用annotation指定。如

Java代码
  1. @InterceptorRefs ({  
  2.   @InterceptorRef ( "loginInterceptor" ),  
  3.   @InterceptorRef ( "defaultStack" )  
  4. })  
  5. @ParentPackage (value= "struts-interceptor" )    
  6. @Namespace (value= "/application" )    
  7. @Result (name= "input" ,location= "/application/login.jsp" )  
  8. public   class  LoginAction  extends  BaseAction  
  9. {  
  10.     private   static   final   long  serialVersionUID = 1L;  
  11. }     
@InterceptorRefs({
  @InterceptorRef("loginInterceptor"),
  @InterceptorRef("defaultStack")
})
@ParentPackage(value="struts-interceptor")  
@Namespace(value="/application")  
@Result(name="input",location="/application/login.jsp")
public class LoginAction extends BaseAction
{
	private static final long serialVersionUID = 1L;
}	


我们在LoginAction里使用了刚刚自定义的Interceptor。

官网教程提供的解决方案:

引用

If you get errors like "Unable to find interceptor class referenced by ref-name XYZ". This means that the package where Convention is placing your actions, does not extend the package where the interceptor is defined. To fix this problem either 1)Use @ParentPackage annotation(or struts.convention.default.parent.package) passing the name of the package that defines the interceptor, or 2) Create a package in XML that extends the package that defines the interceptor, and use @ParentPackage(or struts.convention.default.parent.package) to point to it.

你可能感兴趣的:(apache,Web,jsp,struts,servlet)