Professional Documents
Culture Documents
Struts 2 Interceptors
Struts 2 Interceptors
www.JAVA9
By, Srinivas
Interceptors
Separates the cross cutting concerns Pre Processing and Post processing. Manages the work flow.
www.JAVA9
www.JAVA9
Interceptor By Example
A Login page
www.JAVA9
Interceptor by Example
Redisplay the login page for wrong credentials
Access the Authorized page directly with out logging should forward the login page
www.JAVA9
Creating an Interceptor
The interceptor class should implement the Interceptor interface. Implement the intercept method. Do the pre processing and if everything is ok, invoke the invocation.invoke() which will call the next component in queue. If something goes wrong, return a string based on which a result page will be picked and sent to the client. After the invocation.invoke(), write the post processing logic
Interceptors in defaultstack
<interceptor-stack name="defaultStack"> <interceptor-ref name="exception"/> Handles the result based on the exception. <interceptor-ref name="alias"/> Enables to have a different named parameter <interceptor-ref name="servletConfig"/> Will set the request, response, session objects to the action class. <interceptor-ref name="prepare"/> This interceptor callsprepare()on actions which implement Preperable <interceptor-ref name="i18n"/>sets the locale specified in a session as the locale for the current action request <interceptor-ref name="chain"/> Helps to chain the actions. <interceptor-ref name="debugging"/>Provides several different debugging screens to provide insight into the data behind the page <interceptor-ref name="profiling"/>Allows profiling to be enabled or disabled via request parameters, when devMode is enabled.
www.JAVA9
Interceptors in defaultstack
<interceptor-ref name="scopedModelDriven"/>An interceptor that enables scoped model-driven actions. <interceptor-ref name="modelDriven"/>Watches for ModelDriven actions and adds the action's model on to the value stack <interceptor-ref name="fileUpload"/>Helps in file upload. <interceptor-ref name="checkbox"/>Looks for a hidden identification field that specifies the original value of the checkbox. <interceptor-ref name="staticParams"/>This interceptor populates the action with the static parameters defined in the action configuration. <interceptor-ref name="params">This interceptor sets all parameters on the value stack <param name="excludeParams">dojo\..*</param> </interceptor-ref>
www.JAVA9
Interceptors in defaultstack
<interceptor-ref name="conversionError"/> ConversionErrorInterceptor adds conversion errors from the ActionContext to the Action's field errors. <interceptor-ref name="validation">Validates the parameters <param name="excludeMethods">input,back,cancel,browse</param> </interceptor-ref> <interceptor-ref name="workflow"> An interceptor that does some basic validation workflow before allowing the interceptor chain to continue <param name="excludeMethods">input,back,cancel,browse</param> </interceptor-ref> </interceptor-stack>
www.JAVA9
Thank you
Download PPT and example code from
http://java9s.com