首先定义我们自己的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指定。如
@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.
分享到:
相关推荐
详细介绍struts2 interceptor
NULL 博文链接:https://weizhilizhiwei.iteye.com/blog/1005210
struts2 Interceptor拦截器 http://whitewolf.0fees.net
Struts2拦截器(Interceptor) Struts2拦截器(Interceptor)
Struts2_interceptor_和_filter区别
struts1 拦截器功能的实现。struts2 中的interceptor很方便。但在struts1中没有这个功能,用这个插件很容易实现。
STRUTS2:拦截器Interceptor
用注解(annotation)方式配置struts2 (webwork) 中action的拦截器(interceptor)
简单的拦截器例子,适合初学者学习,也是一个备忘录
Struts2拦截器是在访问某个Action或Action的某个方法,字段之前或之后实施拦截,并且Struts2拦截器是可插拔的,拦截器是AOP的一种实现.
Struts2.3.6实现自定义拦截器Interceptor http://blog.csdn.net/alanchen520/article/details/34086699
struts2 interceptor 的一个简单的 登录系统小demo
综合使用servlet filter与struts2 interceptor对权限进行验证。
NULL 博文链接:https://johnson2132.iteye.com/blog/720398
struts2 拦截器_教程.zip
org.apache.struts2.interceptor.debugging, org.apache.struts2.interceptor.validation, org.apache.struts2.servlet.interceptor, org.apache.struts2.util, org.apache.struts2.views, org.apache.struts2....
Struts2.x_Interceptor已经打包好了,解压后直接用,
应用拦截器 优化增删改查
07 Struts 2的基石——拦截器(Interceptor) 08 在Struts 2中实现IoC 09 在Struts 2中实现文件上传 10 在Struts 2中实现CRUD 11 Struts 2中的OGNL 12 trus 2的新表单标志的使用 13 Struts 2与AJAX
struts2,Interceptor struts2拦截器实例,两套实例,一套是针对单个的action配置的,另一套实例是针对全局的action配置的拦截器interceptor