Spring MVC的checkboxes标签使用

一 领域模型

1 User

package org.fkit.domain;

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


// 域对象,实现序列化接口
public class User implements Serializable{
    
    private static final long serialVersionUID = 1L;
    
    private List<String> courses;
    
    public User() {
        super();
    }

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

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

2  Employee 

package org.fkit.domain;

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


public class Employee implements Serializable{

    private static final long serialVersionUID = 1L;
    
    private List<Dept> depts;

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

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

    
    
    
}

3 Dept

package org.fkit.domain;
import java.io.Serializable;

public class Dept implements Serializable{
     
     private static final long serialVersionUID = 1L;
     
     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;
     }
     
     
}

二 控制器

package org.fkit.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.fkit.domain.Dept;
import org.fkit.domain.Employee;
import org.fkit.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;


/**
*  HelloWorldController是一个基于注解的控制器,
*  可以同时处理多个请求动作,并且无须实现任何接口。
*  org.springframework.stereotype.Controller注解用于指示该类是一个控制器
*/
@Controller
public class UserController{
     
     @GetMapping(value="/checkboxesForm")
     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";
     }
     
     @GetMapping(value="/checkboxesForm2")
     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";
     }
     
     @GetMapping(value="/checkboxesForm3")
     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";
     }
}

三 配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:p="http://www.springframework.org/schema/p"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
     xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd     
        http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd">
        
    <!-- spring可以自动去扫描base-pack下面的包或者子包下面的java文件,
      如果扫描到有Spring的相关注解的类,则把这些类注册为Spring的bean  -->
    <context:component-scan base-package="org.fkit.controller"/>
    <!-- 默认配置方案 -->
    <mvc:annotation-driven/>
     <!-- 静态资源处理 -->
    <mvc:default-servlet-handler/>
    <!-- 视图解析器  p:prefix属性表示前缀  p:suffix 表示后缀  -->
     <bean id="viewResolver"
           class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/content/" p:suffix=".jsp"/>
    
</beans>

四 视图

1 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>

2  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>

3 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>

五 测试

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值