Struts 2 Question.

AbdellahT used Ask the Experts™
I have a JSP form with "Continue" and "Cancel" buttons. I would like to call the same action that the continue button invokes when the cancel button is clicked. I need to be able to determine that it was the cancel button that was clicked so I can process a different section of code. I have done this in Struts1 but I do not know how to do it in Struts2 or I do not know an efficient way to accomplish this.

Any help is appreciated.


Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
set the value of the cancel button to cancel and the name to button

<html:submit value="Cancel" name="button"/>

and the same for the submit
<html:submit value="Continue" name="button"/>

then check the value of the property button in your action.
Top Expert 2009
If you will be executing different sections of code, then it probably makes more sense to declare a method on the action class for each button, and factor out the common functionality to a shared, protected method.

class MyAction extends ActionSupport {

public String cancel() {
// some action specific code
return SUCCESS; }

public String continue() {
// some action specific code
return SUCCESS;

protected doSomething() {
// shared logic


Then some ways to call the action would be as below..

<s:form action="MyAction" namespace="/customers" method="post">

<input type="image" name="method:continue" src="<s:url value="/images/button_continue.png"/>" title="Continue" />
<input type="image" name="method:cancel" src="<s:url value="/images/button_cancel.png"/>" title="Cancel" />


--Or explicit URLs

<s:url id="continueUrl" action="continue" namespace="/myaction"/>
<a href="<s:property value="continueUrl"/>"><img src="<s:url value="/images/button_continue.png"/>" title="Continue" /></a>

<s:url id="cancelUrl" action="cancel" namespace="/myaction"/>
<a href="<s:property value="cancelUrl"/>"><img src="<s:url value="/images/button_cancel.png"/>" title="Cancel"/></a>

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial