以下是关于struts2中用户自定义Interceptor说明:
配置一: web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
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_2_5.xsd">
<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>*.action</url-pattern>
</filter-mapping>
</web-app>
----注: 此拦截器并没在web.xml中进行配置。
配置二: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>
<package name="com" extends="struts-default" namespace="/">
<!--定义用户自定义拦截器 -->
<interceptors>
<interceptor name="userLonginIntercepter" class="com.util.Intercepter.userLonginIntercepter">
<!--可配置参数值 -->
<param name="USER_NAME"></param>
<param name="USER_PASS"></param>
</interceptor>
<!--拦截器栈明 -->
<interceptor-stack name="defaultStack">
<!--这里可参考struts2底层源码配置信息,栈堆区引用-->
<interceptor-ref name="exception"></interceptor-ref>
<interceptor-ref name="params"></interceptor-ref>
</interceptor-stack>
</interceptors>
<!-- 用户登陆Action -->
<action name="userAction" class="com.info.action.userAction" method="userLogin">
<!--引用用户自定义Intercepter -->
<interceptor-ref name="userLonginIntercepter"></interceptor-ref>
<result name="success">/succeess.jsp</result>
<result name="faile">/faile.jsp</result>
<result name="login">/index.jsp</result>
</action>
</package>
</struts>
代码1:userAction.java
package com.info.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import com.opensymphony.xwork2.ActionSupport;
import com.util.Intercepter.userLonginIntercepter;
public class userAction extends ActionSupport implements ServletResponseAware, ServletRequestAware {
/**
*
*/
private static final long serialVersionUID = 1L;
private HttpServletRequest request;
private HttpServletResponse response;
public void setServletResponse(HttpServletResponse response) {
this.response=response;
}
public void setServletRequest(HttpServletRequest request) {
this.request=request;
}
/***
* 用户登陆Action
* @return
* @throws Exception
*/
public String userLogin() throws Exception
{
HttpServletRequest request=ServletActionContext.getRequest();
String UserName=request.getParameter("userName");
String UserPass=request.getParameter("userPass");
String returnValue=userLonginIntercepter.getUserInfo(UserName, UserPass);
return returnValue;
}
}
代码2:userLonginIntercepter.java
package com.util.Intercepter;
import com.info.action.userAction;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class userLonginIntercepter extends AbstractInterceptor {
/**
* 用户自定义拦截器
*/
private static final long serialVersionUID = 1L;
public static final String USER_NAME="AAAA";
public static final String USER_PASS="111";
@Override
public String intercept(ActionInvocation actionInvocation) throws Exception {
/***
* 拦截操作....
*/
System.out.println("------------------:");
System.out.println("------------------:");
System.out.println("------------------:");
System.out.println("------------------:");
System.out.println("------------------:");
/**获取到Action的代理对象**/
Object action=actionInvocation.getAction();
<!--判断获取的action实例是userAction实例, 让Interceptor过滤器失效,继续执行Action请求代码-->
if(action instanceof userAction){
return actionInvocation.invoke();
}
return null;
}
public static String getUserInfo(String UserName, String UserPass){
if(UserName.equals(USER_NAME) && UserPass.equals(USER_PASS))
{
return "success";
}
return "faile";
}
}
代码3:前端页面说明
Index.jsp如下:
<body>
<form action="<%=path %>/userAction.action" name="loginFrom" method="post">
<table cellpadding="0" cellspacing="0" style="line-height: 25px;">
<tr>
<td>用户名:</td>
<td><input type="text" name="userName"></td>
</tr>
<tr>
<td>密 码:</td>
<td><input type="password" name="userPass"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="提交"> </td>
</tr>
</table>
</form>
</body>
faile.jsp如下:
<body>失败!</body>
succeess.jsp如下:
<body>成功!</body>
说明信息:
(1)以上小案例说明对Action对象进行Interceptor过滤作用,(同理在此也可以对业务层进行过滤)。
(2)理解原理:当web服务部署应用程序后,初始化web.xml,并读取struts.xml配置信息。如下在浏览器中输入。http://localhost:8086/Struts2_Interceptor/index.jsp,页面端响应在客户并呈现出。当用户输入信息,点击提交。此时HttpServletRequest 请求对象。根据请求路径,在struts.xml首先就会查找到userLonginIntercepter配置。根据配置处理信息。当userLonginIntercepter失效后,继续获取原来请求进行action处理。然返回页面端。(此案例不到之处,请多多指教)。
java Struts2_用户自定义拦截器
于 2016-08-02 10:02:29 首次发布