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
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.
??? 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.
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.
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
posted
0
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:
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:
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.
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
posted
0
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?
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.
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
posted
0
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.