Struts2的拦截器功能非常强大,几乎每个项目都会用到,其实使用起来也是非常的简单。本文以登录为例子进行介绍。
代码如下:
struts.xml文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<!--登录的拦截器 -->
<package name="common" extends="json-default">
<interceptors>
<!--注册登录检查拦截器 -->
<interceptor name="loginInter" class="com.lmc.interceptor.LoginInterceptor"/>
<interceptor name="openSessionInter" class="com.lmc.interceptor.OpenSessionInViewInterceptor"/>
<!--注册拦截器栈,将自定义拦截器与defaultStack打包,避免丢掉默认拦截器的引用 -->
<interceptor-stack name="loginStack">
<interceptor-ref name="loginInter"/>
<interceptor-ref name="openSessionInter"/>
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
</interceptors>
<!--将当前包下默认引用的拦截器,改为loginStack。这样做不会丢掉对defaultStack的引用,并且又包含
了登录检查拦截器,使action下不必一个一个配置拦截器,简化配置。-->
<default-interceptor-ref name="loginStack"/>
<!--定义全局的result,即当前包下所有的action,相当于都定义了这些result-->
<global-results>
<!-- 跳转到登录页 -->
<result name="login" type="redirectAction">
<param name="namespace">/login</param>
<param name="actionName">Login$toLogin</param>
</result>
<!-- 跳转到错误页面 -->
<result name="error">/WEB-INF/login/loginError.jsp</result>
</global-results>
</package>
<!-- 登录相关 -->
<package name="login" extends="struts-default" namespace="/login">
<action name="*$*" class="com.lmc.action.{1}Action" method="{2}">
<result name="success">${targetURL}</result>
</action>
</package>
<!-- 其他操作,继承common进行登录验证 -->
<package name="manager" extends="common" namespace="/manager">
<action name="*$*" class="com.lmc.action.{1}Action" method="{2}">
<result name="success">${targetURL}</result>
</action>
</package>
</struts>
toLogin.jsp:
<%@page pageEncoding="utf-8" isELIgnored="false"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<form action="Login$loginCheck.action" method="post">
id:<input type="text" name="id" /><br/>
name:<input type="text" name="name" /><br/>
<input type="submit" value="提交"/>
</form>
LoginAction:
package com.lmc.action;
import javax.annotation.Resource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.lmc.dao.LoginDao;
@Controller
@Scope("prototype")
public class LoginAction extends BaseAction{
private static final long serialVersionUID = 1L;
@Resource
private LoginDao loginDao;
private Integer id;
private String name;
public String loginCheck(){
int num = loginDao.loginCheck(id,name);//loginCheck方法为登录验证,具体就不贴出了,因为与本文主题不相关
if(num>0){
session.put("loginSession", "已经登录");//把登录信息放入session,用于拦截器的检查
System.out.println("登录session:"+session.get("loginSession"));
targetURL = "/WEB-INF/login/loginSuccess.jsp";
return "success";
}
else{
targetURL = "/WEB-INF/login/loginError.jsp";
return "error";
}
}
/**
* 销毁登录session
*/
public String loginOut(){
session.remove("loginSession");
// super.targetURL = "/WEB-INF/login/toLogin.jsp";
return "login";
}
/**
*跳转到登录页面
*/
public String toLogin(){
targetURL = "/WEB-INF/login/toLogin.jsp";
return "success";
}
public LoginDao getLoginDao() {
return loginDao;
}
public void setLoginDao(LoginDao loginDao) {
this.loginDao = loginDao;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
LoginInterceptor:
package com.lmc.interceptor;
import java.util.Map;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
public class LoginInterceptor implements Interceptor {
public void destroy() {
}
public void init() {
}
public String intercept(ActionInvocation ai) throws Exception {
Map<String, Object> session = ai.getInvocationContext().getSession();
String str = (String)session.get("loginSession");
if(str == null) {
// System.out.println("登录信息str:"+str);
return "login";
} else {
// System.out.println("登录信息str:"+str);
return ai.invoke();
}
}
}
OpenSessionInViewInterceptor:
package com.lmc.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
public class OpenSessionInViewInterceptor implements Interceptor {
public void destroy() {
}
public void init() {
}
public String intercept(ActionInvocation ai) throws Exception {
ai.invoke();
return null;
}
}