This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Struts and the fly likes Interceptors in Struts 2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Interceptors in Struts 2" Watch "Interceptors in Struts 2" New topic
Author

Interceptors in Struts 2

Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

Hi All,
I have the below clarifications.
1> what is an interceptor in STRUTS 2 ?
2> where do we use them ?
3> Can you provide me an example for the same.?

--
Deepak Lal


When The Going Gets Tougher,The Tougher gets Going
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8915
    
    8

The Struts 2 documentation has a guide on interceptorswhich answers your questions.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 688

Hi..

Interceptors is nothing but just like Class which is called before and after Action called.

So main use of it is reduce load on Action class and mostly use for prevent double form submitting , validation befor action called and more than this.

Struts 2.0 has Interceptors is power full feature and it is better to use in Application.

Thanks,
Nishan Patel.


Thanks, Nishan Patel
SCJP 1.5, SCWCD 1.5, OCPJWSD Java Developer,My Blog
Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

Hmm sounds interesting to me...if you have any handy code or snippets,please share the link.
Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 688

Hi..

I have worked on this and use logininterceptor

public class LoginInterceptor implements Interceptor, StrutsStatics {

public void destroy() {

}
public void init() {

}
public String intercept(ActionInvocation invocation) throws Exception {

final ActionContext context = invocation.getInvocationContext ();
HttpServletRequest request = (HttpServletRequest) context.get(HTTP_REQUEST);
HttpSession session = request.getSession (true);

??? Impliment your validation logic here and use in Struts.xml ???
??? if you use this in xml every time it called and check for validation ???

}

}



Thanks,
Nishan Patel.
Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

Hi Nishant,


??? Impliment your validation logic here and use in Struts.xml ???
??? if you use this in xml every time it called and check for validation ???
--- what is this VALIDATION LOGIC


can you explain me in brief,Nishant,can you give me a working example.
I'm new to struts2,,what is the validation logic you are talking about.

--
Deepak Lal
Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

Hi,
Any working example for Interceptors please...I'm new to Interceptors and finding it difficult to write code for interceptors on my own.
Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

Hi,
Any Complete working example for Interceptors please...I'm new to Interceptors and finding it difficult to write code for interceptors on my own.

Help needed ....


--
Deepak Lal
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8915
    
    8

There's an example in the link I posted a week ago. If you don't understand it or are having trouble implementing one of your own, you must Tell the Details of your code and configuration in order for use to help you.
Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

Please see source code below.It is got from the below URL writing-interceptors

Assuming there is an action of type "MyAction", with a setDate(Date) method, this simple interceptor will set the date of the action to the current date:



Now my questions/Queries are below.

1> how should i configure my struts.xml file to make this interceptor compile and run without errors.?
2> what external configurations have to be done in files to make my code compile and run ?

Help provided will be highly appreicated.

--
Deepak Lal
Tom Rispoli
Ranch Hand

Joined: Aug 29, 2008
Posts: 349
You need to do 3 things in the struts.xml file to get the interceptor to run.

first - in the <interceptors> section you need to <interceptor> tag to declate the class for your interceptor and give it a name that you can refer to it by. Something like this:

<interceptor name="SimpleInterceptor" class="com.yourcompany.interceptor.SimpleInterceptor"></interceptor>

Second - also in the <interceptors> section, you need to define an interceptor stack that includes your interceptor. When you define your stack you will need to include references to any struts interceptors or interceptor stacks that you want to run since you will be overridding the default stack. Something like this:

<interceptor-stack name="MyInterceptorStack">
<interceptor-ref name="prepare"/>
<interceptor-ref name="modelDriven"/>
<interceptor-ref name="exception"/>
<interceptor-ref name="servletConfig"/>
<interceptor-ref name="checkbox"/>
<interceptor-ref name="params"/>
<interceptor-ref name="SimpleInterceptor"></interceptor-ref></interceptor-stack>


third you need to set this as the default stack for your package. Something like this:

<default-interceptor-ref name="MyInterceptorStack"></default-interceptor-ref>


For your second question I don't think you have to do anything externally other than make sure that the class file for your interceptor ends up in the class path of your application server.
Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

I have the below clarification:



Can you tell me how should i implement MyAction(which has setDate Method) of interceptor class ??

I'm a bit confused on how to proceed for this.??

Help provided will be highly appreciated.

--
Deepak Lal
Tom Rispoli
Ranch Hand

Joined: Aug 29, 2008
Posts: 349
Hi Deepak, I don't understand what you are asking. What do you mean by "implement" your action class? The code you have for the interceptor looks correct to me. Are you getting an error from it? Is it not doing what you expect it to?
Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

Originally posted by Tom Rispoli:
Hi Deepak, I don't understand what you are asking. What do you mean by "implement" your action class? The code you have for the interceptor looks correct to me. Are you getting an error from it? Is it not doing what you expect it to?


Dear Tom Rispoli,

Please find my comments below.


My Question here goes as follows.
MyAction in the above code is a class which has setDate method which takes date as an argument.so i wanted to know how to write the code for
class "MyAction" ??

i.e Is MyAction class a built in class as part of any package ???
(or)
Should we provide setters/getters for date in MyAction class ???

hope you understand now???

Please let me know the logic for MyAction class which has setDate(new Date()) as an argument.

Help provided will be highly appreciated.

--
Deepak Lal
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Your sample interceptor code has the following line:



That sort of implies the action will have a setDate(Date) method, yes? So write that method.
Tom Rispoli
Ranch Hand

Joined: Aug 29, 2008
Posts: 349
MyAction is a class that you define. Its one you should have already defined. When you set up your struts.xml file you specify which class will be used to process each action in the file. The class you are getting a reference to in the interceptor is an instance of the action class for your action. So you should be able to cast it to the type of your current action or any of its parent classes. In my application my custom interceptor runs for all actions so I have it get a reference to a parent class that I created for all of my actions.
Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

Dear Tom
Can you tell me how should i go about writing the logic for MyAction class now?

Help provided will be highly appreciated...

Sample logic provided will be highly appreciated.



--
Deepak Lal
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

 
GeeCON Prague 2014
 
subject: Interceptors in Struts 2