GeeCON Prague 2014*
The moose likes Struts and the fly likes controlling submit  button 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 "controlling submit  button in struts-2" Watch "controlling submit  button in struts-2" New topic
Author

controlling submit button in struts-2

Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
In a jsp ,I have a table with a few rows .The user need to select a row and click on 'Go' button placed below the table ,to go to next jsp.

If the user doesn't select a row and clicks 'Go' button, the user should be promped with a message saying "Please select a row to proceed!" and stay in the same page.

How to achieve this? On click of that button I tried calling a javascript function which has the alert for the above message but the control would go from view to Action class.

What should be done here to stay in same page?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

On click of that button I tried calling a javascript function which has the alert for the above message

After the alert, return false from the javascript method. That's it, the page will not be submitted...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
This is what I wrote... I call this on button click



It still goes to next page!
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

Do you see the second alert in your browser (alert("Add some vocabulary to proceed !!!");)?? And what is this

What's this statement supposed to do?? Where are you checking if any checkbox is checked??
Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
Yes, I can see that alert statement!

Actually I don't have a checkbox rather user should select a row and then click on go button... The rows are added dynamically and hence to get the rowcount of the table I used that!

The 'if' condition was to check whether the user added any rows and selected (count>1) else it would be the table with only a header and hence the Alert!
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

Well returning false from a javascript method on the click event of a submit button stops the form processing. If it is not working, then change the submit button to a normal button. Then change the vocabCheck to something like this

Here formId is the value of the ID attribute of the associated form tag to be submitted. So if the user doesn't select a row, then the alert will be shown and nothing will happen, but if the user selects a row, then the form will be submitted...
Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
That didn't work !

tried writing like this...



Unable to figure out the error! wonder how to trace it.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

Pradeep Adibatla wrote:That didn't work !

Did you try my code (the if-else one)?? There's no way it will not work. What you have to do is, change the submit button to a normal button, give the form an ID, and use the ID in the getElementById method in the else part...
Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
Solved! returning false from a function doesn't hold good whereas return statement in a function call is a hit !!!



where reutnVal was the JS variable!
rajesh gorijala
Greenhorn

Joined: Jan 20, 2013
Posts: 1
Ankit Garg wrote:Well returning false from a javascript method on the click event of a submit button stops the form processing. If it is not working, then change the submit button to a normal button. Then change the vocabCheck to something like this

Here formId is the value of the ID attribute of the associated form tag to be submitted. So if the user doesn't select a row, then the alert will be shown and nothing will happen, but if the user selects a row, then the form will be submitted...


Thank you. It worked for me
 
GeeCON Prague 2014
 
subject: controlling submit button in struts-2