Struts2剔除了Struts中对于form的应用,而action(Strus2)的action则综合了action,和actionForm的应用。但很多的应用中,都需要对输入进行验证,Struts中是将输入给表单,然后取得表单数据进行验证。虽然Struts2中取消了form的应用,这种方式还可以通过灵活地转化来继续使用。下面是两个Struts2中Form提交的例子,原理是相同的。
例一
- < SCRIPT>
- function save(){
- var url="< c:out value='${cpath}'/>/publication/mydraftupdateAction.action?param=1"
- document.userForm.action=url;
- document.userForm.method="post";
- document.userForm.submit();
- }
- function tosubmit(){
- var url="< c:out value='${cpath}'/>/publication/mydraftupdateAction.action?param=0"
- document.userForm.action=url;
- document.userForm.method="post";
- document.userForm.submit();
- }
- < /SCRIPT>
- < form name="userForm" method="post">
- < input type="hidden" name="publication.id" id="id" value="${publication.id}" />
- < table width="95%" align=center cellspacing="1" class="contentTable">
- < tr> < td class="low" width="20%">
- 稿件标题
- < /td>
- < td class="lowest" width="30%">
- < input type="text" name="publication.title" id="title"value='${publication.title}'>
- < /td>
- < /tr>
- < /table>
- < table width="95%" border="0" align="center" cellpadding="4" cellspacing="1">
- < tr>
- < td align="right">
- < input name="button" type="button" class="button01"
- onmouseover="makevisible(this,0)"
- nmouseout="makevisible(this,1)" onclick="save()" value="保存"
- style="cursor: hand;">
- < input name="button" type="button" class="button01"
- onmouseover="makevisible(this,0)"
- onmouseout="makevisible(this,1)" onclick="tosubmit()" value="提交"
- style="cursor: hand;">
- < input name="button" type="button" class="button01"
- onmouseover="makevisible(this,0)"
- onmouseout="makevisible(this,1)" onclick="history.back()"
- value="返回" style="cursor: hand;">
- < /td>
- < /tr>
- < /table>
例二
- < SCRIPT type="text/javascript">
- function addsave()
- {
- var name = document.getElementById('subject').value.trim();
- // var depName = document.getElementById('depName').value.trim();
- if(name.length==0)
- {
- alert('讲话主题不能为空或者为空格!')
- return false;
- }
- if(name.length!=0)
- {
- if (name.length< 6||name.length>30)
- {
- alert('讲话主题的长度在6至30之间!')
- return false;
- }
- }
- var url="< c:out value='${cpath}'/>/information/speakaddSaveAction.action"
- document.Form.action=url;
- document.Form.method="post";
- document.Form.enctype="multipart/form-data"
- document.Form.submit();
- }
- function back()
- {
- var url="< c:out value='${cpath}'/>/information/speaklistAction.action"
- document.Form.action=url;
- document.Form.method="post";
- document.Form.submit();
- }
- < /SCRIPT>
- ...
- < form name="Form" method="post" enctype="multipart/form-data">
- < table width="95%" border="0" align="center" cellpadding="4"
- class="resultTable" cellspacing="1">
- < tr class="resultHead">
- < td width="25%" class="leftText">
- 讲话主题
- < /td>
- < td width="25%" class="lowest">
- < s:textfield id ="subject" name="speak.subject" theme="simple" />
- < /td>
- ...
- < /tr>
- < /table>
- < /form>
- < table width="95%" border="0" align="center">
- < tr>
- < td width="80%">< /td>
- < td width="10%" align="right">
- < input name="button" type="button" class="buttonOn"
- onmouseover="makevisible(this,0)"
- onmouseout="makevisible(this,1)" onclick="addsave()" value="保存"
- style="cursor: hand;">
- < /td>
- < td width="10%" align="right">
- < input name="button" type="button" class="buttonOn"
- onmouseover="makevisible(this,0)"
- onmouseout="makevisible(this,1)" onclick="back()" value="返回"
- style="cursor: hand;">
- < /td>
- < /tr>
- < /table>
总结:先给form命名(useform),点击"保存"触发onclick="save()",save()方法指定执行的action的rul,和将整个(useform)提交submit。((将整个(useform)提交submit)不能少,不然会不能提交)同理 (onclick="tosubmit()" value="提交")也一样。
以上,介绍了Struts2中Form提交的方法两则。
本文出自 “南湖矿工J2EE技术博客” 。
【编辑推荐】