struts2自定义拦截器(interceptor)

在AOP(Aspect-Oriented Programming)中用于在某个方法或字段被访问之前,进行拦截然后在之前或之后加入某些操作。
拦截是AOP的一种实现策略。
在Webwork的中文文档的解释为——拦截器是动态拦截Action调用的对象。它提供了一种机制可以使开发者可以定义在一个action执行的前后执行的代码,也可以在一个action执行前阻止其执行。同时也是提供了一种可以提取action中可重用的部分的方式。
谈到拦截器,还有一个词大家应该知道——拦截器链(Interceptor Chain,在Struts 2中称为拦截器栈Interceptor Stack)。拦截器链就是将拦截器按一定的顺序联结成一条链。在访问被拦截的方法或字段时,拦截器链中的拦截器就会按其之前定义的顺序被调用。
拦截器的作用
Interceptor(以下译为拦截器)是Struts 2的一个强有力的工具,有许多功能(feature)都是构建于它之上,如国际化转换器校验等。
已有的拦截器
Struts 2已经为您提供丰富多样的,功能齐全的拦截器实现。大家可以到struts2-all-2.0.1.jar或struts2-core-2.0.1.jar包的struts-default.xml查看关于默认的拦截器与拦截器链的配置。
自定义拦截器demo
1.创建一个注册页面(index.jsp)注册一下
Java代码  
<body>
         这是个注册页面
    <%session.setAttribute("user", "xia"); %>
  </body>
2.在登录界面(login.jsp)登录:
Java代码  
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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 'login.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
</head>
<body>
	<form action="login.action" method="post">
		<table align="center">
			<tr>
				<td>用户名:</td>
				<td><input type="text" name="userName" /></td>
			</tr>
			<tr>
				<td>密  码:</td>
				<td><input type="password" name="password" /></td>
			</tr>
			<tr align="center">
				<td colspan="2"><input type="submit" value="登录" /></td>
			</tr>
		</table>
	</form>
</body>
</html>
3.登录类:LoginAction.java
Java代码 
package com.ncl.action;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
private String userName;
private String password;
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;
}
@Override
public String execute(){
	System.out.println("开始执行execute()方法。。。。");
	Map<String,Object> map = ActionContext.getContext().getSession();
	String user = (String)map.get("user");
	System.out.println("this user name is-----"+user);
	return SUCCESS;	
}
}
4.success.jsp页面
Java代码  
<body>
    welecome ${userName} to struts2 world!
          登录密码是:${password}
  </body>
5.拦截器类:LoginInterceptor.java
Java代码  
package com.ncl.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 {
		//取得session
		Map<String,Object> session = invocation.getInvocationContext().getSession();
		//从session里获取登录时保存进session的user类
		String user =(String) session.get("user");
		System.out.println("在登录前进行拦截进行判断");
		System.out.println("该用户为----"+user);
		//判断用户名是否为空
		if("".equals(user) || null==user){
			//如果未通过验证则返回登录页面
			System.out.println("用户名为空请重新填写---");
			return Action.INPUT;
		}else{
		/**返回验证通过,通知Struts2接着干下面的事情,
		调用下一个拦截器或执行下一个action,
		相当于退出了自己编写的这个interceptor */
			return invocation.invoke();
		}
	}
}
6.struts2配置文件struts.xml
Java代码  
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
	<package name="struts" extends="struts-default">
		<interceptors>
			<!-- 登录权限拦截器 -->
			<interceptor name="authority" class="com.ncl.interceptor.LoginInterceptor" />
			<interceptor-stack name="loginStack">
				<interceptor-ref name="authority" />
				<interceptor-ref name="defaultStack" />
			</interceptor-stack>
		</interceptors>
		<default-interceptor-ref name="loginStack" />
		<action name="login" class="com.ncl.action.LoginAction">
			<result>/success.jsp</result>
			<result name="input" type="redirect">/login.jsp</result>
		</action>
	</package>
</struts>
 7.web.xml
Java代码  

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <display-name></display-name>	
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>
8.部署到tomcat服务器上,在地址栏上输入:http://localhost:8080/testStrutsInterceptor   来注册,
再重新输入:http://localhost:8080/testStrutsInterceptor/login.jsp
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

愚人节第二天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值