Vous êtes sur la page 1sur 14

Struts 2 Interceptors

By,
www.JAVA9S.com Srinivas Reddy.S
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

• A Successful login page with a link to authorized page

www.JAVA9S.com
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
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
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
Important Interceptors in
defaultStack
• modelDriven
– Sets the form bean declared by the action in the
valuestack.
– The action should implement the ModelDriven
interface.
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.
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
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
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
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
Thank you
Download PPT and example code from
http://java9s.com

Vous aimerez peut-être aussi