首先定义我们自己的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"
);
-
-
-
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()
-
{}
-
}
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
>
<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;
-
}
@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_和_filter区别
Struts2拦截器(Interceptor) Struts2拦截器(Interceptor)
详细介绍struts2 interceptor
NULL 博文链接:https://weizhilizhiwei.iteye.com/blog/1005210
struts2 Interceptor拦截器 http://whitewolf.0fees.net
STRUTS2:拦截器Interceptor
简单的拦截器例子,适合初学者学习,也是一个备忘录
应用拦截器 优化增删改查
Struts2拦截器是在访问某个Action或Action的某个方法,字段之前或之后实施拦截,并且Struts2拦截器是可插拔的,拦截器是AOP的一种实现.
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,Interceptor struts2拦截器实例,两套实例,一套是针对单个的action配置的,另一套实例是针对全局的action配置的拦截器interceptor
用注解(annotation)方式配置struts2 (webwork) 中action的拦截器(interceptor)
3.struts2_interceptor 拦截器,自定义拦截器 4.struts2_log4j 5.struts2_spring struts spring 整合 以上功能包含完整示例代码 Eclipse Java EE IDE for Web Developers. Build id: 20090920-1017 Tomcat6.0 下编译...
struts1 拦截器功能的实现。struts2 中的interceptor很方便。但在struts1中没有这个功能,用这个插件很容易实现。
struts2 拦截器_教程.zip
Struts2.x_Interceptor已经打包好了,解压后直接用,
《Struts 2实战》结合实例介绍了Struts 2框架,主要内容包括Action、Result、Interceptor等框架组件,基于注解的配置选项等新特征,Struts 2插件 FreeMarker,如何从Struts 1和WebWork 2迁移到Struts 2,Ajax标签、...
综合使用servlet filter与struts2 interceptor对权限进行验证。
07 Struts 2的基石——拦截器(Interceptor) 08 在Struts 2中实现IoC 09 在Struts 2中实现文件上传 10 在Struts 2中实现CRUD 11 Struts 2中的OGNL 12 Struts 2的新表单标志的使用 13 Struts 2与AJAX
很好的struts2入门教程,网上搜集整理的! 内容: ·常用的Struts 2.0的标签(Tag)介绍 ·Struts 2.0的Action讲解 ·Struts2国际化(i18n)您的应用程序 ·Struts2.0转换器(Converter) ·Struts 2.0中实现...