简体   繁体   中英

How to get list of selected checkbox in springMVC

Currently, I want to get list of selected checkbox from a table. and I tried to have a sample code as below :

public class Student {

public List<String> listSubject;

public List<String> getListSubject() {
    return listSubject;
}

public void setListSubject(List<String> listSubject) {
    this.listSubject = listSubject;
}

private int id;
private String name;
private int age;
public boolean single;

public boolean isSingle() {
    return single;
}

public void setSingle(boolean single) {
    this.single = single;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}

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

public Student(List<String> listSubject, int id, String name, int age,
        boolean single) {
    super();
    this.listSubject = listSubject;
    this.id = id;
    this.name = name;
    this.age = age;
    this.single = single;
}

}

And the blow is controller 在此处输入图片说明

and StudentForm to add information 在此处输入图片说明 after selected checkbox from form, I want to display all result to a view :

在此处输入图片说明

But until now, I still can't controller adding selected value into a listofSubject which I created for a student.

THe blow is the link of sample code which I am implementing :

https://dl.dropboxusercontent.com/u/11576807/spring-mvc-example.zip

Besides, I want to use a tag instead of submit button to redirect to result page. And the system only allow user to select two options, at that time, the remain checkbox will be disabled. Can you please share with me your solution in this case ?

Please tell me know the way to do it with the sample above. Thanks

You already found the answer. Check stu.getListSubject() . All the checked items will populated to List by Spring MVC. Your controller should look like this.

@RequestMapping(value = "/student/add", method = RequestMethod.POST)
public String addStudent(Student stu, ModelMap model){

    for (String s: stu.getListSubject()) {
        //You can see values populated
        System.out.println("string: " + s);
    }
    model.addAttribute("name",stu.getName());
    model.addAttribute("age", stu.getAge());
    model.addAttribute("single", stu.isSingle());
    model.addAttribute("listSubject", stu.getListSubject());
    return "studentView";
}

And you have error in your studentView.jsp file. Instead of this

<c:forEach items="listSubject" var="subject">
    <td>${subject}</td>
</c:forEach>

use this:

<c:forEach items="${listSubject}" var="subject">
    <td>${subject}</td>
</c:forEach>

You missed ${} .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM