2015-09-25 66 views
0

我這樣做教程: http://proliferay.com/form-submit-in-spring-mvc-portlet/Spring MVC中的Portlet:形式不保存數據正確

一切正常,但在最後的部分。 在我的電腦上,客戶表單正確顯示。但是,如果我在表單中設置了客戶的詳細信息,那麼當我驗證表單時,客戶無法保存。 因此,下一個jsp(success.jsp)沒有找到保存的Customer客戶,因此顯示一個新數據爲空數據。

我的版本和教程的唯一區別是我必須從Maven構建項目,但這不應該改變任何東西?我可以得到正確答案: System.out.println(「\ ncustomer:」+ request.getAttribute(「customer」)+「\ n」);

這裏有class.jsp:

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 
    <%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %> 

    <%System.out.println("\ncustomer : " + request.getAttribute("customer") + "\n"); %> 


<portlet:actionURL var="submitFormURL" name="handleCustomer"/> 
<form:form name="customer" method="post" modelAttribute="customer" action="<%=submitFormURL.toString() %>"> <%-- onSubmit="displayParams"> --%> 


    <br/> 
     <table style="margin-left:80px"> 
      <tbody> 
       <tr> 
        <td><form:label path="firstName">First Name</form:label></td> 
        <td><form:input path="firstName"></form:input></td> 
       </tr> 
       <tr> 
        <td><form:label path="middleName">Middle Name</form:label></td> 
        <td><form:input path="middleName"></form:input></td> 
       </tr> 
       <tr> 
        <td><form:label path="lastName">Last Name</form:label></td> 
        <td><form:input path="lastName"></form:input></td> 
       </tr> 
       <tr> 
        <td><form:label path="age">Age</form:label></td> 
        <td><form:input path="age"></form:input></td> 
       </tr> 
       <tr> 
        <td><form:label path="address">Address</form:label></td> 
        <td><form:input path="address"></form:input></td> 
       </tr> 

       <tr> 
        <td colspan="2"><input type="submit" value="Submit Form"> 
        </td> 
       </tr> 
      </tbody> 
     </table> 
    </form:form> 

和相應的控制方法:

@ActionMapping(value = "handleCustomer") 
     public void getCustomerData(
       @ModelAttribute("customer") Customer customer, 
       ActionRequest actionRequest, ActionResponse actionResponse, 
       Model model) { 

      log.info("#############Calling getCustomerData : post form validation##########"); 

      System.out.println("\nModel : " + model); 
//displays : "Model : {customer=null null,............." 



      System.out.println(customer.getFirstName()); 
      System.out.println(customer.getLastName()); 
      System.out.println(customer.getAddress()); 

      actionResponse.setRenderParameter("action", "success"); 

      model.addAttribute("successModel", customer); 
     } 

任何想法是什麼能阻止它的工作? THX提前浮現在我的腦海裏

+0

的聲明在哪裏? – libik

+0

我相信他需要發送某種文章或者將回復發送給spring mvc主幹... –

+0

對不起,在編輯中加入。我在之前的複製粘貼中錯過了它們。 –

回答

2

您可能正在使用的Liferay 6.2+,如果是的話應該比你設定(彈簧未命名空間參數 - 見SPR-11176

<requires-namespaced-parameters>false</requires-namespaced-parameters> 

到Liferay-portlet.xml中

Element : requires-namespaced-parameters 
Set the requires-namespaced-parameters value to true if the 
portlet will only process namespaced parameters. The default 
value is true. 

描述摘自http://www.liferay.com/dtd/liferay-portlet-app_6_2_0.dtd

+0

您好Martin,非常感謝您的支持。它取得了訣竅:) –

+1

我很高興你的問題解決了。根據portlet規範,參數必須是命名空間,Liferay(從版本6.2開始)改變了它的行爲。但爲了向後兼容,他們引入了這個(requires-namespaced-parameters)設置。 –

0

一些事情你可以試試:

改變這種

model.addAttribute("successModel", customer); 

這個

model.addAttribute("customer", customer); 

而不是使用namemodelAttribute的你可以使用這個:commandName="customer"


也許<%@之後的缺失空間可能是問題?

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 
+0

這兩個建議的結果相同 –