如何将多个模型绑定到 spring 网络流中的单个视图?
how to bind multiple models to a single view in spring web flow?
情况: 我正在开发一个 spring MVC webapp 并使用 spring web flow 和 tiles framework。我在数据库 customer
和 customerAdress
中有两个表,我有两个模型 类 分别命名为 customerModel
和 customerAdressModel
.
现在在我的 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 框架将视图 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表
您可以创建复合模型 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>
情况: 我正在开发一个 spring MVC webapp 并使用 spring web flow 和 tiles framework。我在数据库 customer
和 customerAdress
中有两个表,我有两个模型 类 分别命名为 customerModel
和 customerAdressModel
.
现在在我的 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 框架将视图 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表
您可以创建复合模型 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>