Controller方法形参接收checkbox的值,既可以用String,也可以用String[]。

字符串数组接收的测试代码如下:

@Controller
@RequestMapping("/mycontroller")
public class MyController { @RequestMapping(method = RequestMethod.GET)
public String form() {
return "mycontroller";
} @RequestMapping(method = RequestMethod.POST)
public String form1(@RequestParam("interest") String[] interest, Model model) {
String a = Arrays.toString(interest);
model.addAttribute("ins", a);
return "ok";
} }

  测试页面如下:

/WEB-INF/jsp/mycontroller.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>请选择</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/mycontroller.action"
method="post">
<input type="checkbox" name="interest" value="a1" />a1<br />
<input type="checkbox" name="interest" value="a2" />a2<br />
<input type="checkbox" name="interest" value="a3" />a3<br />
<input type="checkbox" name="interest" value="a4" />a4<br />
<input type="checkbox" name="interest" value="a5" />a5<br /> <input type="submit"/>
</form>
</body> </html>

  

结果显示页面如下:

/WEB-INF/jsp/ok.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>操作成功</title>
</head>
<body> 选择的内容是:${ins}<br/>
</body> </html>

  结果:

字符串接收的测试代码如下(测试完数组接收后 修改即可):

@Controller
@RequestMapping("/mycontroller")
public class MyController { @RequestMapping(method = RequestMethod.GET)
public String form() {
return "mycontroller";
} @RequestMapping(method = RequestMethod.POST)
public String form1(@RequestParam("interest") String interest, Model model) {
model.addAttribute("ins", interest); return "ok";
} }

  结果是:

小知识:

  如果checkbox都留空(不选择),那么Controller会报错。解决办法:①前端js判断;②前端加一个hidden的checkbox。

补充方法:

@RequestParam(value = "interest", required = false)

  

SpringMVC接收checkbox传值的更多相关文章

  1. SpringMVC接收前端传值有哪些方式?

    有很多种,比如: 1.通过@RequestParam注解接收请求参数: 2.通过Bean封装,接收多个请求参数 3.通过@ModelAttribute绑定接收前端表单数据 4.通过@PathVaria ...

  2. springmvc用model传值到jsp页面,el表达式引用接收不到传递过来的值

    springmvc用model传值到jsp页面,el表达式引用接收不到传递过来的值 查看下controller层代码可以发现,写的是没有错误的. @RequestMapping("list. ...

  3. springMVC 接收数组参数,mybatis 接收数组参数,mybatis批量插入/批量删除案例

    案例是给一个用户赋予多个权限,多个权限用其对应的主键 id 为参数,组成了 一个id数组,传给springMVC,然后springMVC传给mybatis,然后mybatis批量插入.其实类似的场景还 ...

  4. SpringMVC 接收复杂对象

    要发送的数据为:String topicId,String topicName,String summarize,List<ModuleParam> parentList 前端页面ajax ...

  5. Struts2接收checkbox的值

    Struts2接收checkbox的值:   HTML: <input type="checkbox" name="ssl" value="B1 ...

  6. SpringMVC接收页面表单参数-java-电脑编程网

    body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI ...

  7. ajax传数组到后台,后台springmvc接收数组参数

    var   ids= new Array();  $("input[class='detailCheck']:checked").each(function(i,k){   var ...

  8. SpringMVC接收复杂对象

    SpringMVC接收复杂对象 转载请注明地址:http://www.cnblogs.com/funnyzpc/p/7642977.html 本节内容暂放一边,我先扯点儿,心情好了,代码顺风顺水哈~ ...

  9. springmvc接收前台(如ajax)传来的数组list,set等图文详解

        ref:https://blog.csdn.net/wabiaozia/article/details/50803581 前言: 相信很人都被springmvc接收数组问题折磨过,查过几个解决 ...

随机推荐

  1. 重构实践——为了try-catch大兴排场

    可能是我们共同的强迫症,不要说看到,就算想到太多的try-catch也很难接受. 于是,开始了一些尝试,这些尝试都算是思维的锻炼.场面的见识.经验的积累. Version1 —— 原始版本 一开始,在 ...

  2. How Many Trees?[HDU1130]

    How Many Trees? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  3. AMPPZ2014

    [AMPPZ2014]The Lawyer 记录每天结束的最早的会议以及开始的最晚的会议即可. #include<cstdio> #define N 500010 int n,m,i,d, ...

  4. Quest Central for DataBase 5.0.1,6.1 (软件+注册)

    找寻了多天,终于找到了,记录下,以后重装用.输入所有组件的licenses后,提示要注册,我选择了Canada,Google了一个地方的PostCode和phone number,填写,注册成功! 软 ...

  5. ThinkPHP框架的部署

    1.将ThinkPHP框架的框架文件放到想要放置的地方,与创建的应用文件夹同级 2.vhost文件中设置虚拟目录 3.在hosts文件中配置 4.在应用目录中创建入口文件index.php 5.在入口 ...

  6. 手动编译安装lanmp centos6.5 64位

    对于新手来说一个很大的问题就是连源码包都在到在哪下载,还有就是软件的依赖关系 如果网卡也不会配置,请翻看我的其他文章 这就是基本所需的源码包了 http://pan.baidu.com/s/1kTxb ...

  7. myBatis应用

    最近项目中使用myBatis(iBatis),所以目前对所遇的一些问题及有些模糊的地方在这里标注一下. 首先mybaits是一个“半自动化”的ORM框架. 需要使用的jar包:mybatis-3.0. ...

  8. 用wget下载文件

    wget使用文档:https://www.gnu.org/software/wget/manual/wget.html 最开始常用的比如: wget -O  /e/movie.mp4 http://w ...

  9. HDR the Bungie Way PPT and demo from MSFT

    Gamefest 2006 - HDR the Bungie Way Posted By: Falagard <falagard@hotmail.com> Date: 4/26/07 9: ...

  10. userDefaults

    // // RootViewController.m #import "RootViewController.h" @interface RootViewController () ...