jquery form向spring mvc提交表單

jquery.form把form封裝了一下,能夠直接提交表單,以ajax的形式,而spring mvc中有個modelAttribute屬性,能夠把表單傳來的參數包裝成對象類型,這樣在提交參數的時候處理起來就省事多了(PS:任何省事都是創建在費事研究的基礎上),請看代碼 javascript

javascript:html

 

Js代碼  
  1. <script type="text/javascript">  
  2.     function callBackGraFunc(responseText, statusText) {  
  3.         if (responseText == 1) {  
  4.             // 獲取select控件文本  
  5.             var fgraduationState1 = document.getElementById("fgraduationState");  
  6.             var fgraduationStateText = fgraduationState1.options[fgraduationState1.selectedIndex].text;  
  7.               
  8.             // populate the form  
  9.             $("#fgraduationTime1").text($("#fgraduationTime").val());  
  10.             $("#fgraduationState1").text(fgraduationStateText);  
  11.             $("#fgraduationReason1").text($("#fgraduationReason").val());  
  12.             $("#fdipomaNumberr1").text($("#fdipomaNumberr").val());  
  13.             $("#fdegreeNumber1").text($("#fdegreeNumber").val());  
  14.             $("#fcerNumber1").text($("#fcerNumber").val());  
  15.             $("#fdiplomaDate1").text($("#fdiplomaDate").val());  
  16.             $("#fdegreeDate1").text($("#fdegreeDate").val());  
  17.             $("#fcerDate1").text($("#fcerDate").val());  
  18.         } else {  
  19.             alert("保存數據出錯");  
  20.         }  
  21.     }  
  22.       
  23.     $(document).ready(function() {  
  24.             var options = {  
  25.                 success: callBackGraFunc  
  26.             };  
  27.               
  28.             // jquery.form 提交表單  
  29.             $('#form1').ajaxForm(options);   
  30. </script>  

 

$('#form1').ajaxForm(options)是渲染form裏的數據,提交時以ajax方式提交,頁面不顯示刷新。前端

var options是一個回調函數,當form提交成功,action裏有數據返回時,調用callBackFunc方法進行前端的數據的填充和渲染。java

 

jsp:jquery

 

Html代碼  
  1. <%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>  
  2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>  
  3. <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>  
  4. <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>  
  5. <c:set var="ctx" value="${pageContext.request.contextPath}"/>  
  6.   
  7. <form:form name="graduationForm" modelAttribute="_graduation" id="form1" action="${ctx}/enrollment/graduation/${_info.fid}/save" method="post">  
  8.     <input type="hidden" name="fid" value="${_info.fid}" />  
  9.     <input type="hidden" name="enrStudentInfo.fid" value="${_info.enrStudentInfo.fid}" />  
  10.     <input type="hidden" name="fcredit" value="${_info.fcredit}" />  
  11.     <input type="hidden" name="fappraisal" value="${_info.fappraisal}" />  
  12.   
  13. </form:form>  
 

上面使用了spring的form標籤,在題頭需引進定義ajax

 

Html代碼 
  1. <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>  

 

java:spring

 

Java代碼  
  1. /** 
  2.  * Destription Ajax 保存畢業、結業信息 
  3.  * @param fid 
  4.  * @param enrGraduation 
  5.  * @param redirectAttributes 
  6.  * @return  
  7.  */  
  8. @RequestMapping(value = "/{fid}/save", method = RequestMethod.POST)  
  9. public String saveGra(@ModelAttribute("_graduation") EnrGraduation _graduation, HttpServletRequest request, HttpServletResponse response)  
  10. {  
  11.     response.setContentType("text/plain;charset=UTF-8");    
  12.     PrintWriter out = null;  
  13.     try {  
  14.         out = response.getWriter();  
  15.     } catch (IOException e) {  
  16.         e.printStackTrace();  
  17.     }  
  18.           
  19.     // 判斷信息是否存在  
  20.     if(!_graduation.isNew()){  
  21.         _graduation.setFupdatetime(new Date());  
  22.         _graduation.setFisRemove(0);  
  23.         enrGraduationService.update(_graduation);  
  24.           
  25.         out.print("1");  
  26.         out.close();  
  27.     } else {  
  28.         out.print("0");  
  29.         out.close();  
  30.     }  
  31.       
  32.       
  33.     return null;  
  34. }  
 

在類中接受「_graduation」參數,包裝成對象,而後返回ajax數據。mvc


使用jquery.form,須要引進jquery.form.js,在佈局時,Jquery.js寫在上面,由於先渲染jquery.jsapp

 

Html代碼  
  1. <script type="text/javascript" src="${ctx}/static/js/jquery-1.7.1.min.js"></script>  
  2. <!-- jquery form js -->  
  3. <script type="text/javascript" src="${ctx }/static/js/jquery.form.js" ></script>  
相關文章
相關標籤/搜索