Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Single Action Class for multiple actions

 
Priya Jotwani
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am new to Struts and so don't have much understanding about the Subject.
I have a scenario in my project where I need to call a Single Action class for all my Actions(there are 4 JSP pages). Can someone explain me as to how to achieve this ?
I have heard of something called DispatchAction in Struts ?

Thanks in Advance,
Priya
 
sreenath reddy
Ranch Hand
Posts: 415
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

This can be done in many ways ...if u want to know entire struts features .......its better u go for DispatchAction .....

In this case ur action should extend DisaptchAction and in ur config file

<action path="/save" type="uraction" name="form" scope="request" parameter="method"/>

and in ur jsp use http://localhost:8080/save.do?method=rename then it will look for a method named rename in ur action so in ur action class write a method with signature

public ActionForward rename(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception

similarly for another jsp u use http://localhost:8080/save.do?method=add
then it will execute a method named add with the above methods signature.



But the simpler way is u juts keep on sending a querystring with differemt values in diff jsps nad in ur common action check the req.getParameter and there u seperate the business logic .this will be really easy for maintainance..

But its up to u in which way to use
 
Priya Jotwani
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Sreenath for the Reply.
I find the second approach easier(due to lack of time to actually understand the first one).
Let me elaborate my understanding on the second approach
In my submit method() of JSP where i'll specify my action class, I should set a hidden variable whose value wud differ in all the four JSPs.

function submit_action()
{
document.form.hdnaction.value = "mypage1"; //hdnaction is the hidden variable.
document.form.action = �commonActionClass.do� ;
}

And in my commonActionClass, i wud check as below :
if request.getParameter("hdnaction") =="mypage1" {
// do stuff wich pertains to mypage1
}
else if request.getParameter("hdnaction") =="mypage2" {
// do stuff wich pertains to mypage2
}

Please let me know if I'm thinking on the correct lines.

Thanks a LOT !!
 
sreenath reddy
Ranch Hand
Posts: 415
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi priya

Exactly what you are doing is right ..............but i will advice one thing to make the code look clear after u check for the request parameter call a method which returns a string and that method name should convey something what u r doing for eg

public ActionForward process(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String commandId = request.getParameter("id");

if (commandId.equals("add")) {
return mapping.findForward(add(mapping, form, request,
response));
}

else if (commandId.equals("delete")) {
return new ActionForward(delete(mapping, form, request,
response));
}



and the business methods will jsut return a string then ur code will be much more clear

Get back in case of any probs
 
Rick Herrick
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And one other thing, which sreenath showed but didn't really point out. For your parameter check, keep in mind that '==' for Java Strings won't do what you want it to there (it does an Object compare as opposed to a string compare), so you need to use the String.equals() method. So your snippet of Java code would actually look like this:



You also should really check that you actually got a value for these parameters before calling the equals() method, meaning that this becomes:



That's all...
 
Priya Jotwani
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Both of you for being so patient !!
I actually realized now that using DispatchAction is also built on the almost the same lines except that when myAction class extends from DispatchAction, I have different perform() for each of the action.
Is there anything i would gain when I actually implement DispatchAction in terms of performance etc ? Please Comment.

Thanks in Advance,
Priya.
 
sreenath reddy
Ranch Hand
Posts: 415
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
of course

u would lose some lines of code if u use dispatchaction ....am i right ??? ; better if u know how to use dispatch action go for it .this is what i advice for those who are comfortable with struts
 
Trent Cioran
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm using a DispatchAction(myAction), but i need to perform validation, this is done through DynaValidatorForm (myForm) in the struts-config.xml. My problem is how to acces the other methods that don't need to be validated, because since i have put validate=true in the action configuration it tries to validate myForm every time i refer to myAction.


thanks for your help.
Trent
 
David Hibbs
Ranch Hand
Posts: 374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Trent Cioran:
Hi,

I'm using a DispatchAction(myAction), but i need to perform validation, this is done through DynaValidatorForm (myForm) in the struts-config.xml. My problem is how to acces the other methods that don't need to be validated, because since i have put validate=true in the action configuration it tries to validate myForm every time i refer to myAction.


thanks for your help.
Trent


PLEASE do not hijack other people's threads with irrelevant questions. Instead wait patiently for someone to answer your own question.

@see http://www.coderanch.com/t/49073/Struts/Problems-validation-dispatchAction
[ September 01, 2004: Message edited by: David Hibbs ]
 
sreenath reddy
Ranch Hand
Posts: 415
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Instead of using dyanvalidatorform using dynavalidatoractionform as this will be useful when u use the same form bean for different actions the pusrpose of struts giving this is is the aboev resaon itslef so that wont create any problem s while u mix up 2-3 action classes in one
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic