标签:des style c class blog code
首先定义我们自己的Interceptor
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文件增加如下内容
<package name="struts-interceptor" extends="struts-default">
<interceptors>
<interceptor
name="loginInterceptor"
class="com.web.interceptor.LoginInterceptor"/>
</interceptors>
</package>
这样我们自己定义的Interceptor已经设置完成,如果想使用则可以用annotation指定。如
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
@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.
标签:des style c class blog code
原文地址:http://www.cnblogs.com/sandea/p/3754062.html