Struts2拦截器配置和使用
拦截器是Struts2最强大的特性之一,它是一种可以让用户在Action执行之前和Result执行之后进行一些功能处理的机制。
说到拦截器interceptor,就会想到过滤器filter:
过滤器filter是对所有的东西进行过滤,包括.css,.js,.jpg等这些内容都过滤;
拦截器interceptor是对action进行拦截,是属于struts的工具。
这里以实现登录验证为例来进行拦截器的使用
首先创建一个Login类:
package com.dao; public class Login { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
然后配置struts.xml:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="false" /> <package name="default" namespace="/" extends="struts-default"> <interceptors> <interceptor name="myin" class="com.util.Interceptor"></interceptor> <interceptor name="login" class="com.util.SuccessInterceptor"></interceptor> <!--具体的name和class必须写,指定这个拦截器的位置和名称方便下面调用--> <interceptor-stack name="newStack"> <interceptor-ref name="login"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> <!-- 这个是系统默认的Stack,如果你只调用上面自己写的Stack,则会只执行login拦截器的方法,很多地方会发生错误接收不到值--> </interceptor-stack> </interceptors> <global-results> <!--这个是公共类,所有的action都会走这个results --> <result name="nologin">/Login/login.jsp</result> </global-results> <action name="Login_login" class="com.controller.LoginAction" method="login"> <result>/Login/login.jsp</result> </action> <action name="Login_success" class="com.controller.LoginAction" method="success"> <result>/Login/success.jsp</result> </action> <action name="*_*" class="com.controller.{1}Action" method="{2}"> <interceptor-ref name="newStack"></interceptor-ref> <result> {1}/{2}.jsp </result> </action> </package> </struts>
配置LoginAction.java:
package com.controller; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext; import org.apache.struts2.interceptor.SessionAware; import org.apache.tomcat.jni.User; import com.dao.Login; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; public class LoginAction extends ActionSupport implements ModelDriven<Login>,SessionAware { private Map<String,Object> session ; private Login user = new Login(); public String login(){ return SUCCESS; } public String success(){ java.util.List<String> uid = new ArrayList<String>(); uid.add("admin"); uid.add("zhangsan"); uid.add("lisi"); uid.add("wangwu"); ActionContext context=ActionContext.getContext(); session=context.getSession(); Object obj = session.get("user"); if(obj==null){ if(uid.contains(user.getUsername())){ session.put("user", user.getUsername()); return SUCCESS; } else{ return "nologin"; } } else{ return SUCCESS; } } @Override public void setSession(Map<String, Object> arg0) { // TODO 自动生成的方法存根 this.session=arg0; } @Override public Login getModel() { return user; } }
配置SuccessInterceptor.java:
package com.util; import java.util.Map; import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.AbstractInterceptor; public class SuccessInterceptor extends AbstractInterceptor { private Map<String,Object> session ; @Override public String intercept(ActionInvocation arg0) throws Exception { System.out.println("asdasdas"); session= ActionContext.getContext().getSession(); if(session.get("user")==null){ return "nologin"; } return arg0.invoke(); } }
实现login.jsp页面:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <h2>Login</h2> <form action="Login_success"> username:<input type="text" name="username" ><br> password:<input type="text" name="password" ><br> <input type="submit" value="go" > </form> </body> </html>
实现success.jsp页面:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <h2>success</h2> username:<s:property value="#session.user" /><br> password:<s:property value="password" /> <s:debug></s:debug> </body> </html>
一、拦截器的配置
1.SuccessInterceptor.java文件为拦截器的配置,继承自AbstractInterceptor接口。
2.配置struts.xml文件
<package name="default" namespace="/" extends="struts-default"> <interceptors> <interceptor name="myin" class="com.util.Interceptor"></interceptor> <interceptor name="login" class="com.util.SuccessInterceptor"></interceptor>//自己定义的一个名为login的拦截器,调用自com.util.SuccessInterceptor <interceptor-stack name="newStack"> <interceptor-ref name="login"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </interceptor-stack>//自定义一个名为newStack的拦截器,里面包含login的拦截器和defaultStack默认的拦截器 </interceptors> <global-results> <result name="nologin">/Login/login.jsp</result> </global-results>//通用走nologin的时候都走此处 <action name="Login_login" class="com.controller.LoginAction" method="login"> <result>/Login/login.jsp</result> </action> <action name="Login_success" class="com.controller.LoginAction" method="success"> <result>/Login/success.jsp</result> </action> <action name="*_*" class="com.controller.{1}Action" method="{2}"> <interceptor-ref name="newStack"></interceptor-ref> //调用自定义的拦截器 <result> {1}/{2}.jsp </result> </action> </package>
二、在拦截器中使用内置对象
1.ActionContext 获取Map.
2.ServletActionContext 获取HttpSession
3.实现SessionAware接口
这三种形式详见http://www.cnblogs.com/claricre/p/6548874.html
这样定义完毕后的登录并不完善,没有跳转404界面,以及没有验证用户名密码同时正确的情况,仅作为例子参考。