简体   繁体   English

如何在springMVC中获取选定复选框的列表

[英]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 和StudentForm添加信息 在此处输入图片说明 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 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() . 检查stu.getListSubject() All the checked items will populated to List by Spring MVC. 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. 而且您的studentView.jsp文件中有错误。 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 ${} . 您错过了$ {}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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