有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在SpringWebFlow中将多个模型绑定到一个视图?

情况:我正在开发一个spring MVC webapp,并使用spring web flow和tiles框架。我在DBcustomercustomerAdress中有两个表,我为它们命名了两个模型类customerModelcustomerAdressModel

现在在我的流程中。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将视图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。我该怎么做,我查了spring DOC但什么都找不到,请帮忙

注意:我不能修改我的sql表


共 (1) 个答案

  1. # 1 楼答案

    可以创建复合模型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 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>