How to Detect Submit Button Clicked in Multiple Submit Buttons Scenario in Single Action Class

How to detect submit button clicked in multiple submit buttons scenario in single Action class?

You can define two actions in struts.xml file and use action attribute of <s:submit> tag in order to submit to different actions http://struts.apache.org/docs/submit.html.

In JSP:

<s:submit value="Search" action="searchEmployeeAction"/>
<s:submit value="Add New" action="addEmployeeAction"/>

In struts.xml:

<action name="addEmployeeAction" method="add" class="example.EmployeeAction">
<result>/example/add.jsp</result>
</action>

<action name="searchEmployeeAction" method="search" class="example.EmployeeAction">
<result>/example/search.jsp</result>
</action>

And in your action create two public String methods add and search.

Read about Multiple Submit Buttons http://struts.apache.org/docs/multiple-submit-buttons.html.

Update

Starting from Struts2 version 2.3.15.3 you need to set struts.mapper.action.prefix.enabled constant to true in order to enable support for action: prefix.

Put that in your struts.xml file:

<constant name="struts.mapper.action.prefix.enabled" value="true" />

How to add two submit buttons on one form in Struts 2

How to add 2 submit buttons in one form in struts2

Seems you have already done it, type="button" means a button type="submit" is generated via the Struts tag, but you could use type="submit. For description you can refer the documentation. Not much difference between them, because any of them submit the form. If you want to execute different action or method of the action class then you could use additional attributes like action or method in the submit tag.

How do I determine which is the button I've clicked on jsp form

It depends on where do you want to do it on the client side or on the server side, or both. On the client side you can use javascript to handle events of the form or the button itself. The code is separated by the different handler, so in the handler you exactly know the element that triggered the event, or use an object target attribute to determine that like in this answer. On the server side you can use a value attribute of the input or button tag that is generated and passed as a parameter by name. Note, not all HTML browsers work with the button value attribute.

How to have different submit buttons for a single form but do not be relied on value attribute?

use method or action attributes of the submit tag to redirect each to different method / action.

Multiple submit buttons in Struts 2 form tag

This is normal, because you can map the form to only one action specified by the action attribute of the form tag (Historically in HTML). Submit buttons don't change that mapping, instead they tweak an action mapper to use different action because the prefix is enabled. So, if you need to change this behavior you can alter form mapping dynamically or change the default action mapper, etc. However, it would be a problem if the action instance is already created. Hence, you should use default action mapper. But there's a typo in your post struts.mapper.action.prefix.enabled"="true". The constant that enables action on submit buttons is

<constant name="struts.mapper.action.prefix.enabled" value="true"/>

or if you use struts.properties

struts.mapper.action.prefix.enabled=true

Meteor: form with two submit buttons (determine button clicked in event handler)

You could make the event target inputs with type submit:

Template.NewScenarioForm.events({
"click input[type=submit]": function(e) {
if ($(e.target).prop("id") == "saveScenarioButton") {
// Save the scenario
} else if ($(e.target).prop("id") == "submitScenarioButton") {
// Submit the scenario
}
}
});

You could also make it check the clicked button's value, and drop the ID field

Please note that this will not handle other ways of submitting the form, for example the user pressing Enter in an input field. An approach to handle this as well could be to define a few functions:

function scrapeForm() {
// Collects data from the form into an object
}

function saveData() {
var formData = scrapeForm();
// More logic to save the data
}

function submitData() {
var formData = scrapeForm();
// More logic to submit the data
}

Template.NewScenarioForm.events({
"click input[type=submit]": function(e) {
if ($(e.target).prop("id") == "saveScenarioButton") {
saveData();
} else if ($(e.target).prop("id") == "submitScenarioButton") {
submitData();
}
},
"submit #NewScenarioForm":
// Default action on submit.
// Either save the data
saveData
// or submit the data
submitData
// or nothing, requiring the user to actually click one of the buttons
function(e) {e.preventDefault();}
});

How to submit for different actions from two different button using Struts2

function submitForm(newActionValue){
alert($("#testForm").attr('action')); $("#testForm").attr('action',newActionValue); alert($("#testForm").attr('action')); $("#testForm").submit(); }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><form id="testForm" action="XXX" method="post"> name:<input type="text" name="name"/> <br/> password:<input type="password" name="password"/></form>
<button id="button1" onclick="submitForm('action1')">button1</button><button id="button2" onclick="submitForm('action2')">button2</button>

multiple struts actions on single form not working

Since Struts 2.3.15.3, you need to explicitly enable the action: suffix with:

<constant name="struts.mapper.action.prefix.enabled" value="true"/>

You may also be interested in reading about the ways to call different actions from a form.

Multiple submit buttons in an HTML form

I'm just doing the trick of floating the buttons to the right.

This way the Prev button is left of the Next button, but the Next comes first in the HTML structure:

.f {
float: right;
}
.clr {
clear: both;
}
<form action="action" method="get">
<input type="text" name="abc">
<div id="buttons">
<input type="submit" class="f" name="next" value="Next">
<input type="submit" class="f" name="prev" value="Prev">
<div class="clr"></div><!-- This div prevents later elements from floating with the buttons. Keeps them 'inside' div#buttons -->
</div>
</form>


Related Topics



Leave a reply



Submit