2016-04-07 41 views
0

情況:我正在開發一個spring MVC webapp並使用spring web flow和tiles框架。我在DB customercustomerAdress中有兩個表格,我有兩個表格,分別命名爲customerModelcustomerAdressModel如何將多個模型綁定到Spring Web Flow中的單個視圖?

在我flow.xml

我現在有以下view-state

<var name = "cust" class = "com.model.CustomerModel"/> 

<view-state id = "customerViewState" view = "customer" model = "cust"> 

     <transition on="next" to="customerData"/> 

    </view-state> 

磁磚框架可解決其在下面示出的視圖customer撥出customer.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"  
pageEncoding="ISO-8859-1"%> 
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%> 
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%> 
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  
"http://www.w3.org/TR/html4/loose.dtd"> 

<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
</head> 
<body> 

<div> 
<form id="Form" method="post" enctype="multipart/form-data" class="form- 
inline">  
    <div class="inputDiv"> 

      <label class="inputLabel"> Name :</label> 
      <input type="text" name="name" id="name">   



      <label class="inputLabel">Email :</label> 
      <input type="email" name="email" id="email"> 

    </div>    
    <input type="button" id="forwardButton" value="Next"/> 
</form> 
</div> 

</body> 
<script> 

$("#forwardButton").click(function(){ 

$("#WlDetailsForm").attr('action','${flowExecutionUrl}&_eventId=next'); 
    $("#WlDetailsForm").submit(); 

}); 
</script> 
</html> 

問題:現在customer.jsp中指定的格式具有一些輸入字段,其中包含customerAdressModel的屬性值。因此,我想綁定customerModel以及customerAdressModel到相同的視圖狀態customerViewState。我怎麼做,我經歷了春季DOC,但沒有找到任何東西,請幫助!

注:我不能改變我的SQL表

回答

0

您可以創建複合模型DTO

public class CompositeModelDto { 

    private CustomerModel suctomer; 

    private CustomerAddressModel address; 

    //setters ang getters ... 

} 

而且使用它作爲一個視圖狀態模型

<var name = "cust" class = "com.model.CustomerModel"/> 
<var name = "address" class = "com.model.CustomerAddressModel"/> 
<var name = "customerDto" class = "com.model.CompositeModelDto"/> 

<view-state id = "customerViewState" view = "customer" model = "customerDto"> 
    <on-entry> 
     <set name="customerDto.customer" value="cust"/> 
     <set name="customerDto.address" value="address"/> 
    </on-entry> 

    <transition on="next" to="customerData"/> 

</view-state> 


UPDATE
對於我建議使用Spring的表單標記庫的視圖。定義標籤庫

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

<form:form method="POST" action="${flowExecutionUrl}&_eventId=next" modelAttribute="customerDto"> 
    <table> 
    <tr> 
     <td><form:label path="customer.name">Name</form:label></td> 
     <td><form:input path="customer.name" /></td> 
    </tr> 
    <tr> 
     <td><form:label path="customer.email">Email</form:label></td> 
     <td><form:input path="customer.email" /></td> 
    </tr> 
    <tr> 
     <td><form:label path="address.addressLine1">Address Line 1</form:label></td> 
     <td><form:input path="address.addressLine1" /></td> 
    </tr> 
    <tr> 
     <td colspan="2"> 
      <input type="submit" value="Submit"/> 
     </td> 
    </tr> 
    </table> 
</form:form> 
+0

替換形式,在你的JSP什麼的一組語句做,他們爲什麼需要? –

+0

@varunsinghal'set'語句將'value'表達式結果設置爲'name'中定義的屬性。 ''表示'customerDto.setCustomer(cust)'。這兩套都需要將客戶和地址設置到DTO中,以便在jsp中引用它們。 – Evgeny

+0

有一個疑問,我有一個buisness服務,它將customerModel作爲輸入參數,然後將該輸入參數插入到customer表中。我將如何實現這一目標?我可以這樣做:cusomerBuisness.createCustomer(customerDto.cust)?請幫忙 ! –

相關問題