Struts2中Form提交的Javascript實(shí)現(xiàn)兩例
Struts2剔除了Struts中對(duì)于form的應(yīng)用,而action(Strus2)的action則綜合了action,和actionForm的應(yīng)用。但很多的應(yīng)用中,都需要對(duì)輸入進(jìn)行驗(yàn)證,Struts中是將輸入給表單,然后取得表單數(shù)據(jù)進(jìn)行驗(yàn)證。雖然Struts2中取消了form的應(yīng)用,這種方式還可以通過(guò)靈活地轉(zhuǎn)化來(lái)繼續(xù)使用。下面是兩個(gè)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%">
- 稿件標(biāo)題
- < /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('講話主題的長(zhǎng)度在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>
總結(jié):先給form命名(useform),點(diǎn)擊"保存"觸發(fā)onclick="save()",save()方法指定執(zhí)行的action的rul,和將整個(gè)(useform)提交submit。((將整個(gè)(useform)提交submit)不能少,不然會(huì)不能提交)同理 (onclick="tosubmit()" value="提交")也一樣。
以上,介紹了Struts2中Form提交的方法兩則。
本文出自 “南湖礦工J2EE技術(shù)博客” 。
【編輯推薦】