Spring MVC框架checkboxes标签的三种使用方式

这篇博客详细介绍了Spring MVC框架中checkboxes标签的三种用法,包括基本使用、自定义属性设置以及结合后台数据操作的方法。通过实例代码解析,帮助读者掌握在Spring MVC项目中如何有效利用checkboxes进行表单数据处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 代码:

checkboxesForm.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>测试checkboxes标签</title>
</head>
<body>
<h3>form:checkboxes测试</h3>
<form:form modelAttribute="user" method="post" action="checkboxesForm">
   <table>
      <tr>
         <td>选择课程:</td>
         <td>
            <form:checkboxes items="${courseList}" path="courses"/>
         </td>
      </tr>
   </table>
</form:form>
</body>
</html>

checkboxesForm2.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>测试checkboxes标签</title>
</head>
<body>
<h3>form:checkboxes测试</h3>
<form:form modelAttribute="user" method="post" action="checkboxesForm2" >
	<table>
		<tr>
			<td>选择课程:</td>
			<td>
				<form:checkboxes items="${courseMap}" path="courses"/>
			</td>
		</tr>
	</table>
</form:form>
</body>
</html>

checkboxesForm3.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>测试checkboxes标签</title>
</head>
<body>
<h3>form:checkboxes测试</h3>
<form:form modelAttribute="employee" method="post" action="checkboxesForm3" >
	<table>
		<tr>
			<td>选择部门:</td>
			<td>
				<form:checkboxes items="${deptList}" path="depts" 
					itemLabel="name" itemValue="id"/>
			</td>
		</tr>
	</table>
</form:form>
</body>
</html>

User.java

package com.bean;

import java.io.Serializable;
import java.util.List;

public class User implements Serializable {
	private List<String> courses;

	public User() {
		super();
		// TODO Auto-generated constructor stub
	}

	public List<String> getCourses() {
		return courses;
	}

	public void setCourses(List<String> courses) {
		this.courses = courses;
	}
	
}

Dept.java

package com.bean;

public class Dept {
	private Integer id;
	private String name;
	public Dept() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Dept(Integer id, String name) {
		super();
		this.id = id;
		this.name = name;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
}

Employee.java

package com.bean;

import java.util.List;

public class Employee {
	private List<Dept> depts;

	public List<Dept> getDepts() {
		return depts;
	}

	public void setDepts(List<Dept> depts) {
		this.depts = depts;
	}
	
}

UserController.java

package com.control;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.bean.Dept;
import com.bean.Employee;
import com.bean.User;

@Controller
public class UserController {
	/*
	 * 第一种方式
	 */
	@RequestMapping(value="/checkboxesForm",method=RequestMethod.GET)
	public String registerForm(Model model){
		User user=new User();
		// 为集合变量courses添加“JAVAEE”和“Spring”,页面的checkbox复选框这两项会被选中
		List<String> list = new ArrayList<String>();
		list.add("JAVAEE");
		list.add("Spring");
		user.setCourses(list);
		// 页面展现的可供选择的复选框内容courseList
		List<String> courseList = new ArrayList<String>();
		courseList.add("JAVAEE");
		courseList.add("Mybatis");
		courseList.add("Spring");
		// model中添加属性user和courseList
		 model.addAttribute("user",user);
    	 model.addAttribute("courseList",courseList);
	     return "checkboxesForm";
	}
	
	/*
	 * 第二种方式
	 */
	@RequestMapping(value="/checkboxesForm2",method=RequestMethod.GET)
	public String registerForm2(Model model){
		User user=new User();
		// 为集合变量courses添加“JAVAEE”和“Spring”,页面的checkbox复选框这两项会被选中
		List<String> list = new ArrayList<String>();
		list.add("1");
		list.add("3");
		user.setCourses(list);
		// 页面展现的可供选择的复选框内容courseList
		Map<String, String> courseMap = new HashMap<String, String>();
		 courseMap.put("1","JAVAEE");
		 courseMap.put("2","Mybatis");
		 courseMap.put("3","Spring");
		 // model中添加属性user和courseList
    	 model.addAttribute("user",user);
    	 model.addAttribute("courseMap",courseMap);
	     return "checkboxesForm2";
	}
	
	/*
	 * 第三种方式
	 */
	 @RequestMapping(value="/checkboxesForm3",method=RequestMethod.GET)
	 public String registerForm3(Model model) {
		 Employee employee = new Employee();
		 Dept dept = new Dept(1,"开发部");
		 // 为集合变量depts添加Dept对象,该对象id=1,name=开发吧,页面的checkbox复选框这一项会被选中
		 List<Dept> list = new ArrayList<Dept>();
		 list.add(dept);
		 employee.setDepts(list);
		 // 页面展现的可供选择的复选框内容deptList
		 List<Dept> deptList = new ArrayList<Dept>();
		 deptList.add(dept);
		 deptList.add(new Dept(2,"销售部"));
		 deptList.add(new Dept(3,"财务部"));
		 // model中添加属性employee和deptList
    	 model.addAttribute("employee",employee);
    	 model.addAttribute("deptList",deptList);
	     return "checkboxesForm3";
	 }
}

截图:






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值