1 / 14

Struts 2 Interceptors

Struts 2 Interceptors. By, Srinivas Reddy.S. www.JAVA9S.com. Interceptors. Separates the cross cutting concerns Pre Processing and Post processing. Manages the work flow. www.JAVA9S.com. www.JAVA9S.com. Interceptor By Example. A Login page.

celina
Download Presentation

Struts 2 Interceptors

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Struts 2 Interceptors By, SrinivasReddy.S www.JAVA9S.com

  2. Interceptors • Separates the cross cutting concerns • Pre Processing and Post processing. • Manages the work flow. www.JAVA9S.com

  3. www.JAVA9S.com

  4. Interceptor By Example • A Login page • A Successful login page with a link to authorized page www.JAVA9S.com

  5. Interceptor by Example • Redisplay the login page for wrong credentials • Access the Authorized page directly with out logging should forward the login page www.JAVA9S.com

  6. 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

  7. Important Interceptors in defaultStack • servletConfig • Sets the context related objects like HttpServletRequest, HttpServletResponse, HttpSession and HttpServletContext to the action if the action class implements the ServletRequestAware, ServletResponseAware, SessionAware, ServletContextAware respectively

  8. Important Interceptors in defaultStack • modelDriven • Sets the form bean declared by the action in the valuestack. • The action should implement the ModelDriven interface.

  9. Important Interceptors in defaultStack • staticParams Helps to set the values from the struts.xml to the properties of action instead of the request parameters. • params Responsible for all the request parameters to be set to the properties of the action. • conversionError Tries to covert the request parameters to primitive data types and if there are any errors, the field errors will be set.

  10. Important Interceptors in defaultStack • Validation Checks if the validationAware is implemented by action or validates by the validation framework and sets the validation errors. • workflow Checks if there are any field errors or action errors and then decides to invoke the action or to re send the input page

  11. 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 calls prepare() 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.JAVA9S.com

  12. 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.JAVA9S.com

  13. 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.JAVA9S.com

  14. Thank you Download PPT and example code from http://java9s.com

More Related