Spring MVC Form(textarea, checkbox, radiobutton) Example


I'll introduce how to use the form tags on Spring MVC.


This controller to handle the form values.


package sample;

import java.util.LinkedHashMap;
import java.util.Map;

import model.IndexFormModel;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

public class SampleController {
	@RequestMapping(value = "/index" , method = RequestMethod.GET)
	public String index(Model model) {
		Map<String, String> ageMap = new LinkedHashMap<String, String>();
		ageMap.put("10~20", "10代");
		ageMap.put("20~30", "20代");
		ageMap.put("30~40", "30代");
		model.addAttribute("ageList", ageMap);
		model.addAttribute("title", "フォームを使ってみよう!");
		model.addAttribute("message", "項目に入力をお願いします。");
		model.addAttribute("formModel", new IndexFormModel());
		return "showMessage";
	@RequestMapping(value = "/form", method = RequestMethod.POST)
	public String form(@ModelAttribute IndexFormModel formModel,Model model) {
		String name = formModel.getName();
		String pass = formModel.getPass();
		String sex = formModel.getSex();
		String age = formModel.getAge();
		String[] interests = formModel.getInterests();
		String message = formModel.getMessage();
		StringBuffer buf = new StringBuffer();
		for (String val: interests) {
		model.addAttribute("name", name);
		model.addAttribute("pass", pass);
		model.addAttribute("sex", sex);
		model.addAttribute("age", age);
		model.addAttribute("interests", buf.toString()); //interests
		model.addAttribute("message", message);
		return "result";


this is model.IndexFormModel.java
This store the forms value.

package model;

public class IndexFormModel {
	private String title;
	private String titleMessage;
	private String name;
	private String pass;
	private String sex;
	private String age;
	private String[] interests;
	private String message;
	public String getTitle() {
		return title;
	public void setTitle(String title) {
		this.title = title;
	public String getTitleMessage() {
		return titleMessage;
	public void setTitleMessage(String titleMessage) {
		this.titleMessage = titleMessage;
	public String getName() {
		return name;
	public void setName(String name) {
		this.name = name;
	public String getPass() {
		return pass;
	public void setPass(String pass) {
		this.pass = pass;
	public String getSex() {
		return sex;
	public void setSex(String sex) {
		this.sex = sex;
	public String getAge() {
		return age;
	public void setAge(String age) {
		this.age = age;
	public String[] getInterests() {
		return interests;
	public void setInterests(String[] interests) {
		this.interests = interests;
	public String getMessage() {
		return message;
	public void setMessage(String message) {
		this.message = message;


A JSP page to show the use of Spring's form tag.

<!DOCTYPE html>

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
		<meta charset="utf-8">
		<title>${title} }</title>
			<form:form modelAttribute="formModel" method="POST" action="form">
				<tr><td>名前:<form:input path="name"/></td></tr>
				<tr><td>パスワード:<form:password path="pass" showPassword="on"/></td></tr>
					<form:checkbox path="interests" value="soccer" checked="true"/>サッカー<br/>
					<form:checkbox path="interests" value="tennis"/>テニス<br/>
					<form:checkbox path="interests" value="baseball"/>野球<br/>
					<form:radiobutton path="sex" value="man"/><br/>
<form:radiobutton path="sex" value="woman"/><br/>
					<form:select path="age">
						<form:options items="${ageList}" />
				<form:textarea path="message" cols="40" rows="3" /></td></tr>
				<tr><td><input type="submit"></td></tr>


The value of this, will catch by the controller.
The getting requested method of controller is form() method.


A JSP page to show the result of getting the form value.

<!DOCTYPE html>

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


・send request


Pro Spring 3 (Professional Apress)

Pro Spring 3 (Professional Apress)