又见The request sent by the client was syntactically incorrect ()

前几天遇到过这个问题(Ref:http://www.cnblogs.com/xiandedanteng/p/4168609.html),问题在页面的组件name和和注解的@param名匹配不对,这个好解决,一一对好就行了。

但是,这回情况不一样了,我的页面控件是类似这样的:

<p style="height:280px;display:block;">
    <span class="req">
        <label><input type="checkbox" value="A" name="to" />&nbsp;A</label>
        <label><input type="checkbox" value="B" name="to" />&nbsp;B</label>
        <label><input type="checkbox" value="C" name="to" />&nbsp;C</label>
    </span>
    <label><span></span></label>
</p>

而控制器是这样写的:

@RequestMapping(value="/sendEmailReport")
    public String sendEmailReport(@RequestParam("idTxt") String id,
                                  @RequestParam("to")  String[] to,
                                  @RequestParam("cc")  String[] cc,
                                  @RequestParam("bcc") String[] bcc,
                                  HttpServletRequest request,
                                  HttpServletResponse response){
。。。
}

看,to部分对应一点没错,但是,问题来了,如果name为to的一组复选框一个都没有选中的话,那么,提交页面后就会报The request sent by the client was syntactically incorrect ()错误。

但是,如果哪怕只要选中一个,程序就正常运行了。

我是通过添加一个默认的隐藏的选中复选框来避免这个错误的,代码如下:

<p style="height:280px;display:block;">
    <span class="req">
        <label><input type="checkbox" value="A" name="to" />&nbsp;A</label>
        <label><input type="checkbox" value="B" name="to" />&nbsp;B</label>
        <label><input type="checkbox" value="C" name="to" />&nbsp;C</label>
        <label><input type="checkbox" value="" checked name="to" style="display:none;"/></label>
    </span>
    <label><span></span></label>
</p>

这样,这组复选框就不必非要选中一个了,当然,后台需要添加点过滤措施。

应该是SpringMVC自身的问题,希望它能修正这个Bug。

原文地址:https://www.cnblogs.com/heyang78/p/4189489.html