layout: post
title: struts2实验5:struts2 拦截器
categories: STRUTS2
description: struts2实验5:struts2 拦截器
keywords: STRUTS2
实验思路
- 编写Interceptor 继承 AbstractInterceptor类
- 重写public String intercept(ActionInvocation invocation)方法
- 编写struts2.xml
关键struts2.xml 代码
<interceptors>
<interceptor name="MyInterceptor" class="com.zy.javaee.interceptor.MyInterceptor">
<!--指定当前方法拦截器对象要进行拦截的方法对象-->
<!--<param name="includeMethods">doFirst,doSecond</param>-->
<!-- 指定对doThird方法不进行过滤操作 -->
<!--<param name="excludeMethods">doThird</param> -->
<param name="myInterceptorParam">my first interceptor</param>
</interceptor>
</interceptors>
<interceptor-ref name="MyInterceptor">
<param name="defaultUsername">testuser</param>
<param name="defaultPassword">123</param>
<param name="myInterceptorParam">my changed interceptor</param>
</interceptor-ref>
目录结构
实验结果
代码(关键代码,文末有实验5github链接)
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" />
<constant name="struts.i18n.encoding" value="GBK"/>
<constant name="struts.action.extension" value="do,action,"/>
<constant name="struts.serve.static.browserCache" value="false"/>
<constant name="struts.configuration.xml.reload" value="true"/>
<!-- ################################ 国际化重点 -Start- ################################-->
<constant name="struts.custom.i18n.resources" value="mess"/>
<!-- ################################ 国际化重点 -End- ################################-->
<package name="struts-global" namespace="/" extends="struts-default">
<interceptors>
<interceptor name="MyInterceptor" class="com.zy.javaee.interceptor.MyInterceptor">
<!--指定当前方法拦截器对象要进行拦截的方法对象-->
<!--<param name="includeMethods">doFirst,doSecond</param>-->
<!-- 指定对doThird方法不进行过滤操作 -->
<!--<param name="excludeMethods">doThird</param> -->
<param name="myInterceptorParam">my first interceptor</param>
</interceptor>
</interceptors>
<global-results>
<result name="exception" >/error.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping result="exception" exception="java.lang.Exception">
</exception-mapping>
</global-exception-mappings>
<action name="tryException" class="com.zy.javaee.controller.actions.ErrorProcessAction" method="tryException">
<result name="success">/users/login_userLoginPage.do</result>
</action>
</package>
<package name="default" namespace="/" extends="struts-global">
<action name="login">
<result type="redirect">/users/login_userLoginPage</result>
</action>
</package>
<package name="admin" namespace="/admin" extends="struts-global">
<action name="login_*" class="com.zy.javaee.controller.actions.AdminLoginAction" method="{1}">
<result name="success">${directUrl}</result>
<param name="defaultAdmin.username">testadmin</param>
<param name="defaultAdmin.password">123</param>
</action>
</package>
<package name="users" namespace="/users" extends="struts-global">
<action name="login_*" class="com.zy.javaee.controller.actions.UserLoginAction" method="{1}">
<interceptor-ref name="MyInterceptor">
<param name="defaultUsername">testuser</param>
<param name="defaultPassword">123</param>
<param name="myInterceptorParam">my changed interceptor</param>
</interceptor-ref>
<result name="success">${directUrl}</result>
</action>
</package>
</struts>
MyInterceptor.java
package com.zy.javaee.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
import com.zy.javaee.controller.actions.UserLoginAction;
import com.zy.javaee.domin.User;
import java.util.Date;
import java.util.Map;
/**
* Created by Joey_Tsai on 2018/4/11.
*/
public class MyInterceptor extends AbstractInterceptor {
private String myInterceptorParam;
private String defaultUsername;
public String getDefaultPassword() {
return defaultPassword;
}
public void setDefaultPassword(String defaultPassword) {
this.defaultPassword = defaultPassword;
}
private String defaultPassword;
public String getDefaultUsername() {
return defaultUsername;
}
public void setDefaultUsername(String defaultUsername) {
this.defaultUsername = defaultUsername;
}
// private String defaultPassword;
public String getMyInterceptorParam() {
return myInterceptorParam;
}
public void setMyInterceptorParam(String myInterceptorParam) {
this.myInterceptorParam = myInterceptorParam;
}
//省略getter和setter
@Override
public String intercept(ActionInvocation invocation) throws Exception {
// TODO Auto-generated method stub
Map paramMap = invocation.getInvocationContext().getParameters();
String[] username = (String[]) paramMap.get("user.username");
String[] password = (String[]) paramMap.get("user.password");
String result;
if (username != null && password != null){
String[] usernameParam = username[0].split(",");
String[] passwordParam = password[0].split(",");
User user = new User();
user.setUsername(usernameParam[0]);
user.setPassword(passwordParam[0]);
User defaultUser = new User();
defaultUser.setUsername(defaultUsername);
defaultUser.setPassword(defaultPassword);
UserLoginAction userLoginAction = (UserLoginAction) invocation.getAction();
userLoginAction.setUser(user);
userLoginAction.setDefaultUser(defaultUser);
System.out.println(myInterceptorParam + " 拦截器---开始执行登录Action的时间为" + new Date());
long start = System.currentTimeMillis();
result = invocation.invoke();
System.out.println(myInterceptorParam + " 拦截器---执行完登录Action的时间为" + new Date());
long end = System.currentTimeMillis();
System.out.println(myInterceptorParam + " 拦截器---执行Action的时间为" + (end - start) + "ms");
}
else result = invocation.invoke();
return result;
}
}