简体   繁体   English

如何在Spring Web Flow中将多个模型绑定到单个视图?

[英]how to bind multiple models to a single view in spring web flow?

Situation : I am developing a spring MVC webapp and using spring web flow and tiles framework . 现状:我正在开发一个Spring MVC Webapp,并使用Spring Web Flow和Tile框架。 I have two tables in DB customer and customerAdress and i have two model classes for them named customerModel and customerAdressModel . 我在DB customercustomerAdress有两个表,我有两个模型类,分别为customerModelcustomerAdressModel

Now in my flow.xml i have the following view-state : 现在在我的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>

Tile framework resolves the view customer to appropriate customer.jsp which is shown below : Tile框架将视图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>

Problem : Now the form specified in customer.jsp has some input fields that contains value of properties of customerAdressModel .Thus i want to bind customerModel as well as customerAdressModel to the same view state customerViewState . 问题:现在在customer.jsp中指定的表单具有一些输入字段,其中包含customerAdressModel的属性值。因此我想将customerModelcustomerAdressModel绑定到相同的视图状态customerViewState How do i do that , i went through spring DOC but couldnt find anything , please help ! 我该怎么做,我通过了春季DOC,但找不到任何东西,请帮忙!

Note : i cant modify my sql tables 注意:我无法修改我的SQL表

You can create composite model DTO 您可以创建复合模型DTO

public class CompositeModelDto {

    private CustomerModel suctomer;

    private CustomerAddressModel address;

    //setters ang getters ...

}

And use it as a view state model 并将其用作视图状态模型

<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 更新
For views I'd suggest using Spring's form tag library. 对于视图,我建议使用Spring的表单标签库。 Define taglib 定义标签库

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

and replace form in your jsp with 并替换为您的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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM