如何在弹簧网络流程中将多个模型绑定到单个视图?

时间:2016-04-07 13:59:26

标签: java spring jsp spring-mvc spring-webflow

情况:我正在开发一个spring MVC webapp并使用spring web flow和tiles框架。我在数据库customercustomerAdress中有两个表,我有两个名为customerModelcustomerAdressModel的模型类。

现在在我的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的{​​{1}}。我怎么做到这一点,我经历了春季DOC但无法找到任何东西,请帮忙!

注意:我无法修改我的sql表

1 个答案:

答案 0 :(得分: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>

<小时/> 的更新
对于观点,我建议使用Spring的表单标记库。定义taglib

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

并用

替换jsp中的表单
<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>