2013-10-02 45 views
1

在這裏快速的問題(希望如此)。我正在開發一個webapp,並試圖使用ajax來完成一個post子。這工作正常,第一次...在第二次嘗試在同一個會話期間,模型屬性發送到服務器,是第一個。不知何故,即使我發送模型的新實例,它也不會被重置。讓我添加一些代碼:Spring MVC模型屬性沒有得到重置

控制器:

@RequestMapping(value = "/productores/new.html", method = RequestMethod.GET) 
public ModelAndView newForm(){  
    log.info("Cargando form de productor..."); 
    return new ModelAndView("/secure/Productor","productor",new Productor());  
} 

@RequestMapping(value = "/productores/new.html",method = RequestMethod.POST) 
public ModelAndView processNewForm(
     @ModelAttribute("productor") Productor prod, 
     BindingResult result, SessionStatus status){ 

    log.info("Procesando nuevo productor.");  
    prodServ.guardar(prod); 
    return null; 

} 

Productores.jsp(本頁持有,顯示所有生產商的網格,並有一個按鈕,顯示出表格添加一個,作爲一個模式對話框這使用jQuery的用戶界面對話框。)

function showDialog() { 
    var tag = $("<div id='modalForm'></div>"); 
    $.ajax({ 
     type : "GET", 
     url : "/SegurosWeb/productores/new.html", 
     success : function(response) { 
      if (typeof response == "object" && data.html) { //response is assumed to be JSON 
       tag.html(response.html).dialog({ 
        modal : options.modal 
       }).dialog("open"); 
      } else { //response is assumed to be HTML 
       tag.html(response).dialog({ 
        modal : true, 
        position: ["center", "center"], 
        width : 1000 
       }).dialog("open"); 
      } 
     } 
    }); 
} 

Productor.jsp(這是將在一個模態對話框被POP操作了形式)

  <form:form id="formProductor" method="POST" commandName="productor" 
    class="formgeneral" onsubmit="doAjaxPost('/SegurosWeb/productores/new.html');return false;"> 
    <div id="row"> 
     <div id="separador"> 
      <p class="separador">Datos del Productor</p> 
     </div> 
     <ul> 
      <li><form:label path="apellido">Apellido:</form:label> <form:input 
        path="apellido" type="text" class="formL" required="required" /></li> 
      <li><form:label path="nombre">Nombre:</form:label> <form:input 
        path="nombre" type="text" class="formL" required="required"></form:input></li> 
      <li><form:label path="matricula">Matrícula:</form:label> <form:input 
        path="matricula" type="number" class="formM" pattern="[0-9]{9}" 
        title="Ingrese la matricula del Productor (solo numeros)" /></li> 
     </ul> 
    </div> 
    <!--fin de row--> 
    <div id="botoneraform"> 

Ingresar < /按鈕> - > Ingresar Cancelar

,並在其中

<script type="text/javascript"> 
function doAjaxPost(toUrl) { 
    var a=$("#formProductor").serialize(); 
    $.ajax({ 
     type : "POST", 
     url : toUrl, 
     data: a, 
     success : function(response) { 
      $('#modalForm').dialog('close'); 
      $('#grid').trigger('reloadGrid'); 
     }, 
     error : function(jqXHR, textStatus, errorThrown) { 
      alert(jqXHR + " : " + textStatus + " : " + errorThrown); 
     } 
    }); 

}

我需要專門破壞模型屬性某處後阿賈克斯重置它的過程?或者我在這裏做的很糟糕?

在此先感謝!

回答

0

Model屬性在一次請求期間只是「活着的」。在處理過程中,它們被添加到HttpServletRequest屬性中,然後可用於JSP。

當您使用@ModelAttribute的處理方法」參數列表就像你在這裏做

public ModelAndView processNewForm(
     @ModelAttribute("productor") Productor prod, 
     BindingResult result, SessionStatus status){ 

春天試圖從你的請求參數創建對象。您的Productor.jsp中有一些這樣的,即。 <form:input>元素。每次執行POST時,都會使用它們來創建一個Productor對象。

+0

謝謝Sotirios。但是我開始認爲我的問題可能與這樣一個事實有關,即我沒有做標準表單子表單,而是做了一個ajax表單,並且具有表單的對話框在後續調用中可能仍然存在,因爲我只是做一個「$('#modalForm')。對話框('close');」到它。 – Emilio

+0

@Emilio我的觀點是模型屬性沒有跨服務器端保存在請求中。您每次都必須重新發送相同的內容。 –