拦截器的概念是servlet过滤器或jdk代理类一样的。拦截器允许横切功能分开实现的动作,以及框架。使用拦截器,可以实现如下:
提供预处理行动之前被称为逻辑。
提供后处理逻辑动作后被调用
捕获异常,这样可以进行替代处理。
struts2框架提供的许多功能都使用拦截实现的例子包括异常处理,文件上传,生命周期回调和验证等事实上作为struts2的基础,其功能拦截,这可能有7或8拦截器分配给每个动作。
struts 2框架提供了良好的箱拦截列表来预先设定的,并准备使用。下面列出了几个重要的拦截:
sn | 拦截器 & 描述 |
---|---|
1 |
alias allows parameters to have different name aliases across requests. |
2 |
checkbox assists in managing check boxes by adding a parameter value of false for check boxes that are not checked. |
3 |
conversionerror places error information from converting strings to parameter types into the action's field errors. |
4 |
createsession automatically creates an http session if one does not already exist. |
5 |
debugging provides several different debugging screens to the developer. |
6 |
execandwait sends the user to an intermediary waiting page while the action executes in the background. |
7 |
exception maps exceptions that are thrown from an action to a result, allowing automatic exception handling via redirection. |
8 |
fileupload facilitates easy file uploading. |
9 |
i18n keeps track of the selected locale during a user's session. |
10 |
logger provides simple logging by outputting the name of the action being executed. |
11 |
params sets the request parameters on the action. |
12 |
prepare this is typically used to do pre-processing work, such as setup database connections. |
13 |
profile allows simple profiling information to be logged for actions. |
14 |
scope stores and retrieves the action's state in the session or application scope. |
15 |
servletconfig provides the action with access to various servlet-based information. |
16 |
timer provides simple profiling information in the form of how long the action takes to execute. |
17 |
token checks the action for a valid token to prevent duplicate formsubmission. |
18 |
validation provides validation support for actions |
请看struts 2文档的完整细节上面提到的拦截。会告诉如何使用struts应用程序在一个拦截器。
让我们来看看如何使用已有的拦截,我们的“hello world”程序。我们将使用计时器来测量过了多长时间执行操作方法,其目的是拦截。同时使用params拦截器,其目的是发送请求参数的动作。您可以尝试不使用这个拦截您的示例中会发现,没有被设置name属性,因为参数是无法达到动作。
我们将继续helloworldaction.java,web.xml 的helloworld.jsp 和 index.jsp 文件,因为他们已经建立了范例章节,但让我们如下修改struts.xml文件,添加一个拦截器
<?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> <constant name="struts.devmode" value="true" /> <package name="helloworld" extends="struts-default"> <action name="hello" class="com.h3.struts2.helloworldaction" method="execute"> <interceptor-ref name="params"/> <interceptor-ref name="timer" /> <result name="success">/helloworld.jsp</result> </action> </package> </struts>
右键点击项目名称,并单击 export > war file 创建一个war文件。然后部署在tomcat 的webapps目录下这个war。最后,启动tomcat服务器和尝试访问url http://localhost:8080/helloworldstruts2/index.jsp。这会给你以下画面:
现在,在给定的文本框中输入单词,并单击“say hello按钮执行已定义的动作。现在,如果将检查生成的日志,会发现下面的文字:
info: server startup in 3539 ms 27/08/2011 8:40:53 pm com.opensymphony.xwork2.util.logging.commons.commonslogger info info: executed action [//hello!execute] took 109 ms.
这里底行,正在生成因为这告诉动作发生要执行的总共为 109ms定时器的拦截器。
在应用程序中使用自定义的拦截器是一种优雅的方式提供横切的应用功能。创建一个自定义拦截器是很容易的,需要扩展的接口,下面的interceptor接口:
public interface interceptor extends serializable{ void destroy(); void init(); string intercept(actioninvocation invocation) throws exception; }
正如其名称所表明的,init()方法提供了一种方法来初始化拦截器,并destroy() 方法提供了一种工具拦截清理。不同的行动,拦截被重用跨请求和需要是线程安全的,尤其是intercept() 方法。
actioninvocation对象可以访问运行时环境。它允许访问的动作本身和方法调用的动作,并确定动作是否已被调用。
如果不需要初始化或清除代码,可以扩展abstractinterceptor类。这提供了一个默认的无操作实现的init()和 destroy()方法。
让我们创建java资源 myinterceptor.java> src 文件夹:
package com.h3.struts2; import java.util.*; import com.opensymphony.xwork2.actioninvocation; import com.opensymphony.xwork2.interceptor.abstractinterceptor; public class myinterceptor extends abstractinterceptor { public string intercept(actioninvocation invocation)throws exception{ /* let us do some pre-processing */ string output = "pre-processing"; system.out.println(output); /* let us call action or next interceptor */ string result = invocation.invoke(); /* let us do some post-processing */ output = "post-processing"; system.out.println(output); return result; } }
就像看到的,实际行动将使用拦截器执行invocation.invoke()调用。所以,可以做一些前处理和一些处理后,根据需要。
该框架本身启动的过程中,在第一次调用actioninvocation对象的invoke()。每次 invoke()被调用,actioninvocation的咨询的状态和执行为准拦截接下来。通过请求流以下数据图显示了相同的概念:
让我们创建一个java文件helloworldaction.java的java下java resources > src下面给出的内容包名为 com.h3.struts2。
package com.h3.struts2; import com.opensymphony.xwork2.actionsupport; public class helloworldaction extends actionsupport{ private string name; public string execute() throws exception { system.out.println("inside action...."); return "success"; } public string getname() { return name; } public void setname(string name) { this.name = name; } }
在前面的例子中,我们已经看到,这是一个相同的类。我们有标准的“名称”属性的getter和setter方法,并返回字符串“success”的执行方法。
让我们创建以下jsp文件helloworld.jsp,在eclipse项目在webcontent文件夹。
<%@ page contenttype="text/html; charset=utf-8" %> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>hello world</title> </head> <body> hello world, <s:property value="name"/> </body> </html>
我们还需要在webcontent文件夹中创建 index.jsp。该文件将作为初始动作url,用户可以在其中点击告诉struts 2框架调用 helloworldaction类定义的方法呈现 helloworld.jsp视图。
<%@ page language="java" contenttype="text/html; charset=iso-8859-1" pageencoding="iso-8859-1"%> <%@ 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> <title>hello world</title> </head> <body> <h1>hello world from struts2</h1> <form action="hello"> <label for="name">please enter your name</label><br/> <input type="text" name="name"/> <input type="submit" value="say hello"/> </form> </body> </html>
hello 动作定义在上面的视图文件将被映射到helloworldaction类和其执行方法使用struts.xml文件。
现在,我们需要注册我们的拦截器,然后调用它默认的拦截器在前面的例子中调用。要注册一个新定义的拦截,直接放在的<interceptors>...</interceptors>标签下<package>的标签插件struts.xml文件。您可以跳过这一步为默认的拦截器,就像我们在我们前面的例子。但在这里,让我们注册和使用它,如下所示:
<?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> <constant name="struts.devmode" value="true" /> <package name="helloworld" extends="struts-default"> <interceptors> <interceptor name="myinterceptor" class="com.h3.struts2.myinterceptor" /> </interceptors> <action name="hello" class="com.h3.struts2.helloworldaction" method="execute"> <interceptor-ref name="params"/> <interceptor-ref name="myinterceptor" /> <result name="success">/helloworld.jsp</result> </action> </package> </struts>
应该指出的是,可以注册多个拦截器<package>标签内,同一时间,可以调用多个拦截里面的<action>标签。可以调用相同的拦截器与不同的动作。
web.xml文件需要在 web-inf文件夹下创建 webcontent 如下:
<?xml version="1.0" encoding="utf-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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"> <display-name>struts 2</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.filterdispatcher </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
右键点击项目名称,并单击 export > war file 文件创建一个war文件。然后部署在tomcat 的webapps目录下这个war。最后,启动tomcat 服务器和尝试访问url http://localhost:8080/helloworldstruts2/index.jsp。这会给你以下画面:
现在,在给定的文本框中输入任何单词,并单击“say hello“ 按钮执行已定义的动作。现在,如果检查生成的日志,会发现下面的文本下方:
pre-processing inside action.... post-processing
可以想像,配置多个拦截器每个动作很快就会变得非常难以控制。出于这个原因,拦截器与拦截器栈管理。下面是一个例子,直接从在struts-default.xml文件:
<interceptor-stack name="basicstack"> <interceptor-ref name="exception"/> <interceptor-ref name="servlet-config"/> <interceptor-ref name="prepare"/> <interceptor-ref name="checkbox"/> <interceptor-ref name="params"/> <interceptor-ref name="conversionerror"/> </interceptor-stack>
上面的栈被调用basicstack,可用于在配置中,如下所示。此配置节点放置在<package.../>节点下。每个<interceptor-ref.../>标记引用一个拦截器或拦截器栈已配置在当前的拦截器栈。因此,这是非常重要的,以确保该名称是唯一的所有拦截器和拦截器栈配置配置初始的拦截器和拦截器栈时。
我们已经看到了如何应用拦截的动作,将拦截器栈是没有什么不同。事实上,我们完全使用相同的标签:
<action name="hello" class="com.h3.struts2.myaction"> <interceptor-ref name="basicstack"/> <result>view.jsp</result> </action
上述注册的“basicstack”所有6个拦截器完成注册的栈 hello 动作。应该指出的是,拦截器执行的顺序在配置中。例如,在上述情况下,异常将被执行,servlet 配置等。