Struts2 in action读书笔记(3)

xiaoxiao2026-08-04  3

[b]第四章 Adding workflow with interceptor(上)[/b] 1. Interceptor接口的三个方法: void destroy();void init();String intercept(ActionInvocation invocation) throws Exception; 抽象类AbstractInterceptor实现了Interceptor接口 abstract class AbstractInterceptor implements Interceptor 2. timer拦截器,用于记录执行的时间 3. logger拦截器,用于在执行前和执行后打印出信息 4. params拦截器(defaultStack),通过ValueStack将request parameters保存到属性中 5. staticParams拦截器(defaultStack),将配置文件中设置的参数通过ValueStack保存到属性中,在defaultStack中位于params拦截器前面,因此request parameters会覆盖配置文件中的parameters。 如下所示: <action name="exampleAction" class="example.ExampleAction"> <param name="firstName">John</param> <param name="lastName">Doe</param></action> 6. servlet-config拦截器(defaultStack),用于将各种Servlet API注入到action中, ServletContextAware—Sets the ServletContext ServletRequestAware—Sets the HttpServletRequest ServletResponseAware—Sets the HttpServletResponse ParameterAware—Sets a map of the request parameters RequestAware—Sets a map of the request attributes SessionAware—Sets a map of the session attributes ApplicationAware—Sets a map of application scope properties PrincipalAware—Sets the Principal object (security) 只要action继承了这些接口,实现相应的Setter方法,都可以实现注入 7. fileupload拦截器(defaultStack),将文件的三个属性,File,ContentType,Filename保存到request parameters中去,使得文件的这些属性可以像基本的request parameters那样保存到action或者域模型中去,因此这个拦截器在defualtStack中位于staticParams和params拦截器之前 8. workflow拦截器(defaultStack),用于做数据校验,DefaultWorkflowInterceptor继承MethodFilterInterceptor,MethodFilterInterceptor类用doIntercept()方法封装了intercept(),主要是添加了一个方法过滤。 Action只要实现了Validateable接口,都可以通过validate()方法进行数据校验(如何校验,用户自己实现) 实现了ValidationAware接口,可以通过判断ValidationAwareSupport中是否有错误内容而进行重定向到INPUT,(ValidationAwareSupport实现了ValidationAware接口,主要保存actionErrors、actionMessages、fieldErrors等这些错误信息,ActionSupport中有该类的一个实例) workflow拦截器在defualtStack中位于最后 workflow拦截器的几个参数 alwaysInvokeValidate (true or false; defaults to true, which means that validate() will be invoked) inputResultName (name of the result to choose if validation fails; defaults to Action.INPUT) excludeMethods (names of methods for which the workflow interceptor shouldn’t execute, thereby omitting validation checking for a specific entry point method on an action) 9. validation拦截器(defaultStack),区别于workflow校验器,主要是在XML或者Java annotations中定义校验规则(AnnotationValidationInterceptor继承ValidationInterceptor) validation拦截器在defualtStack中位于workflow拦截器之前 10. prepare拦截器(defaultStack),需要action实现Preparable接口,如果加载prepare拦截器,prepare()方法会在execute()方法调用前被调用(无论execute()方法是否被invoke) Action可以除了execute(),还可以有其他的方法,例如input(),update(),则prepare可以采用 prepareInput()或prepareDoInput()、prepareupdate()或prepareDoUpdate()方法来对应action的执行方法(采用prepare,prepareDo作为前缀) prepare拦截器的参数 alwaysInvokePrepare - Default to true.设置为false可以关闭prepare()方法。 11. modelDriven拦截器(defaultStack),将Model放入ValueStack中 modelDriven拦截器位于prepare拦截器之后params拦截器之前 12. exception拦截器(defaultStack),在defaultStack中位于第一位,捕获异常定位到用户定义的错误页面 例如在struts.xml配置文件中定义全局的result映射处理异常情况 <package name="chapterFourPublic" namespace="/chapterFour" extends="struts-default">…… <!--package中的全局result映射 --> <global-results> <result name="error">/chapterFour/Error.jsp</result> </global-results> <global-exception-mappings> <exception-mapping exception="java.lang.Exception" result="error" /> </global-exception-mappings>……</package> 然后在error.jsp中的内容如下,用户显示异常 <p><h4>Exception Name: </h4><s:property value="exception" /></p> <p><h4>What you did wrong:</h4> <s:property value="exceptionStack" /></p> 13. token&tokenSession拦截器,用于防止重复提交 14. scopedModelDriven拦截器(defaultStack), 15. execAndWait拦截器,长时间执行请求,给用户一个feedback
转载请注明原文地址: https://www.6miu.com/read-5050702.html

最新回复(0)