In this post, I shall explain how to create a Spring form with check boxes. Additionally, we will learn how to make a list of items checked by default.
web.xml :
web.xml :
1: <web-app id="WebApp_ID" version="2.4"
2: xmlns="http://java.sun.com/xml/ns/j2ee"
3: xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4: xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
5: http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
6: <display-name>Spring MVC Application</display-name>
7: <servlet>
8: <servlet-name>HelloWeb</servlet-name>
9: <servlet-class>
10: org.springframework.web.servlet.DispatcherServlet
11: </servlet-class>
12: <load-on-startup>1</load-on-startup>
13: </servlet>
14: <servlet-mapping>
15: <servlet-name>HelloWeb</servlet-name>
16: <url-pattern>/</url-pattern>
17: </servlet-mapping>
18: </web-app>
HelloWeb-servlet.xml :
1: <beans xmlns="http://www.springframework.org/schema/beans"
2: xmlns:context="http://www.springframework.org/schema/context"
3: xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4: xsi:schemaLocation="
5: http://www.springframework.org/schema/beans
6: http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
7: http://www.springframework.org/schema/context
8: http://www.springframework.org/schema/context/spring-context-3.0.xsd">
9: <context:component-scan base-package="com.sri.test" />
10: <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
11: <property name="prefix" value="/WEB-INF/jsp/" />
12: <property name="suffix" value=".jsp" />
13: </bean>
14: </beans>
1: package com.sri.test;
2: import java.io.Serializable;
3: public class Subject implements Serializable{
4: /**
5: *
6: */
7: private static final long serialVersionUID = 7135393884889475409L;
8: private int subjectId;
9: private String subjectName;
10: public Subject() {
11: super();
12: }
13: public Subject(int subjectId, String subjectName) {
14: super();
15: this.subjectId = subjectId;
16: this.subjectName = subjectName;
17: }
18: public int getSubjectId() {
19: return subjectId;
20: }
21: public void setSubjectId(int subjectId) {
22: this.subjectId = subjectId;
23: }
24: public String getSubjectName() {
25: return subjectName;
26: }
27: public void setSubjectName(String subjectName) {
28: this.subjectName = subjectName;
29: }
30: @Override
31: public boolean equals(Object obj) {
32: if (this == obj)
33: return true;
34: if (obj == null)
35: return false;
36: if (getClass() != obj.getClass())
37: return false;
38: Subject other = (Subject) obj;
39: if (subjectId != other.subjectId)
40: return false;
41: if (subjectName == null) {
42: if (other.subjectName != null)
43: return false;
44: } else if (!subjectName.equals(other.subjectName))
45: return false;
46: return true;
47: }
48: }
Student.java :
Student has one-to-many association with Subject:
1: package com.sri.test;
2: import java.io.Serializable;
3: import java.util.List;
4: public class Student implements Serializable{
5: /**
6: *
7: */
8: private static final long serialVersionUID = -779930820340758193L;
9: private int studentId;
10: private String studentName;
11: private List<Subject> subjectList;
12: public Student(int studentId, String studentName, List<Subject> subjectList) {
13: super();
14: this.studentId = studentId;
15: this.studentName = studentName;
16: this.subjectList = subjectList;
17: }
18: public Student() {
19: }
20: public int getStudentId() {
21: return studentId;
22: }
23: public void setStudentId(int studentId) {
24: this.studentId = studentId;
25: }
26: public String getStudentName() {
27: return studentName;
28: }
29: public void setStudentName(String studentName) {
30: this.studentName = studentName;
31: }
32: public List<Subject> getSubjectList() {
33: return subjectList;
34: }
35: public void setSubjectList(List<Subject> subjectList) {
36: this.subjectList = subjectList;
37: }
38: }
HelloController.java :
allList is a list of all the subjects available; enrolledList is a list of subjects that a student has enrolled. We simply add student object and allList to the ModelMap and return the view.
1: package com.sri.test;
2: import java.util.ArrayList;
3: import java.util.List;
4: import org.springframework.stereotype.Controller;
5: import org.springframework.ui.ModelMap;
6: import org.springframework.web.bind.annotation.RequestMapping;
7: import org.springframework.web.bind.annotation.RequestMethod;
8: @Controller
9: @RequestMapping("/")
10: public class HelloController{
11: @RequestMapping(value = "", method = RequestMethod.GET)
12: public String printHello(ModelMap model) {
13: model.addAttribute("message", "Hello Spring MVC Framework!");
14: List<Subject> allList = new ArrayList<Subject>();
15: allList.add(new Subject(1,"English"));
16: allList.add(new Subject(2,"Maths"));
17: allList.add(new Subject(3,"Science"));
18: allList.add(new Subject(4,"Social"));
19: allList.add(new Subject(5,"Tamil"));
20: List<Subject> enrolledList = new ArrayList<Subject>();
21: enrolledList.add(new Subject(1,"English"));
22: enrolledList.add(new Subject(3,"Science"));
23: Student student = new Student(1,"SRI",enrolledList);
24: model.addAttribute("student", student);
25: model.addAttribute("allSubjectList", allList);
26: return "home";
27: }
28: }
home.jsp :
This holds the spring form. commandName is the model attribute which is used to fill the form, the path is the model to be used to pre-populate data, items point to list of all the subjects available and path to the subjects taken by the student model.
1: <%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
2: <html>
3: <head>
4: <title>Hello World</title>
5: </head>
6: <body>
7: <h2>${student.studentName}</h2>
8: <form:form method="POST" action="save" commandName="student">
9: <form:checkboxes title="Subjects taken" path="subjectList"
10: items="${allSubjectList}" itemLabel="subjectName" element="div" />
11: </form:form>
12: </body>
13: </html>
Output:
Cheers!
S. Srikrishnan