Struts2自定义拦截器的基本方法

struts.xml

  struts2配置文件,因为我们为Action配置了拦截器,默认的拦截器就会失效。为了程序的正常运行,需要我们显示引入默认拦截器。

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
 3 <struts>
 4     <package name="lee" extends="struts-default" namespace="/">
 5            <interceptors >
 6             <interceptor name="log"  class="Interceptor.Myinterceptor"></interceptor>
 7         </interceptors> 
 8         <action name="Login" class="Action.LoginAction" >
 9             <result name="input">index.jsp</result>    
10             <result name="success">WEB-INF/success.jsp</result>
11              <interceptor-ref name="log"/> 
12              <!-- 我们为一个Action配置了拦截器时,默认的拦截器就会失效,
13              但是Struts2本身的一些功能,比如说参数自动赋值又是依赖配置的默认拦截器实现,
14              所有应用程序会出错。这时需要我们手动将默认的拦截器引用进来,再为这项Action加入默认拦截器 -->
15              <interceptor-ref name="defaultStack"/>
16         </action>
17 
18     </package>
19 
20 </struts>    
View Code

Myinterceptor.java

  自定义的拦截器,继承了AbstractInterceptor类,并重写intercept方法

 1 package Interceptor;
 2 
 3 import Action.LoginAction;
 4 
 5 import com.opensymphony.xwork2.ActionInvocation;
 6 import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
 7 
 8 public class Myinterceptor extends AbstractInterceptor {
 9 
10     /**
11      * 
12      */
13     private static final long serialVersionUID = 1L;
14 
15 
16 
17     @Override
18     public String intercept(ActionInvocation invocation) throws Exception {
19         // TODO Auto-generated method stub
20         LoginAction action=(LoginAction) invocation.getAction();
21         System.out.println("########interceptor#########");
22         String result=invocation.invoke();
23         System.out.println(result);
24 
25         return result;
26     }
27 
28 }
View Code

LoginAction.java

  struts2的action,继承ActionSupport类

 1 package Action;
 2 
 3 import com.opensymphony.xwork2.ActionSupport;
 4 
 5 public class LoginAction extends ActionSupport {
 6 
 7     /**
 8      * 
 9      */
10     private static final long serialVersionUID = 1L;
11     private String userName;
12     private String passwd;
13 
14     public String getUserName() {
15         return userName;
16     }
17 
18     public void setUserName(String userName) {
19         this.userName = userName;
20     }
21 
22     public String getPasswd() {
23         return passwd;
24     }
25 
26     public void setPasswd(String passwd) {
27         this.passwd = passwd;
28     }
29 
30     @Override
31     public String execute() throws Exception {
32         // TODO Auto-generated method stub
33         System.out.println("#####LoginAction########");
34         System.out.println(this.getUserName());
35         if (this.userName.equals("frank"))
36             return SUCCESS;
37         return INPUT;
38     }
39 
40 }
View Code

index.jsp

简单的登录界面

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11     
12     <title>My JSP 'index.jsp' starting page</title>
13     <meta http-equiv="pragma" content="no-cache">
14     <meta http-equiv="cache-control" content="no-cache">
15     <meta http-equiv="expires" content="0">    
16     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
17     <meta http-equiv="description" content="This is my page">
18     <!--
19     <link rel="stylesheet" type="text/css" href="styles.css">
20     -->
21   </head>
22   
23   <body>
24         <form action="Login" method="post">
25             <input type="text" name="userName" ><br>
26             <input type="password" name="passwd"><br>
27             <input type="submit" value="submit">
28         </form>
29   </body>
30 </html>
View Code

web.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
 3   <display-name>interceptorTest</display-name>
 4   <welcome-file-list>
 5     <welcome-file>index.html</welcome-file>
 6     <welcome-file>index.htm</welcome-file>
 7     <welcome-file>index.jsp</welcome-file>
 8     <welcome-file>default.html</welcome-file>
 9     <welcome-file>default.htm</welcome-file>
10     <welcome-file>default.jsp</welcome-file>
11   </welcome-file-list>
12   <filter>
13     <filter-name>struts2</filter-name>
14     <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
15   </filter>
16   <filter-mapping>
17     <filter-name>struts2</filter-name>
18     <url-pattern>/*</url-pattern>
19   </filter-mapping>
20 </web-app>
View Code
原文地址:https://www.cnblogs.com/frankM/p/4438958.html