Programming Tutorials

(追記) (追記ここまで)

Using JavaScript to submit a form in Struts

By: Apache Foundation in Struts Tutorials on 2008年09月20日 [フレーム]

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.




(追記) (追記ここまで)


Add Comment

JavaScript must be enabled for certain features to work
* Required information
1000

Comments

No comments yet. Be the first!
(追記) (追記ここまで)
(追記) (追記ここまで)

AltStyle によって変換されたページ (->オリジナル) /