spring:bind 使用中一个 值追加 问题

在使用spring:bind 配合 SimpleFormController时,出了一个让我困惑的问题:
获取Form表单的请求url是:

<a href="visitForm.htm?customerId=${contact.customerId}&contactId=${contact.id}&communicateMode=2">
${contact.businPhone}
</a>


在Controller中

public class VisitFormController extends BaseCrmFormController
{
protected Object formBackingObject( HttpServletRequest request) throws Exception {

CrmVisit visit = new CrmVisit();

if(request.getParameter( "id")!=null){//修改
int id=Integer.parseInt( request.getParameter( "id"));
visit = this.crmService.getVisit( id);

else{//新建
int customerId = ServletRequestUtils.getRequiredIntParameter( request,"customerId");
int communicateMode = ServletRequestUtils.getRequiredIntParameter( request,"communicateMode ");
int[] contactIds = ServletRequestUtils.getRequiredIntParameters( request,"contactId");


visit.setCustomerId( customerId);
visit.setCommunicateMode(communicateMode);

}

return visit;
}

protected void onBind( HttpServletRequest httpServletRequest, Object object, BindException bindException ) throws Exception
{
super.onBind( httpServletRequest, object, bindException );
}

protected ModelAndView onSubmit(
HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)
throws Exception {

CrmVisit visit = (CrmVisit)command;
this.crmService.saveVisit( visit);//持久化

return new ModelAndView( getSuccessView(),"visit",visit);
}
}

下面是Form.jsp片段

<form:form name=f1 method=post >
......
<td class="fieldnamecenter">沟通方式</td>
<td class="fieldvalue" colspan="2"><spring:bind path="command.communicateMode"
//...此段略 设定本地变量 communicateTypes代码...
<c:forEach items="${communicateTypes}" var="communicateType">
<input type="radio" name="${status.expression}" value="${communicateType.value}" <c:if test="${status.value == communicateType.value}">checked="checked"</c:if> />${communicateType.name}
</c:forEach>
</spring:bind>
</td>
......
</form:form>


此时如果提交后 发现
visit.communicateMode 的值 不是所期望的值.
因为之前第一个url 带有参数communicateMode=2 。而这个url又被自动传递给 form.jsp表单,作为这个表单 <form:form name=f1 ...>action的值得;并且,表单中又绑定了一个name=communicateMode单选框的。此时,spring的onbind(request,object) 会获取所有communicateMode参数的值,并以字符串追加形式 赋给visit.communicateMode...即 单选择框值为1时候 赋值 21 ;单选择框值为2时候 赋值 22;单选择框值为3时候 赋值 23 ...

解决办法:
偶目前就是直接把之前的url参数改个名,communicateMode 改成mode;
即:

<a href="visitForm.htm?customerId=${contact.customerId}&contactId=${contact.id}&mode=2">
${contact.businPhone}
</a>

不知道是不是有其他办法解决这种无意间的冲突...???


另:spring:bind + SimpleFormController
submit表单后 依然还是要走formBackingObject()方法走
(把 command 对象存储模式改为 session 即可...
    this.setSessionForm( true);
)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值