Spring MVC遭遇checkbox的问题解决方案

 2023-09-15 阅读 18 评论 0

摘要:Spring MVC遭遇checkbox的问题是:当checkbox全不选时候,则该checkbox域的变量为null,不能动态绑定到spring的controller方法的入参上,并抛出异常。解决方案:1、javascript方式提交,提交前拼提交参数串,拼完后通过ajax方式提

Spring MVC遭遇checkbox的问题是:当checkbox全不选时候,则该checkbox域的变量为null,不能动态绑定到spring的controller方法的入参上,并抛出异常。


解决方案:

1、javascript方式提交,提交前拼提交参数串,拼完后通过ajax方式提交。可以使用controller请求参数绑定。


java.lang.reflect.undeclared、    缺点:逐个提取表单参数,并对checkbox选项参数进行判断拼装(字符分割),最终提交到后台太麻烦。

 

2、添加checkbox的同名隐藏域,从而使提交过去数据永不为null。这样就可以使用controller请求参数绑定了。

<input type="checkbox"  name="test" value="1"/>aaa
<input type="checkbox"  name="test" value="2"/>bbb
<input type="checkbox"  name="test" value="3"/>ccc
<input type="hidden" name="test"/>


提交到controller的方法:

@RequestMapping("/test")
private String LoginAction(HttpServletRequest request,HttpServletResponse response,@RequestParam(value = "username") String username,@RequestParam(value = "password") String password,@RequestParam(value = "test") String test,@RequestParam(value = "yzm") String yzm) {


maven本地仓库有jar还是报错?test参数的值为:

全不选时候:""

选两个:"2,3,"


3、使用spring的标签,我不会用,我也不想会,因为表单很多时候都是用js写的,而非html。不希望mvc***太深。


jdbc connection will not be?综合对比:方案2是首选。简单易行。



如果你发现更简单的处理方式,也别忘了回复告诉我。


版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/56101.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息