5, Interceptors
Interceptors allows code that is executed after the calling stack is included before and / or processing before processing. This is your code simple, more reuse. Most of XWork and WebWork is implemented in Interceptors. You can apply your own interceptors to the specified action by external configuration, according to your defined order. Apply your own Interceptors.
When you visit .Artion URL, WebWork's servletdispatcher boots an action object, starting to be interrupted by other objects before Action is executed, which is called Interceptor. Execute Interceptor before the specified action (or later), as long as the properties are configured in xwork.xml. Here are an Interceptor configuration that shows an example of the UI tag usage in Section 4.1.1:
action>
FormProcessingAction uses ValidationWorkflowstack. This is an interceptor heap that organizes a set of interceptors in order. ValidationWorkflowStack is configured in WebWork-Default.xml, so we just use
"http://www.opensymphony.com/xwork/xwork-1.0.dtd">
action>
package>
xwork>
Look at how Interceptor works
l Create an interceptor class, you need to extend the com.OpenSymphony.xWork.Interceptor.Interceptor interface (included in xwork-1.0.jar);
l In the xwork.xml file, use the
l Use
l Which anterceptor is used by
(1) Webwork-default.xml
Let's take a look at the contents of web-default.xml:
"http://www.opensymphony.com/xwork/xwork-1.0.dtd">
Class = "com.opensymphony.webwork.dispatcher.servletdispatcherResult" /> Class = "com.opensymphony.webwork.dispatcher.servletredirectResult" /> Class = "com.opensymphony.webwork.dispatcher.velocityResult" /> Class = "com.opensymphony.xwork.actionchainResult" /> Class = "com.opensymphony.webwork.views.xslt.xsltResult" /> result-types> Class = "com.opensymphony.xwork.interceptor.timinterceptor" /> Class = "com.opensymphony.xwork.interceptor.logginginterceptor" /> Class = "com.opensymphony.xwork.interceptor" /> Class = "com.opensymphony.xwork.interceptor.StaticParametersInterceptor" /> Class = "com.opensymphony.xwork.interceptor" /> Class = "com.opensymphony.xwork.interceptor.modeldriveninterceptor" /> Class = "com.opensymphony.xwork.interceptor.component.componentinterceptor" /> Class = "com.opensymphony.webwork.interceptor.tokeninterceptor" /> Class = "com.opensymphony.webwork.interceptor.tokenssionStoreInterceptor" /> Class = "com.opensymphony.xwork.validator.validationinterceptor" /> Class = "com.opensymphony.xwork.interceptor.defaultWorkflowInterceptor" /> Class = "com.opensymphony.webwork.interceptor.servletconfiginterceptor" /> Class = "com.opensymphony.xwork.interceptor.prepareinterceptor" /> Class = "com.opensymphony.webwork.interceptor.webworkconversionerrorinterceptor" /> interceptor-stack> interceptor-stack> interceptors> package> xwork> Since WebWork-Default.xml is included in our xwork.xml, we can use these Interceptor or Interceptor piles in Action. Below is what these Interceptors do: l Timer: Perform timing (including Nested Interceptor and view) l Chain: Make the properties of the previous Action valid for the current action, usually create an Action chain l Static-Params: Setting parameters in xwork.xml to action ( embedded in l Params: Settings the request parameter (POST or GET) to the action l Model-Driven: If the action realizes ModelDriven, push getModel () results into the Value Stack l Component: Enable and register components to make it valid for Action l Token: Check the effective token in action to prevent repeated submission l Token-session: Same as above, but saves submitted data in the session when processed to invalid token l Validation: Use the validator defined in {Action} -vailDation.xml for data verification l Workflow: Call the validate () method in the Action class, return to the INPUT view when an error occurs; should be used with the Validation Interceptor l Servlet-Config: Access to HTTPSERVLETREQUEST and HTTPSERVLETRESPONSE (Because Bind to Servlet API, it is best not to use) l prepare l conversionerror (2) Creating your own interceptor If the above Interceptor is not suitable, you can create your own interceptor. The following example assumes that we need an interceptor to place a welcome information based on the day of the SESSION: GreetingInterceptor.java: Package lesson05; Import java.util.calendar; Import com.opensymphony.xwork.interceptor.interceptor; Import com.opensymphony.xwork.actioninvocation; Public class greetinginterceptor imports interceptor { Public void init () {} Public void destroy () {} Public String Intercept (ActionInvocation Invocation) throws exception { Calendar Calendar = Calendar.getInstance (); INT Hour = Calendar.get (Calendar.Hour_Of_DAY); String Greeting = (Hour <6)? "Good evening": ((Hour <12)? "Good Morning": ((Hour <18)? "good insternoon": "good evening")); Invocation.getinvocationContext (). GetSession (). PUT ("Greeting", Greeting; String result = invocation.invoke (); Return Result; } } Xwork.xml:
"http://www.opensymphony.com/xwork/xwork-1.0.dtd"> interceptors> action> package> xwork> GreetingAction.java: Package lesson05; Import com.opensymphony.xwork.actionsupport; Public class greetingaction extends actionSupport { Public String Execute () throws exception { RETURN SUCCESS; } } EX01-Result.vm: hEAD> #set ($ SES = $ Req.GetSession ()) $ {ses.getattribute ('Greeting')}! b> p> body> html> Interceptor class To extend the com.opensymphony.xwork.interceptor.interceptor interface: init () calls when Interceptor initialization; Destroy () calls when destroying; Intercept (ActionInvocation Invocation) is the center of processing. Invocation.Invoke () is used to call the next interceptor in the interceptor pile, or Action (if not, if not). Therefore, we can completely bypass the Action and return the result (do not perform an action). The above example is called before the action is executed. If you want to call Interceptor after the Action is executed, simply put the execution code in Invocation.invoke (). Webwork provides an abstract class com.opensymphony.xWork.Interceptor.AroundInterceptor that implements this method, you can implement its Before (ActionInvocation Invocation) and After (ActionInvocation Dispatcher, String Result).