Вы находитесь на странице: 1из 14

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 E a!ple
" #ogin page

A Successful login page with a link to authorized page

www.JAVA9

Interceptor $y E a!ple
Redisplay the login page for wrong credentials

Access the Authorized page directly with out logging should forward the login page

www.JAVA9

*reating an Interceptor
%he interceptor class should i!ple!ent the Interceptor interface. I!ple!ent the intercept !ethod. &o the pre processing and if everything is ok, invoke the invocation.invoke'( which will call the ne t co!ponent in )ueue. If so!ething goes wrong, return a string $ased on which a result page will $e picked and sent to the client. "fter the invocation.invoke'(, write the post processing logic

I!portant Interceptors in defaultStack


servlet*onfig Sets the context related objects like +ttpServletRe)uest, +ttpServletResponse, +ttpSession and +ttpServlet*onte t to the action if the action class implements the ServletRe)uest"ware, ServletResponse"ware, Session"ware, Servlet*onte t"ware respectively

I!portant Interceptors in defaultStack


!odel&riven Sets the form bean declared by the action in the valuestack. The action should implement the ModelDriven interface.

I!portant Interceptors in defaultStack


staticPara!s
Helps to set the values from the struts.xml to the properties of action instead of the request parameters. para!s esponsible 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 !ill be set.

I!portant Interceptors in defaultStack


,alidation "hecks if the validation#!are is implemented by action or validates by the validation frame!ork and sets the validation errors. workflow "hecks if there are any field errors or action errors and then decides to invoke the action or to re send the input pa$e

Interceptors in defaultstack
-interceptor.stack na!e/0defaultStack01 %interceptor&ref name'(exception()* Handles the result based on the exception. %interceptor&ref name'(alias()* +nables to have a different named parameter %interceptor&ref name'(servlet"onfi$()* ,ill set the request, response, session objects to the action class. %interceptor&ref name'(prepare()* This interceptor calls-prepare./-on actions !hich implement 0reperable %interceptor&ref name'(i12n()*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'(debu$$in$()*0rovides several different debu$$in$ screens to provide insi$ht into the data behind the pa$e %interceptor&ref name'(profilin$()*#llo!s profilin$ to be enabled or disabled via request parameters, !hen devMode is enabled.

www.JAVA9

Interceptors in defaultstack
%interceptor&ref name'(scopedModelDriven()*#n interceptor that enables scoped model&driven actions. %interceptor&ref name'(modelDriven()*,atches for ModelDriven actions and adds the action3s model on to the value stack %interceptor&ref name'(file4pload()*Helps in file upload. %interceptor&ref name'(checkbox()*5ooks for a hidden identification field that specifies the ori$inal value of the checkbox. %interceptor&ref name'(static0arams()*This interceptor populates the action !ith the static parameters defined in the action confi$uration. %interceptor&ref name'(params(*This interceptor sets all parameters on the value stack %param name'(exclude0arams(*dojo6..7%)param* %)interceptor&ref*

www.JAVA9

Interceptors in defaultstack
%interceptor&ref name'(conversion+rror()* "onversion+rror8nterceptor adds conversion errors from the #ction"ontext to the #ction3s field errors. %interceptor&ref name'(validation(*9alidates the parameters %param name'(excludeMethods(*input,back,cancel,bro!se%)param* %)interceptor&ref* %interceptor&ref name'(!orkflo!(* #n interceptor that does some basic validation !orkflo! before allo!in$ the interceptor chain to continue %param name'(excludeMethods(*input,back,cancel,bro!se%)param* %)interceptor&ref* -2interceptor.stack1

www.JAVA9

%hank you
&ownload PP% and e a!ple code fro!

http3224ava5s.co!

Вам также может понравиться