感謝のプログラミング 10000時間

たどり着いた結果(さき)は、感謝でした。

Spring MVC Form(textarea, checkbox, radiobutton) Example

スポンサーリンク

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

1.Controller

This controller to handle the form values.

sample.SampleController.java

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;

@Controller
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) {
			buf.append(val);
			buf.append(",");
		}
		
		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";
	}
}

2.Model

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;
	}
}

3.View

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" %>
<html>
	<head>
		<meta charset="utf-8">
		<title>${title} }</title>
	</head> 
	<body>
		<h2>${titleMessage}</h2>
		<table>
			<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>
				<tr><td>
					関心:<br/>
					<form:checkbox path="interests" value="soccer" checked="true"/>サッカー<br/>
					<form:checkbox path="interests" value="tennis"/>テニス<br/>
					<form:checkbox path="interests" value="baseball"/>野球<br/>
				</td></tr>
				<tr><td>
					性別<br/>
					<form:radiobutton path="sex" value="man"/><br/>
<form:radiobutton path="sex" value="woman"/><br/>
				</td></tr>
				<tr><td>
					年齢:<br/>
					<form:select path="age">
						<form:options items="${ageList}" />
					</form:select>
				</td></tr>
				<tr><td>メッセージ:<br/>
				<form:textarea path="message" cols="40" rows="3" /></td></tr>
				<tr><td><input type="submit"></td></tr>
			</form:form>
		</table>
		

	</body>
</html>

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

4.Result

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"%>
    
<html>
	<head>
		<meta charset="utf-8">
		<title>Welcome</title>
	</head> 
	<body>
		<h2>あなたが入力した値</h2>
		<ul>
			<li>name:${name}</li>
			<li>sex:${sex}</li>
			<li>age:${age}</li>
			<li>pass:${pass}</li>
			<li>interests:${interests}</li>
			<li>message:${message}</li>
		</ul>
	</body>
</html>

5.Demo

・send request
f:id:sho322:20140114224437j:plain
・result
f:id:sho322:20140114224443j:plain

reference

Pro Spring 3 (Professional Apress)

Pro Spring 3 (Professional Apress)