Simple Form on Spring Web MVC

Create the bean first

package com.dyutiman.spring.formbean;

public class Person {

	private String name;
	private int age;
	private String sex;
	private String location;

	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 String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getLocation() {
		return location;
	}

	public void setLocation(String location) {
		this.location = location;
	}
}

and a validator to validate this bean

package com.dyutiman.spring.validator;

public class PersonValidator implements Validator {

	@Override
	public boolean supports(Class<?> clazz) {

		return clazz == Person.class;
	}

	@Override
	public void validate(Object target, Errors errors) {

		Person p = (Person) target;

		ValidationUtils
				.rejectIfEmpty(errors, "name", "nameEmpty", "enter name");
		if (p.getAge() <= 0 || p.getAge() > 110) {
			errors.rejectValue("age", "wrongAge", "enter valid age");
		}
		ValidationUtils.rejectIfEmpty(errors, "sex", "sexEmpty", "select sex");
		ValidationUtils.rejectIfEmpty(errors, "location", "locationEmpty",
				"select location");
	}
}

Now, we create the controller where we register the validator for Person bean in the @InitBinder, a method to show the Person add form and another method to display the added Person

package com.dyutiman.spring.controller;

@Controller
public class PersonController {

	@InitBinder
	public void initModel(WebDataBinder binder) {

		Object target = binder.getTarget();
		
		if (target instanceof Person) {
			if (binder.getValidator() == null) {
				binder.setValidator(new PersonValidator());
			}
		}
	}

	@RequestMapping(value = "/index.htm")
	public ModelAndView showForm(Person p) {

		ModelAndView model = new ModelAndView("PersonForm");

		model.addObject("person", p);
		return model;
	}

	@RequestMapping(value = "/showPerson.htm", method = RequestMethod.POST)
	public ModelAndView submitPerson(@Valid Person p, BindingResult error) {

		// Submit to Database or else
		ModelAndView model = new ModelAndView();
		model.addObject("person", p);

		if (error.hasErrors()) {
			model.setViewName("PersonForm");
		}

		return model;
	}
}

As the controller is built, lets create the add form view first

<%@ page language="java" contentType="text/html; charset=UTF-8"	pageEncoding="UTF-8"%>

<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Add Person</title>
<style type="text/css">
td span{color:red;}
</style>

</head>
<body>

<form:form commandName="person" action="showPerson.htm" method="post">
	<table>
		<tr>
			<th colspan="3">Add Person</th>
		</tr>
		<tr>
			<td width="49%" align="right">Name</td>
			<td width="2%">&nbsp;</td>
			<td width="49%"><form:input path="name" /><form:errors path="name"/></td>
		</tr>
		<tr>
			<td width="49%" align="right">Age</td>
			<td width="2%">&nbsp;</td>
			<td width="49%"><form:input path="age" /><form:errors path="age"/></td>
		</tr>
		<tr>
			<td width="49%" align="right">Sex</td>
			<td width="2%">&nbsp;</td>
			<td width="49%">Male: <form:radiobutton path="sex" value="male" />
			
			Female: <form:radiobutton path="sex" value="female" /><form:errors path="sex"/></td>
		</tr>
		<tr>
			<td width="49%" align="right">Location</td>
			<td width="2%">&nbsp;</td>
			<td width="49%">
				<form:select path="location">
					<form:option value="Bangalore" />
					<form:option value="Delhi" />
					<form:option value="Kolkata" />
					<form:option value="Mumbai" />
				</form:select><form:errors path="location"/></td>
		</tr>
		<tr>
			<td width="49%" align="right">&nbsp;</td>
			<td width="2%">&nbsp;</td>
			<td width="49%"><input type="submit" value="Save Changes" /></td>
		</tr>
	</table>

</form:form>
</body>
</html>

and the display view

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<table>
		<tr>
			<th colspan="3">Added Person</th>
		</tr>
		<tr>
			<td width="49%" align="right"><b>Name</b></td>
			<td width="2%">&nbsp;</td>
			<td width="49%"><c:out value="${person.name}"/></td>
		</tr>
		<tr>
			<td width="49%" align="right"><b>Age</b></td>
			<td width="2%">&nbsp;</td>
			<td width="49%"><c:out value="${person.age}"/></td>
		</tr>
		<tr>
			<td width="49%" align="right"><b>Sex</b></td>
			<td width="2%">&nbsp;</td>
			<td width="49%"><c:out value="${person.sex}"/></td>
		</tr>
		<tr>
			<td width="49%" align="right"><b>Location</b></td>
			<td width="2%">&nbsp;</td>
			<td width="49%"><c:out value="${person.location}"/></td>
		</tr>
		<tr>
			<td width="49%" align="right">&nbsp;</td>
			<td width="2%">&nbsp;</td>
			<td width="49%"><a href="index.htm">go back</a></td>
		</tr>
	</table>
</body>
</html>

http://blog.jteam.nl/2009/05/14/simple-forms-with-spring-mvc-2-5/
http://maestric.com/doc/java/spring/form_validation

Advertisements