Spring MVC遭遇checkbox的问题解决方式
Spring MVC遭遇checkbox的問題是:當(dāng)checkbox全不選時(shí)候,則該checkbox域的變量為null,不能動(dòng)態(tài)綁定到spring的controller方法的入?yún)⑸?#xff0c;并拋出異常。
解決方式:
1、javascript方式提交,提交前拼提交參數(shù)串。拼完后通過ajax方式提交。能夠使用controller請(qǐng)求參數(shù)綁定。
? ?缺點(diǎn):逐個(gè)提取表單參數(shù),并對(duì)checkbox選項(xiàng)參數(shù)進(jìn)行推斷拼裝(字符切割)。終于提交到后臺(tái)太麻煩。
??
2、加入checkbox的同名隱藏域,從而使提交過去數(shù)據(jù)永不為null。這樣就能夠使用controller請(qǐng)求參數(shù)綁定了。
| 1 2 3 4 | <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的方法:
| 1 2 3 4 5 6 7 | @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) { |
test參數(shù)的值為:
全不選時(shí)候:""
選兩個(gè):"2,3,"
3、使用spring的標(biāo)簽,我不會(huì)用,我也不想會(huì),由于表單非常多時(shí)候都是用js寫的,而非html。不希望mvc入侵太深。
綜合對(duì)照:方案2是首選。簡(jiǎn)單易行。
轉(zhuǎn)載于:https://www.cnblogs.com/mfrbuaa/p/5184119.html
總結(jié)
以上是默认站点為你收集整理的Spring MVC遭遇checkbox的问题解决方式的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦到屋外刮大风是什么意思
- 下一篇: Spring DI模式 小样例