February 09, 2017

Interceptors



Used to control a request
They are invoked by the controller (Filter Dispatcher)
Interceptors perform action such as
o Logging
o validation
o file upload
o double submit guard etc.

 How Interceptor works?
1. Request is generated by user and sent to Servlet container.
2. Servlet container invokes FilterDispatcher filter which in turn determines appropriate action.
3. One by one Intercetors are applied before calling the Action. Interceptors perform tasks such as Logging, Validation, File Upload, Double-submit guard etc.
4. Action is executed and the Result is generated by Action.
5. The output of Action is rendered in the view (JSP, Velocity, etc) and the result is returned to the user.

Explanation: -

1. Request is generated by user and sent to Servlet container http://localhost:8080/StrutsValidationEx/ - web.xml
2. Servlet container invokes FilterDispatcher filter which in turn determines appropriate action. Invoking filter Dispatcher (web.xml)

<filter>

<filter-name>Struts2</filter-name>

<filter-
class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>

</filter>

<filter-mapping>

<filter-name>Struts2</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>


Determining Appropriate action (struts.xml) Welcome.jsp

index.jsp 

Configuring interceptor in struts: -
<interceptors>

<interceptor name="mylogging"

class="net.viralpatel.struts2.interceptor.MyLoggingInterceptor">

</interceptor>

<interceptor-stack name="loggingStack">



<interceptor-ref name="mylogging" />

<interceptor-ref name="defaultStack" />

</interceptor-stack>

</interceptors>



This code has to be added after <result-types > tag in <package>

</package>

Note that creation of interceptor stack with the name “logginstack” is to

make sure that struts2 calls all the default interceptors as well while calling

the custom interceptor mylogging.

This is important because the validation logic will not works in our

application if we ignore default stack of interceptor.



http://javabelazy.blogspot.in/

No comments:

Post a Comment

Your feedback may help others !!!

Facebook comments