在Struts2中,通过实现com.opensymphony.xwork2.interceptor.Interceptor
接口来开发自己的拦截器。当我们的拦截器不需要打开资源时,也可以通过直接继承AbstractInterceptor
类来自定义拦截器。
在实现AbstractInterceptor
的intercept(ActionInvocation invocation)
方法时,可以获得ActionInvocation 参数,这个参数又可以获得被拦截的Action实例。最后通过调用invocation.invoke()
方法将控制权转给下一个拦截器,或者传给Action的execute方法。
自定义拦截器
使用strus2框架,设计一个拦截器,使得只有登入才能看到自己的信息
第一步:拦截器类
package interceptor;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import pojo.Person;
public class LoginInterceptor extends AbstractInterceptor {
@Override
public String intercept(ActionInvocation arg0) throws Exception {
HttpServletRequest request=ServletActionContext.getRequest();
if(isCheck(request.getRequestURI())){
Map<String,Object>session=arg0.getInvocationContext().getSession();
Person p=(Person)session.get("user");
if(p==null){
return Action.LOGIN;
}
}
return arg0.invoke();
}
private boolean isCheck(String path){
if(path.endsWith("/login.action")){
return false;
}else{
return true;
}
}
}
这个拦截器类通过比较action的地址来筛选Action,从而区分要拦截哪个Action,我们要拦截除login.action外所有action。
第二步:Action类
package action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import pojo.Person;
public class LoginAction extends ActionSupport {
private Person p;
public String login(){
ActionContext.getContext().getSession().put("user",p);
return SUCCESS;
}
public String welcome(){
return SUCCESS;
}
public Person getP() {
return p;
}
public void setP(Person p) {
this.p = p;
}
}
该Action类通过login这个方法(即login.action)将用户保存到session。
第三步:javabean类
package pojo;
public class Person {
private String name;
private String password;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
第四步:view页面
index.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>
<form action="login.action" method="post">
<p>
用户名:<input name="p.name" type="text">
</p>
<p>
用户名:<input name="p.password" type="password">
</p>
<input type="submit" value="登入">
</form>
</body>
</html>
result.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">
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
hello,<s:property value="#session.user.name"/>,你的密码是:<s:property value="#session.user.password"/>
</body>
</html>
第五步:配置文件——struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="conventer" namespace="/" extends="struts-default">
<interceptors>
<interceptor name="login" class="interceptor.LoginInterceptor"></interceptor>
<interceptor-stack name="loginStack">
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="login"></interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="loginStack"></default-interceptor-ref>
<global-results>
<result name="login">/index.jsp</result>
</global-results>
<action name="login" class="action.LoginAction" method="login">
<result name="success" type="redirectAction">
welcome.action
</result>
</action>
<action name="welcome" class="action.LoginAction" method="welcome">
<result name="success">/result.jsp</result>
</action>
</package>
</struts>
通过interceptor 标签自定义拦截器,再将自定义拦截器加到一个interceptor-stack拦截器栈中,同时把默认拦截器也加入拦截器栈中然后将拦截器栈设置为默认拦截器。最后设置一个全局result,这样被拦截的action可以指向这个全局result指定的login.jsp页面。