aspose file tools*
The moose likes Struts and the fly likes 2 submit buttons with different action Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "2 submit buttons with different action" Watch "2 submit buttons with different action" New topic
Author

2 submit buttons with different action

ashish kulkarni
Ranch Hand

Joined: Aug 15, 2002
Posts: 130
Hi,
I have 2 buttons on one jsp, one is submit button and
the other is back. I want to use only one action form
to handle both the events, I am using
DynaValidatorForm.
So the problem i amfacing is, the fields get validated
even if i press the back button,
Is there a way to make sure that the validation occurs
only when i press submit button


A$HI$H
lokesh reddy
Ranch Hand

Joined: Sep 15, 2000
Posts: 66
Hi Ashish,
You can define two action mappings in the struts-config.xml file which uses one action form.
So for the first tag you say validate=true and for another tag you say validate=false. If you specify validate=false then the validate method in action form does not execute.
For example
For submit button
-----------------
<action path="/submit" type="someclass" name="DynaValidateForm" validate="true" input="/xxx">
</action>
For back button
---------------
<action path="/back" type="someclass" name="DynaValidateForm" validate="false" input="/xxx">
</action>

I hope you find this info useful.
Loke.
Jesse Beaumont
Ranch Hand

Joined: May 30, 2002
Posts: 43
I don't think two actions using the same form is the question here. If I understand correctly you have two submit buttons on the same page and you want to post to a different action depending on which one was clicked?
There are three ways to do this:
1. use javascript to rewrite the form target prior to submission
2. hold a hidden field specifying which action is to be taken and just post to one Action class which does both things
3. Use DispatchAction class to forward the action on appropriately. (This is probably the best option)
Hope this helps
Jesse


---<br />Time flies like an arrow,<br />Fruit flies like a banana
lokesh reddy
Ranch Hand

Joined: Sep 15, 2000
Posts: 66
Hi Jessi,
I think you dint got the problem completely. Pl have a look at the bold letters in the question and let met know your opinion.
I want to use only one action form to handle both the events. I am using DynaValidatorForm here he is talking about one action form handling two different actions and not two submit buttons handling one single action. And also the problem is with handling validations in the actionform i hope so.
Loke .
---------------
I am never afraid of tommorow, since i saw yesterday and i love today.
Jesse Beaumont
Ranch Hand

Joined: May 30, 2002
Posts: 43
Sorry guys,
Ignore my ramblings above. I guess it's good practice to read the post one is replying to, huh
Jesse
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
Originally posted by ashish kulkarni:
Hi,
I have 2 buttons on one jsp, one is submit button and
the other is back. I want to use only one action form
to handle both the events, I am using
DynaValidatorForm.
So the problem i amfacing is, the fields get validated
even if i press the back button,
Is there a way to make sure that the validation occurs
only when i press submit button

Since you are saying you are using one jsp, I will assume both buttons are on the same form and therefore submitting to the same action.
Set validation to false in your struts config file. In the action that is called, determine which button was pressed to submit the form. Depending on which button was pressed, manually call the form's validate method or use a validator class if appropriate.
[ December 31, 2002: Message edited by: Jason Menard ]
Bloo Barton
Ranch Hand

Joined: May 09, 2005
Posts: 63
Rather than start a new topic, I decided to just dig this one back up since it is essentially the same problem I'm having.

I have two buttons that are supposed to submit to the same Action. One button is an actual "Submit" button that I want to require validation from my ActionForm. The other is a "Logout" button that should not be validated.

I have tried setting validation="false" in my struts-config for this action forward and then called the ActionForm's validate method directly from the Action class' submit method.

This partially works. It results in the Logout not requiring validation and logging out correctly.

Submit does validation and submits if validation passes and returns to the original JSP if validation fails.

The problem though is when returned to the original JSP I'm not able to display the ActionErrors because it is empty! How can I get this to stay populated using the method I have described?
Navaneeth Rao
Greenhorn

Joined: Sep 15, 2005
Posts: 3
IF
* You are allowed to use JavaScript
* Don't have to go to server to actually render the resulting page
THEN
* Add javascript code to go back when the user clicks on the back button.
You can do this by :
<input type=button value="back" onKlick="history.go(-1)">
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: 2 submit buttons with different action