关于Struts2中的拦截器,本人是通过一个跳转的小例子来理解的,详见下方代码:
index.jsp(首页)
<%@ 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>
<form action="login.action">
<!--
用户:<input type="text" name="user.username"><br>
密码:<input type="password" name="user.password"><br>
-->
用户:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
<input type="submit" value="提交">
</form>
</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 参考 default.properties
-->
<constant name="struts.devMode" value="true"></constant>
<package name="a1" namespace="/"
extends="struts-default">
<!-- 因为可以配置多个拦截器,注意元素名是复数形式 -->
<interceptors>
<!-- 实例化对应的拦截器 -->
<interceptor name="loginCheck"
class="com.zte.interceptor.LoginInterceptor">
</interceptor>
<!-- 在拦截器栈当中添加拦截器 -->
<interceptor-stack name="check">
<interceptor-ref name="loginCheck"></interceptor-ref>
</interceptor-stack>
</interceptors>
<!-- <default-interceptor-ref name="check"></default-interceptor-ref> -->
<!-- 当前package下的所有action,
如无特殊自定义,使用共通的result -->
<global-results>
<result name="login">/index.jsp</result>
</global-results>
<action name="login" class="com.zte.LoginAction" method="execute">
<!-- 方法处理结果和视图的映射关系 -->
<result name="success" type="dispatcher">/success.jsp</result>
</action>
<action name="bookInput" class="com.zte.BookAction" >
<interceptor-ref name="loginCheck"></interceptor-ref>
<result name="success">/book.jsp</result>
</action>
</package>
</struts>
LoginAction.java(属性驱动处理请求参数)
package com.zte;
import com.opensymphony.xwork2.ActionContext;
public class LoginAction {
private String username;
private String password;
public String execute(){
System.out.println("username:" + username);
if (username != null && !"".equals(username)){
ActionContext ctx = ActionContext.getContext();
ctx.getSession().put("user", username);
}
return "success";
}
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;
}
}
success.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>
用户:${username }<br>
<a href="bookInput.action">图书信息</a>
</body>
</html>
LoginInterceptor.java(实现拦截)
package com.zte.interceptor;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
public class LoginInterceptor implements Interceptor {
@ Override
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("aaaaaaaaa");
ActionContext ctx = ActionContext.getContext();
System.out.println(ctx.getSession().get("user"));
if (ctx.getSession().get("user")== null) {
return "login";
} else {
return invocation.invoke();
}
}
@Override
public void destroy() {
// 销毁拦截器时做的工作
// 只做一次
}
@Override
public void init() {
// 初始化拦截器时要做的工作
// 只做一次
}
}
BookAction.java
package com.zte;
import com.opensymphony.xwork2.ActionContext;
public class BookAction {
public String execute(){
/*
ActionContext ctx = ActionContext.getContext();
//用户未正常登陆
if (ctx.getSession().get("user") == null){
return "login";
}
*/
System.out.println("aaaaa");
return "success";
}
}
整个流程就上述那么多,例外这是我的目录结构,方便大家查看。
有任何问题的请在下方评论留言,谢谢。