问题
I have 3 buttons for a form I would like to submit. I'm new to struts and I was wondering, is there a way to find which button was clicked? In my html I have this
<html:form action="myAction.jspa">
<div align="right">
<html:submit value="back" />
<html:submit value="continue Later" />
<html:submit value="submit" />
</div>
</html:form>
And in my Java Action
class in the execute()
method, I attempt using request.getParameter("back")
to check if the back button has been pressed but it doesn't work. How does one handle multiple submit buttons in struts?
Thanks for help in advance :)
回答1:
Use the property
attribute on your <html:submit>
tag to specify the name of the request parameter that you'll check in your action.
JSP
<html:submit property="back">back</html:submit>
Action
if(request.getParameter("back") != null) {
// back button was clicked
}
You can, alternatively, give all of your submit buttons the same property, then check the value of the parameter in your action. However, that adds additional internationalization concerns (you'd need to check for non-English translations of the same string).
For more information, see the Struts API.
回答2:
Either use DispatchAction or use javascript and pass value actontype in hidden parameter (back,continue later).
http://unnisworld.wordpress.com/2008/02/14/struts-tip-how-to-organize-related-operations-into-a-single-action-class/
回答3:
you can get press button id by jquery. Then you know which button has clicked. According to Click button Call struts Action.
来源:https://stackoverflow.com/questions/11986599/struts-using-multiple-submit-buttons-for-one-form-how-to-know-which-was-clic