2016-04-07 12 views
0

状況:私は春のMVC Webアプリケーションを開発しており、春のWebフローとタイルフレームワークを使用しています。私はDB customercustomerAdressの2つのテーブルを持っており、私はcustomerModelcustomerAdressModelという2つのモデルクラスを持っています。春のWebフローで複数のモデルを単一のビューにバインドする方法はありますか?

は今私の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.jspを充当するビューcustomerを解決します

<%@ 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のプロパティの値を含む入力フィールドをいくつか持っています。バインドしたいですか同じビュー状態customerViewStateにはとcustomerAdressModelが含まれています。どのように私はそれを行う、私は春のDOCを通過したが、何かを見つけるcouldnt、助けてください!

注:私は私の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"%> 

を定義し、SETステートメントが何をしているか

<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) 'を意味します。どちらのセットも、顧客と住所をjspで参照するためにDTOに設定する必要があります。 – Evgeny

+0

1つの疑問は、私は入力パラメータとしてcustomerModelを受け取り、その入力パラメータをcustomerテーブルに挿入するビジネスサービスを持っています。どうすればここでそれを達成できますか?私はこのようにすることができます:cusomerBuisness.createCustomer(customerDto.cust)?助けてください ! –

関連する問題