Struts拦截器
Struts拦截器和过滤器的区别:
过滤器:对所有的东西都过滤。包括css,.js,.jpg等。
拦截器:只对action拦截。是struts工具。
一、拦截器配置。
1.创建一个类,让其继承自AbstractInterceptor
2.配置struts.xml
1)添加<interceptors>配置节,添加拦截器配置。
2)在action中引用,<interceptors>中的配置。
注意:
1.在action中需要添加defaultStack引用
2.在建<interceptors>可以建立stack,把defaultStack也添加在里面,在引用的时候引用此stack即可。
<?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="true" /> <package name="default" namespace="/" extends="struts-default"> <interceptors> <interceptor name="lanjieqi" class="com.itnba.maya.util.Lanjieqi"></interceptor> <interceptor-stack name="newStack"> <interceptor-ref name="lanjieqi"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </interceptor-stack> </interceptors> <action name="*_*" class="com.itnba.maya.controller.{1}Action" method="{2}"> <!-- 直接引用newStack --> <interceptor-ref name="newStack"></interceptor-ref> <result> {1}_{2}.jsp </result> </action> </package> </struts>
二、在拦截器中使用内置对象。
获取Session
1.ActionContext 获取Map.
2.ServletActionContext
3.实现SessionAware接口
*****************************************************************
实例:使用拦截器实现登录身份验证。
有两个action是不能拦截。打回登录界面action,登录提交验证并写 session的action
在登录验证的auth方法中,分两种情况:
1.第一次登录验证。此时,session中没有信息,需要向session中添加登录信息
2.非第一次登录验证。此时,session中有信息,不需要再入session中添加信息
建一个User对象
package com.itnba.maya.model; public class User { 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="true" /> <package name="default" namespace="/" extends="struts-default"> <!-- --> <interceptors> <interceptor name="lanjieqi" class="com.itnba.maya.util.Lanjieqi"></interceptor> <interceptor name="Loginlanjieqi" class="com.itnba.maya.util.Loginlanjieqi"></interceptor> <interceptor-stack name="newStack"> <interceptor-ref name="lanjieqi"></interceptor-ref> <interceptor-ref name="Loginlanjieqi"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </interceptor-stack> </interceptors> <global-results> <result name="nologin">Login_login.jsp</result><!-- session里没有值再跳回Login_login.jsp --> </global-results> <!-- Login_login和Login_home不能拦截掉,写上面不受拦截器控制 --> <action name="Login_login" class="com.itnba.maya.controller.LoginAction" method="login"> <result>Login_login.jsp</result> </action> <action name="Login_home" class="com.itnba.maya.controller.LoginAction" method="home"> <result>Login_home.jsp</result> </action> <action name="*_*" class="com.itnba.maya.controller.{1}Action" method="{2}"> <interceptor-ref name="newStack"></interceptor-ref> <result> {1}_{2}.jsp </result> </action> </package> </struts>
ActionSupport类
package com.itnba.maya.controller; import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext; import com.itnba.maya.model.User; import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport { HttpSession session=ServletActionContext.getRequest().getSession(); private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public String login(){ //login页面直接跳走 return SUCCESS; } public String home(){ //模拟从数据库中查出来 List<String> list=new ArrayList<String>(); list.add("zhangsan"); list.add("lisi"); list.add("wangwu"); list.add("zhaoliu"); //第一次session肯定是null的 Object obj=session.getAttribute("user"); if(obj==null){ if(user!=null){ //不判断下会空会报空指针错误 if(list.contains(user.getUserName())){ //看看账号是否有,这里没验证密码 session.setAttribute("user", user.getUserName());//有就往session放值 return SUCCESS; } else{ return "nologin"; //不包含就走"nologin" } }else{ return "nologin"; } } else{ return SUCCESS; } } }
拦截器的类,判断sessioni是否有值
package com.itnba.maya.util; import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.AbstractInterceptor; public class Loginlanjieqi extends AbstractInterceptor { @Override public String intercept(ActionInvocation arg0) throws Exception { //先获取session HttpSession session=ServletActionContext.getRequest().getSession(); //session如果没值就转到nologin if(session.getAttribute("user") == null){ return "nologin"; } return arg0.invoke(); } }
jsp页面
登录
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!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=utf-8"> <title>Insert title here</title> </head> <body> <h1>这是登录页面</h1> <form action="Login_home"> 账号:<input type="text" name="user.userName"><br> 密码:<input type="password" name="user.password"><br> <input type="submit" value="登录"> </form> </body> </html>
home验证session之后的页面
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ 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=utf-8"> <title>Insert title here</title> </head> <body> 账号:<s:property value="user.userName"/> </body> </html>