标签:
在网页开发中有一个很重要的东西就是拦截器,就是在请求接收到的时候先到拦截器中进行一些逻辑处理,例如会话是否过期的验证等。在Struts2中我们可以编写一个拦截器的类,然后在struts.xml中简单配置,然后实现Action的拦截。下面我们来看看配置的具体内容。
1.书写拦截器的类,要继承com.opensymphony.xwork2.interceptor.AbstractInterceptor。
package com.babybus.sdteam.filter;
import java.util.Map;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class LoginIntercept extends AbstractInterceptor {
/**
* 序列号
*/
private static final long serialVersionUID = 1L;
@Override
public String intercept(ActionInvocation invocation) throws Exception {
// 获取上下文
ActionContext ctx = invocation.getInvocationContext();
// 获取request参数
Map<String, Object> params = ctx.getParameters();
// 判断是否传递参数,可以写自己的逻辑
if(params.size()<=0)
{
System.out.println("没有参数....");
return Action.LOGIN;
}
return invocation.invoke();
}
}
2.struts.xml配置
<!-- 定义一个拦截器 -->
<interceptors>
<interceptor name="authority" class="com.babybus.sdteam.filter.LoginIntercept">
</interceptor>
<!-- 拦截器栈 -->
<interceptor-stack name="mydefault">
<interceptor-ref name="defaultStack" />
<interceptor-ref name="authority" />
</interceptor-stack>
</interceptors>
<!-- 定义全局Result -->
<global-results>
<!-- 当返回login视图名时,转入/login.jsp页面 -->
<result name="login">/index.jsp</result>
</global-results>
<action name="logOn" class="com.babybus.sdteam.action.LoginAction">
<result name="loginSuccess">/UserList.jsp</result>
<interceptor-ref name="mydefault" />
</action>
3.通过如上在action中配置定义的拦截器去对指定的Action添加拦截器
结语

本站文章为 宝宝巴士 SD.Team 原创,转载务必在明显处注明:(作者官方网站: 宝宝巴士 )
转载自【宝宝巴士SuperDo团队】 原文链接: http://www.cnblogs.com/superdo/p/4737634.html
标签:
原文地址:http://www.cnblogs.com/superdo/p/4737634.html