您的位置:首页 > 编程语言 > Java开发

Struts2拦截器&拦截器栈 实例:登录验证拦截器

2017-02-14 22:02 399 查看
本篇源自 java1234小峰 尊重原创 热爱生活

测试所用源码可在本人百度网盘下载:
http://pan.baidu.com/s/1bpnDslh
Struts2 拦截器是在访问某个Action 或Action 的某个方法,字段之前或之后实施拦截,并且Struts2 拦截器是可

插拔的,拦截器是AOP的一种实现.
优点:通用功能的封装,提供了可重用性;

写两个拦截器和一个Action

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>

<package name="firstInterceptor" extends="struts-default">
<interceptors>
<interceptor name="display1" class="com.zhiqi.interceptor.FirstInterceptor"></interceptor>
<interceptor name="display2" class="com.zhiqi.interceptor.SecondInterceptor"></interceptor>
</interceptors>

<action name="myAction" class="com.zhiqi.action.MyAction">
<result name="success">result.jsp</result>
<interceptor-ref name="display1"></interceptor-ref>
<interceptor-ref name="display2"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>

</package>
</struts>
package com.zhiqi.interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class FirstInterceptor implements Interceptor{

/**
*
*/
private static final long serialVersionUID = 1L;

@Override
public void destroy() {
// TODO Auto-generated method stub
System.out.println("1FirstInterceptor销毁");
}

@Override
public void init() {
// TODO Auto-generated method stub
System.out.println("1FirstInterceptor初始化");
}

@Override
public String intercept(ActionInvocation invocation) throws Exception {
// TODO Auto-generated method stub
System.out.println("1FirstInterceptor的intercept方法前");
String str=invocation.invoke();
System.out.println("1FirstInterceptor的intercept方法后");
return str;
}

}

package com.zhiqi.interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class SecondInterceptor implements Interceptor {

/**
*
*/
private static final long serialVersionUID = 1L;

@Override
public void destroy() {
// TODO Auto-generated method stub
System.out.println("2SecondInterceptor销毁");
}

@Override
public void init() {
// TODO Auto-generated method stub
System.out.println("2SecondInterceptor初始化");
}

@Override
public String intercept(ActionInvocation invocation) throws Exception {
// TODO Auto-generated method stub
System.out.println("2SecondInterceptor的intercept方法前");
String str=invocation.invoke();
System.out.println("2SecondInterceptor的intercept方法后");
return str;
}

}






实例:登录验证拦截器

写两个Action,一个UserAction用于登录,一个OtherAction对比验证

写两个拦截器Interceptor,MyInterceptor和LoginInterceptor

然后配置xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>

<package name="first" extends="struts-default">
<interceptors>
<interceptor name="myInter" class="com.zhiqi.interceptor.MyInterceptor"></interceptor>
<interceptor name="loginInter" class="com.zhiqi.interceptor.LoginInterceptor"></interceptor>
</interceptors>

<action name="user" class="com.zhiqi.action.UserAction">
<result name="success">success.jsp</result>
<result name="error">error.jsp</result>
<interceptor-ref name="myInter"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>

<action name="other" class="com.zhiqi.action.OtherAction">
<result name="success">success.jsp</result>
<result name="error">error.jsp</result>
<interceptor-ref name="loginInter"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>

</package>
</struts>

public class UserAction extends ActionSupport {

/**
*
*/
private static final long serialVersionUID = 1L;
private User user;
private UserService userService=new UserService();
private String error;

public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String getError() {
return error;
}
public void setError(String error) {
this.error = error;
}

@Override
public String execute() throws Exception {
if(userService.login(user)){
ActionContext actionContext=ActionContext.getContext();
Map<String, Object> session=actionContext.getSession();
session.put("currentUser", user);
return SUCCESS;
}else{
this.error="用户名或密码错误";
return "error";
}
}

}


拦截器

package com.zhiqi.interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class MyInterceptor implements Interceptor{

/**
*
*/
private static final long serialVersionUID = 1L;

@Override
public void destroy() {
// TODO Auto-generated method stub
System.out.println("MyInterceptor destroy");
}

@Override
public void init() {
// TODO Auto-generated method stub
System.out.println("MyInterceptor init");
}

@Override
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("执行之前");
String result=invocation.invoke();
System.out.println("result:"+result);
System.out.println("执行之后");
return result;
}

}


拦截器

package com.zhiqi.interceptor;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class LoginInterceptor implements Interceptor {

/**
*
*/
private static final long serialVersionUID = 1L;

@Override
public void destroy() {
// TODO Auto-generated method stub
System.out.println("LoginInterceptor destroy");
}

@Override
public void init() {
// TODO Auto-generated method stub
System.out.println("LoginInterceptor init");
}

@Override
public String intercept(ActionInvocation invocation) throws Exception {
// TODO Auto-generated method stub
ActionContext actionContext=invocation.getInvocationContext();
Map<String, Object> session=actionContext.getSession();
Object obj=session.get("currentUser");
String result=null;
if(obj!=null){
result=invocation.invoke();
}else{
HttpServletRequest request=(HttpServletRequest)invocation.getInvocationContext().get(ServletActionContext.HTTP_REQUEST);
request.setAttribute("error", "请先登录");
result="error";
}
return result;
}

}



当输入错误账号和密码时:转发到error页面



输入正确,显示用户 web



此时 session中保存了用户,访问other,拦截器判断session,不会拦截。



重启阿帕奇,session消失,访问other,



此时被拦截了。

优化struts.xml文件的配置。

使用全局result

将error设为全局

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>

<package name="first" extends="struts-default">
<interceptors>
<interceptor name="myInter" class="com.zhiqi.interceptor.MyInterceptor"></interceptor>
<interceptor name="loginInter" class="com.zhiqi.interceptor.LoginInterceptor"></interceptor>
</interceptors>

<global-results>
<result name="error">error.jsp</result>
</global-results>

<action name="user" class="com.zhiqi.action.UserAction">
<result name="success">success.jsp</result>
<interceptor-ref name="myInter"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>

<action name="other" class="com.zhiqi.action.OtherAction">
<result name="success">success.jsp</result>
<interceptor-ref name="loginInter"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>

</package>
</struts>


拦截器栈interceptor-stack

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>

<package name="first" extends="struts-default">
<interceptors>
<interceptor name="myInter" class="com.zhiqi.interceptor.MyInterceptor"></interceptor>
<interceptor name="loginInter" class="com.zhiqi.interceptor.LoginInterceptor"></interceptor>
<interceptor-stack name="Inter">
<interceptor-ref name="myInter"></interceptor-ref>
<interceptor-ref name="loginInter"></interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="Inter"></default-interceptor-ref>

<global-results>
<result name="error">error.jsp</result>
</global-results>

<action name="user" class="com.zhiqi.action.UserAction">
<result name="success">success.jsp</result>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
<action name="other" class="com.zhiqi.action.OtherAction">
<result name="success">success.jsp</result>
</action>
</package>
</struts>




以后都采用这个写法。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: