Struts2 拦截器的配置:
第一步:在struts.xml文件中加入拦截器配置:
我们可以在配置拦截器的时候,自己定义一个拦截器栈来包含我们自己定义的拦截器(注意:一定要将 defaultStack 加进去,这是框架自带的拦截器,能够帮助我们完成表单数据封装等功能)
<!-- 配置拦截器 -->
<interceptors>
<interceptor name="mytimer" class="com.test.interceptor.TimerInterceptor" />
<interceptor name="checklogin" class="com.test.interceptor.LoginInterceptor" />
<!-- 自己定义的拦截器栈 -->
<interceptor-stack name="myStack">
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="mytimer"></interceptor-ref>
</interceptor-stack>
</interceptors>
第二步:使用配置的拦截器
第一种方法:在action中使用配置的拦截器:
<action name="param" class="com.test.action.ParamAction">
<result>/ognl.jsp</result>
<!-- action里面配置拦截器 -->
<interceptor-ref name="myStack"></interceptor-ref>
</action>
第二种方法:配置默认拦截器(全局拦截器):
注意:在单个action配置了拦截器,就不在给这个action配置默认的拦截器,需要自己配置需要的拦截器(注意:别忘了 defaultStack 拦截器)
<!-- 默认拦截器,在action外面配置拦截器,为该package下的所有action都配置拦截器 -->
<default-interceptor-ref name="myStack"></default-interceptor-ref>
登录拦截实例:
struts.xml配置(src文件目录下):
<?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> <package name="gyx" extends="struts-default">
<!-- 配置拦截器 -->
<interceptors>
<interceptor name="mytimer" class="com.test.interceptor.TimerInterceptor" />
<interceptor name="checklogin" class="com.test.interceptor.LoginInterceptor" />
<interceptor-stack name="myStack">
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="mytimer"></interceptor-ref>
</interceptor-stack>
</interceptors>
<!-- 默认拦截器,在action外面配置拦截器,为该package下的所有action都配置拦截器 -->
<default-interceptor-ref name="myStack"></default-interceptor-ref>
<!-- 全局页面配置 -->
<global-results>
<result name="login" type="redirect">/login.jsp</result>
<result name="error">/error.jsp</result>
</global-results>
<action name="param" class="com.test.action.ParamAction">
<result>/ognl.jsp</result>
<interceptor-ref name="checklogin"></interceptor-ref>
<interceptor-ref name="myStack"></interceptor-ref>
<!-- action里面配置拦截器
<interceptor-ref name="myStack"></interceptor-ref>
-->
</action>
<action name="login" class="com.test.action.LoginAction">
<result name="success" type="redirect">/param.jsp</result>
</action>
</package>
</struts>
LoginAction(src下的com.test.action包下):
package com.test.action;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
private String uname;
private String upwd;
@Override
public void validate() {
if (uname.length()==0) {
this.addFieldError("uname", "用户名为空!");
}
if (upwd.length()==0) {
this.addFieldError("upwd", "密码为空!");
}
}
public String execute() throws Exception {
if ("admin".equals(uname)&&"123".equals(upwd)) {
System.out.println("登录成功");
Map session = ActionContext.getContext().getSession();
session.put("loginok", "admin");
return "success";
}else{
System.out.println("登录失败");
return "error";
}
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getUpwd() {
return upwd;
}
public void setUpwd(String upwd) {
this.upwd = upwd;
}
}
ParamAction(src下的com.test.action包下):
package com.test.action;
import java.util.HashMap;
import java.util.Map;
import com.gyx.entity.User;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class ParamAction extends ActionSupport{
private User user;
@Override
public String execute() throws Exception {
System.out.println("传入的参数为:"+user);
Map session = ActionContext.getContext().getSession();
session.put("hero", "剑魔");
Map city = new HashMap();
city.put("北京", "中国");
city.put("东京", "日本");
city.put("莫斯科", "俄罗斯");
city.put("巴黎", "法国");
session.put("city", city);
return SUCCESS;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
LoginInterceptor(src下的com.test.interceptor包下):登录拦截器
package com.test.interceptor;
import java.util.Map;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class LoginInterceptor extends AbstractInterceptor {
@Override
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("进入登录拦截器");
Map session = invocation.getInvocationContext().getSession();
Object user = session.get("loginok");
if (user==null) {
return Action.LOGIN;
}else{
return invocation.invoke();
}
}
}
TimerInterceptor(src下的com.test.interceptor包下):计时拦截器:
package com.test.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class TimerInterceptor extends AbstractInterceptor{
@Override
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("进入timer拦截器");
long begintimer = System.currentTimeMillis();
//放行
String result = invocation.invoke();
long exetimer = System.currentTimeMillis()-begintimer;
System.out.println("执行action,用了"+exetimer);
return result;
}
}
jsp页面:login.jsp
<!-- 这句加在页面顶部(添加标签库) -->
<%@ taglib uri="/struts-tags" prefix="s"%>
<div>
<s:form action="login.action" method="post">
<s:textfield name="uname" label="用户名:"/><br/>
<s:password name="upwd" label="密码:" /><br/>
<s:submit value="登录"/>
</s:form>
</div>
param.jsp:
<a href="param?user.name=VN&user.age=21">链接</a>
<form action="param">
姓名:<input type="text" name="user.name" /><br/>
年龄:<input type="text" name="user.age" /><br/>
日期:<input type="text" name="user.now" /><br/>
性别:男<input type="radio" name="user.sex" value="true"/>
女<input type="radio" name="user.sex" value="false"/><br/>
爱好:打球<input type="checkbox" name="user.hobbies" value="ball"/>
玩游戏<input type="checkbox" name="user.hobbies" value="game"/>
看书<input type="checkbox" name="user.hobbies" value="book"/>
<input type="submit" value="提交" />
</form>
ognl.jsp:
<!-- 这句加在页面顶部(添加标签库) -->
<%@ taglib uri="/struts-tags" prefix="s"%>
值栈取值:<s:property value="user.name" /><br/>
<s:property value="user.hobbies" /><br/>
作用域取值:<s:property value="#attr.hero" />(在三大作用域中,自动的从小往上找,把第一个找到的显示)
<br/>
MAP取值:<s:property value="#session.city" /><br/>
取MAP中的一个值:<s:property value="#session.city.巴黎" /><br/>
取MAP中的一个值:<s:property value="#session.city['北京']" /><br/>
error.jsp:
<h1>用户登录失败</h1>
<h1>用户名或者密码错误!</h1>