You are on page 1of 2

You can submit a form with a link as below.

BTW, the examples below assume you are


in an <html:form> block and 'myForm' is picked up from the struts-config.xml name field
of the action.

<a href='javascript:void(document.forms["myForm"].submit()>My
Link</a>
Now the trick in the action is to decode what action you intend to perform. Since you are
using JavaScript, you could set a field value and look for it in the request or in the form.
... html/javascript part ...

<input type='hidden' value='myAction' />


<input type='button' value='Save Meeeee'
onclick='document.forms["myForm"].myAction.value="save";
document.forms["myForm"].submit();' />
<input type='button' value='Delete Meeeee'
onclick='document.forms["myForm"].myAction.value="delete";
document.forms["myForm"].submit();' />

... the java part ...

class MyAction extends ActionForm implements Serializable {

public ActionForward execute (ActionMapping map, ActionForm


form,
HttpServletRequest req, HttpServletResponse) {

String myAction = req.getParameter("myAction");

if (myAction.equals("save") {
// ... save action ...
} else if (myAction.equals("delete") {
// ... delete action ...
}
}
}
}

This is just one of many ways to achieve submitting a form and decoding the intended
action. Once you get used to the framework you will find other ways that make more
sense for your coding style and requirements. Just remember this example is completely
non-functional without JavaScript.

//*****************************************************************************
****
submit form using javascript in struts

You might also like