使 Spring MVC tags 支持多 model

问题解释:用过 Spring MVC tags 的朋友都知道,要想将 表单元素的 value 与 相应的 bean 属性对应,需要在 <form:form> 里设置 commandName,spring mvc 会根据这个值在 requestContext 或 sessionContext 里面查找相应的 model,但是这样会是每个 <form:form> 表单只能使用一个 model,这降低了使用的灵活性。

 

其实导致这一问题的原因是 spring mvc 根据 form tag 中 path 属性查找 bean 及其属性的算法。

 

在 spring mvc 的 AbstractDataBoundFormElementTag.java (名字真长)中的 getBindStatus 方法中有这么一句:

String pathToUse = (nestedPath != null ? nestedPath + getPath() : getPath());

 其中的 nestedPath 默认为 "command.",就是默认的 commandName 加上一个点。如果设置了 commandName,nestedPath 的值就是相应的 commandName 加上一个点。而 path 的指就是相应 tag 中的 path 属性(像 input、select 等 tag 都是继承了这个 tag)。所以如果 nestedPath 不为空的话 pathToUse 就是 commandName.path。之后 spring mvc 就会根据 pathToUse 的值将 tag 的 value 和相应的 bean 的属性进行绑定。知道这一点我想你就知道如何是 spring mvc tag 支持多 model 了。

 

具体怎么做这里就不说了。懒了,呵呵。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值