开发者

Multiple bean binding in Spring?

开发者 https://www.devze.com 2023-03-21 15:22 出处:网络
Suppose I want to fill two or three different beans with Spring 3 开发者_开发知识库mvc how do I do that given that <form:form> only supports one modelAttribute? And what if I have some nested be

Suppose I want to fill two or three different beans with Spring 3 开发者_开发知识库mvc how do I do that given that <form:form> only supports one modelAttribute? And what if I have some nested beans?

Obviously I don't want to create a "BIG" bean that then I use to fill'em all...


I think the simplest solution is create a new command class and use object composition.

public class MyCommandClass implements Serializable {
   private MyFirstBean myFirstBean;
   private MySecondBean mySecondBean;
   // other beans, getters and setters, etc.
}

Your form:

<form:form modelAttribute="myCommand">
<form:input path="myFirstBean.someProperty" ... />
<form:input path="mySecondBean.someOtherProp" ... />
</form:form>
0

精彩评论

暂无评论...
验证码 换一张
取 消