ITeye博客

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

yanglei008 · 2008-07-29 13:42 · spring · Markdown

在使用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);

)

评论(1)

  1. #1 楼 · yanglei008 · 2008-09-26 09:37

    该问题是 setSessionForm(true) 就ok了... 把 bind的 对象放在session中...

评论功能维护中,当前仅展示历史评论。