Spring——checkbox && checkboxes标签的使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/coder_gwr/article/details/87474247

checkbox使用

registerForm1.jsp

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="tr" uri="http://www.springframework.org/tags/form" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>注册</title>
</head>
<body>
<form:form modelAttribute="user" method="post" action="">
    <table>
        <tr>
            <td>选择课程:</td>
            <td>
                <form:checkbox path="courses" value="JAVAEE" label="JAVAEE"/>
                <form:checkbox path="courses" value="Mybatis" label="Mybatis"/>
                <form:checkbox path="courses" value="Spring" label="Spring"/>
            </td>
        </tr>
    </table>
    <form:checkbox path="reader"/>已经阅读相关协议
</form:form>
</body>
</html>

CheckBoxController

package com.wen.controller;

import com.wen.domain.User111;
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 java.util.ArrayList;
import java.util.List;

@Controller
public class CheckBoxController {
    @RequestMapping(value = "/registerForm1", method = RequestMethod.GET)
    public String registerForm1(Model model){
        User111 user111=new User111();
        user111.setReader(true);
        List<String> list=new ArrayList<>();
        list.add("JAVAEE");
        list.add("Spring");
        user111.setCourses(list);
        model.addAttribute("user",user111);
        return "registerForm1";
    }
}

CheckBoxes的使用

CheckBoxesController

package com.wen.controller;

import com.wen.domain.User111;
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 java.util.ArrayList;
import java.util.List;

@Controller
public class CheckBoxesController {
    @RequestMapping(value = "/checkboxesForm",method = RequestMethod.GET)
    public String registerForm222(Model model){
        User111 user111=new User111();
        List<String> list=new ArrayList<>();
        list.add("JAVAEE");
        list.add("Spring");
        user111.setCourses(list);
        List<String> courseList=new ArrayList<>();
        courseList.add("JAVAEE");
        courseList.add("Mybatis");
        courseList.add("Spring");
        model.addAttribute("user",user111);
        model.addAttribute("courseList",courseList);
        return "checkboxesForm";
    }
}

checkboxesForm.jsp

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form:form modelAttribute="user">
    <table>
        <tr>
            <td>选择课程:</td>
            <td>
                <form:checkboxes path="courses" items="${courseList}"/>
            </td>
        </tr>
    </table>
</form:form>
</body>
</html>

预览图
我们可以看到label和value的值一致,
那么,如果我们需要让checkboxes的值不一致,我们需要怎么做呢?

我们可以使用Map作为数据源~

Map作为checkboxes的items属性的数据源时,Map集合的key将作为复选框的value,Map集合的value将作为label进行显示。

判断items,Map中是否含有对应的key来决定当前的复选框是否处于选中状态。

CheckBoxes2Controller

package com.wen.controller;

import com.wen.domain.User111;
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 java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class CheckBoxes2Controller {
    @RequestMapping(value = "/checkboxesForm2", method = RequestMethod.GET)
    public String checkboxesForm2(Model model) {
        User111 user111=new User111();
        List<String> list=new ArrayList<>();
        list.add("1");
        list.add("3");
        user111.setCourses(list);
        Map<String,String> courseMap=new HashMap<>();
        courseMap.put("1","JAVAEE");
        courseMap.put("2","Mybatis");
        courseMap.put("3","Spring");
        model.addAttribute("user",user111);
        model.addAttribute("courseMap",courseMap);
        return "checkboxesForm2";
    }
}

预览图
这样我们发现,现在的input中的value就是map中的key了~

当我们使用Array或者集合作为数据源的使用,且里面的元素都是一个domain对象时,还可以使用checkboxes标签的itemLabel和itemValue属性来表示。

CheckBoxes3Controller

package com.wen.controller;

import com.wen.domain.Dept;
import com.wen.domain.Employee;
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 java.util.ArrayList;
import java.util.List;

@Controller
public class CheckBoxes3Controller {
    @RequestMapping(value = "/checkboxesForm3", method = RequestMethod.GET)
    public String checkboxesForm2(Model model) {
        //页面checkbox复选框这一项会被选中
        Employee employee = new Employee();
        Dept dept = new Dept(1, "开发部");
        List<Dept> list = new ArrayList<>();
        list.add(dept);
        employee.setDepts(list);
        //页面展现的可供选择的复选框内容
        List<Dept> deptList = new ArrayList<>();
        deptList.add(dept);
        deptList.add(new Dept(2, "销售部"));
        deptList.add(new Dept(3, "财务部"));
        model.addAttribute("employee", employee);
        model.addAttribute("deptList", deptList);
        return "checkboxesForm3";
    }
}

checkboxesForm3.jsp

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form:form modelAttribute="employee">
    <table>
        <tr>
            <td>
                选择部门:
            </td>
            <td>
                <form:checkboxes path="depts" items="${deptList}" itemLabel="name" itemValue="id"/>
            </td>
        </tr>
    </table>
</form:form>
</body>
</html>

预览图
我们可以看到,Dept对象的name就是label标签,id就是input的value值~

猜你喜欢

转载自blog.csdn.net/coder_gwr/article/details/87474247