0

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 enter image description here

and StudentForm to add information enter image description here after selected checkbox from form, I want to display all result to a view :

enter image description here

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

1 Answer 1

1

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 ${} .

Sign up to request clarification or add additional context in comments.

5 Comments

Currently, I want to use a <a/> tag instead of submit button to redirect to result page. Can you please share with me your idea in this case ?
In that case you should use some jQuery to catch the click event and manually submit the form.
Thanks Guneri, do you have any sample for this ? I think it will be very helpful for me.I hope to receive your support in this case.
And with my real project, I also met an issue which I posted in the topic stackoverflow.com/questions/27467591/… . Do you know about this issue ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.