Struts2提供了拦截器接口,允许用户将自己的逻辑封装在拦截器中,然后进行简单的配置就可以满足要求。
需求:用户登录则可以访问action中的任意方法,否则不允许用户访问。
1 . 编写自定义拦截器类,继承与AbstractInterceptor
package org.struts.user;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.tree.AbstractInsnNode;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class LoginInterceptor extends AbstractInterceptor {
/**
* 实现判断登录的入口方法,这里的intercept类似于AOP中的环绕通知
*/
@Override
public String intercept(ActionInvocation actionInvocation) throws Exception {
HttpServletRequest request = ServletActionContext.getRequest();
if(isIntercept(request.getRequestURI())){
//通过ActionInvocation对象获取ActionContext对象
//ActionContext对象可以直接访问session对象
<span style="color:#FF0000;">Map<String,Object> session = actionInvocation.getInvocationContext().getSession();</span>
User user = (User) session.get("user");
if(user==null){
return Action.LOGIN;
}
}
//正常执行
<span style="color:#CC0000;">return actionInvocation.invoke();</span>
}
//过滤掉不合适的请求
private boolean isIntercept(String path){
if(path.endsWith("/login.action")||path.endsWith("/login")){
return false;
}
return true;
}
}
2. 编写相应的action类
package org.struts.user;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport{
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
//跳转到欢迎页面
public String welcome(){
return SUCCESS;
}
//执行登录的方法
public String login(){
<span style="color:#FF0000;">ActionContext.getContext().getSession().put("user", user);</span>
return SUCCESS;
}
}
3.JavaBean
package org.struts.user;
public class User {
private String uname;
private String pwd;
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
4. 编写页面文件
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
请输入用户名和密码:
<br>
<form action="login" method="post">
<p>
用户名:
<input type="text" name="user.uname"/>
密码:
<input type="text" name="user.pwd"/>
<p>
<input type="submit" value="登录">
</form>
</body>
</html>
登录成功页面
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'welcome.jsp' starting page</title>
</head>
<body>
用户名:
<s:property value="#session.user.uname"/>
密码:
<s:property value="#session.user.pwd"/>
</body>
</html>
5. 修改配置文件struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.devMode" value="true"></constant>
<package name="aaa" namespace="/" extends="struts-default">
<interceptors>
<!-- 配置自定义的拦截器,名字为login -->
<interceptor name="login" class="org.struts.user.LoginInterceptor"/>
<!-- 配置自定义拦截器栈,把默认拦截器加入 -->
<interceptor-stack name="myStack">
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="login"></interceptor-ref>
</interceptor-stack>
</interceptors>
<!-- 设置默认的拦截器,覆盖struts-default的设置 -->
<default-interceptor-ref name="myStack"/>
<!-- 定义一个全局的result login,用于返回到登录页面使用 -->
<global-results>
<result name="login">/index.jsp</result>
</global-results>
<action name="login" class="org.struts.user.LoginAction" method="login">
<result type="redirectAction">welcome</result>
</action>
<action name="welcome" class="org.struts.user.LoginAction" method="welcome">
<interceptor-ref name="myStack"></interceptor-ref><!-- 针对特定的action的拦截器这样实现-->
<result>/WEB-INF/welcome.jsp</result>
</action>
</package>
</struts>
结果:
输入用户名和密码正常登录,要、若直接访问welcome.action
直接访问跳转到index.jsp页面,说明拦截器起了作用。
拦截器有缺陷:不能够直接拦截.jsp文件。即web-root下的文件可以直接访问。
解决:把文件放到web-inf文件夹下。