Struts2中Form提交的Javascript实现两例

开发 后端
本文介绍了两种Struts2中Form提交的Javascrpit实现方法。Struts2剔除了Struts中对于form的应用,而action(Strus2)的action则综合了action,和actionForm的应用。

Struts2剔除了Struts中对于form的应用,而action(Strus2)的action则综合了action,和actionForm的应用。但很多的应用中,都需要对输入进行验证,Struts中是将输入给表单,然后取得表单数据进行验证。虽然Struts2中取消了form的应用,这种方式还可以通过灵活地转化来继续使用。下面是两个Struts2中Form提交的例子,原理是相同的。

例一

  1. < SCRIPT> 
  2. function save(){  
  3.     var url="< c:out value='${cpath}'/>/publication/mydraftupdateAction.action?param=1" 
  4.       document.userForm.action=url;  
  5.    document.userForm.method="post";  
  6.    document.userForm.submit();  
  7. }     
  8. function tosubmit(){  
  9.    
  10.     var url="< c:out value='${cpath}'/>/publication/mydraftupdateAction.action?param=0" 
  11.     document.userForm.action=url;  
  12.    document.userForm.method="post";  
  13.    document.userForm.submit();  
  14. }  
  15. < /SCRIPT> 
  16.    
  17. < form name="userForm" method="post"> 
  18.     < input type="hidden" name="publication.id" id="id" value="${publication.id}" /> 
  19. < table width="95%" align=center cellspacing="1" class="contentTable"> 
  20.     < tr> < td class="low" width="20%"> 
  21.         稿件标题  
  22.         < /td> 
  23.         < td class="lowest" width="30%"> 
  24.     < input type="text" name="publication.title"   id="title"value='${publication.title}'> 
  25.         < /td> 
  26. < /tr> 
  27. < /table> 
  28. < table width="95%" border="0" align="center" cellpadding="4"                cellspacing="1"> 
  29.     < tr> 
  30.         < td align="right"> 
  31.     < input name="button" type="button" class="button01" 
  32.         onmouseover="makevisible(this,0)" 
  33.             nmouseout="makevisible(this,1)" onclick="save()" value="保存" 
  34.             style="cursor: hand;"> 
  35.     < input name="button" type="button" class="button01" 
  36.             onmouseover="makevisible(this,0)" 
  37.             onmouseout="makevisible(this,1)" onclick="tosubmit()" value="提交" 
  38.             style="cursor: hand;"> 
  39.     < input name="button" type="button" class="button01" 
  40.             onmouseover="makevisible(this,0)" 
  41.             onmouseout="makevisible(this,1)" onclick="history.back()" 
  42.             value="返回" style="cursor: hand;"> 
  43.         < /td> 
  44.         < /tr> 
  45.     < /table> 

例二

  1. < SCRIPT type="text/javascript"> 
  2.        function addsave()  
  3.        {  
  4.        var name = document.getElementById('subject').value.trim();  
  5.        //  var depName = document.getElementById('depName').value.trim();  
  6.            if(name.length==0)  
  7.            {  
  8.                alert('讲话主题不能为空或者为空格!')  
  9.                return false;  
  10.            }  
  11.            if(name.length!=0)  
  12.            {  
  13.                if (name.length< 6||name.length>30)  
  14.                {  
  15.                   alert('讲话主题的长度在6至30之间!')  
  16.                   return false;  
  17.                }  
  18.            }  
  19.            var url="< c:out value='${cpath}'/>/information/speakaddSaveAction.action" 
  20.             document.Form.action=url;  
  21.             document.Form.method="post";  
  22.             document.Form.enctype="multipart/form-data" 
  23.             document.Form.submit();  
  24.        }  
  25.        function back()  
  26.        {  
  27.            var url="< c:out value='${cpath}'/>/information/speaklistAction.action" 
  28.             document.Form.action=url;  
  29.             document.Form.method="post";  
  30.             document.Form.submit();  
  31.        }  
  32.     < /SCRIPT> 
  33.          
  34. ...  
  35.    
  36. < form name="Form" method="post" enctype="multipart/form-data"> 
  37.            < table width="95%" border="0" align="center" cellpadding="4" 
  38.               class="resultTable" cellspacing="1"> 
  39.               < tr class="resultHead"> 
  40.                   < td width="25%" class="leftText"> 
  41.                      讲话主题  
  42.                   < /td> 
  43.                   < td width="25%" class="lowest"> 
  44.                      < s:textfield id ="subject" name="speak.subject" theme="simple" /> 
  45.                   < /td> 
  46.                     
  47. ...  
  48.    
  49.               < /tr> 
  50.            < /table> 
  51.            < /form> 
  52.            < table width="95%" border="0" align="center"> 
  53.               < tr> 
  54.                   < td width="80%">< /td> 
  55.                   < td width="10%" align="right"> 
  56.                      < input name="button" type="button" class="buttonOn" 
  57.                          onmouseover="makevisible(this,0)" 
  58.                          onmouseout="makevisible(this,1)" onclick="addsave()" value="保存" 
  59.                          style="cursor: hand;"> 
  60.                   < /td> 
  61.                   < td width="10%" align="right"> 
  62.                      < input name="button" type="button" class="buttonOn" 
  63.                          onmouseover="makevisible(this,0)" 
  64.                          onmouseout="makevisible(this,1)" onclick="back()" value="返回" 
  65.                          style="cursor: hand;"> 
  66.                   < /td> 
  67.               < /tr> 
  68.            < /table>   

总结:先给form命名(useform),点击"保存"触发onclick="save()",save()方法指定执行的action的rul,和将整个(useform)提交submit。((将整个(useform)提交submit)不能少,不然会不能提交)同理 (onclick="tosubmit()" value="提交")也一样。

以上,介绍了Struts2中Form提交的方法两则。

本文出自 “南湖矿工J2EE技术博客” 。

【编辑推荐】

  1. 教你如何配置Struts2 web.xml文件
  2. Struts2深入详解properties配置文件
  3. Struts2 iterator介绍及功能详解
  4. Struts2 checkbox适用场景及实例分析
  5. Struts2 国际化与防止刷新重复提交表单
责任编辑:yangsai 来源: 51CTO博客
相关推荐

2009-02-04 11:00:44

2011-04-28 09:52:04

Struts2

2009-06-25 15:22:03

Struts2教程一个form多个sub

2009-07-29 09:54:34

struts2和str

2012-04-25 10:14:40

JavaStruts

2009-06-25 15:11:28

Struts2教程Struts2程序

2009-06-25 15:59:21

Struts2教程拦截器

2009-06-05 10:37:52

struts2 国际化表单

2011-07-18 14:43:40

JSON模拟加载初析

2009-02-04 10:51:07

2012-05-10 14:00:06

StrutsjsonJava

2009-07-14 17:10:44

struts2webwork

2009-06-04 09:20:19

struts2 if标使用

2009-02-04 14:45:06

2009-06-04 08:01:25

Struts2拦截器原理

2009-06-04 08:45:01

Struts2下载

2009-07-03 09:35:57

Struts2 JSP

2009-06-08 16:44:00

Struts2文件上传

2009-06-04 08:34:24

Struts2配置struts.xml

2009-06-05 10:05:50

struts menustruts2
点赞
收藏

51CTO技术栈公众号