I'm developing an application using Struts2. My application is for online shopping. My task is updating and removing the shopping cart item. I have a JSP page and it has 4 text fields. I need to submit these text field's data to the Modify action. Updating and removing submit buttons are in same page, same form. How do I determine which is the button I've clicked on JSP form?

My form is:

<s:form action="ModifyCart">
   <s:textfield readonly="true" name="shoppingCart.item.id" label="Item Code" value="% item.id}"/>
   <s:textfield readonly="true" label="Item Name" value="%{item.itemName}"/>
   <s:textfield name="shoppingCart.qty" value="%{qty}"/>                                                                
   <s:textfield readonly="true" label="Available Quantity" name="shoppingCart.item.qty" value="%{item.qty}"/>
   <s:submit type="button" label="Update" name="submit"/>
   <s:submit type="button" label="Remove" name="clear"/></s:form>

Important:

I need to submit all text field's data to the Action class with every submit.

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top