• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Interceptors in Struts 2

 
Ranch Hand
Posts: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Struts 2 documentation has a guide on interceptorswhich answers your questions.
 
Ranch Hand
Posts: 689
Scala Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Deepak Lal
Ranch Hand
Posts: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm sounds interesting to me...if you have any handy code or snippets,please share the link.
 
Nishan Patel
Ranch Hand
Posts: 689
Scala Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 349
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 349
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
reply
    Bookmark Topic Watch Topic
  • New Topic