Java 如何在SpringWebFlow中将多个模型绑定到单个视图?
情况:我正在开发一个SpringMVCwebapp,并使用SpringWebFlow和tiles框架。我在DBJava 如何在SpringWebFlow中将多个模型绑定到单个视图?,java,spring,jsp,spring-mvc,spring-webflow,Java,Spring,Jsp,Spring Mvc,Spring Webflow,情况:我正在开发一个SpringMVCwebapp,并使用SpringWebFlow和tiles框架。我在DBcustomer和CustomerAddress中有两个表,我有两个模型类,分别命名为customerModel和CustomerAddressModel 现在,在my flow.xml中,我有以下视图状态: <var name = "cust" class = "com.model.CustomerModel"/> <view-state id = "custom
customer
和CustomerAddress
中有两个表,我有两个模型类,分别命名为customerModel
和CustomerAddressModel
现在,在my flow.xml中,我有以下视图状态
:
<var name = "cust" class = "com.model.CustomerModel"/>
<view-state id = "customerViewState" view = "customer" model = "cust">
<transition on="next" to="customerData"/>
</view-state>
问题:现在customer.jsp
中指定的表单有一些输入字段,其中包含customerAddressModel
的属性值。因此,我想将customerModel
以及customerAddressModel
绑定到相同的视图状态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>
名称
电子邮件
地址行1
set语句在做什么,为什么需要它们?@varunsinghalset
statement将value
表达式结果设置为name
中定义的属性<代码>表示customerDto.setCustomer(cust)
。这两个集合都需要将customer和address设置到DTO中,以便在jsp中引用它们。毫无疑问,我有一个business服务,它将customerModel作为输入参数,然后将该输入参数插入customer表中。我如何在这里做到这一点?我可以这样做:cusomerBuisness.createCustomer(customerDto.cust)?请帮忙@varunsinghal是的,您可以。我有点困惑如何在JSP中引用customerModel
的customerModel
变量,然后将此变量作为输入传递给customerModel
业务服务,如果您能对此有所了解,这将非常有用
<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>