m_shige1979のときどきITブログ

プログラムの勉強をしながら学習したことや経験したことをぼそぼそと書いていきます

Github(変なおっさんの顔でるので気をつけてね)

https://github.com/mshige1979

Spring Bootでリクエスト時の受け取るパラメータをクラスにする

リクエストパラメータを1つずつ書くのはちょっと手間がかかる

もう少し楽したい(^^)

こんな感じのクラスを用意

TestForm.java
package com.example.beans;

public class TestForm {
	
	private String name;
    private String age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }
}

※lombokを使用したかったけどわからなくなるのでやめた

コントローラー

HelloController.java
package com.example;

import java.util.Date;

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;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.example.beans.TestForm;

@Controller
public class HelloController {
	
	@RequestMapping(value = {"/"}, method = {RequestMethod.GET})
	public ModelAndView index(@ModelAttribute TestForm form) {
		
		// 生成
		ModelAndView mv = new ModelAndView();
		
		// テンプレートを指定
		mv.setViewName("index");
		
		// 日時を取得、設定
		mv.addObject("now", new Date().toString());
		
		// modelに設定して画面に表示するようにする
		mv.addObject("form", form);
		
		// 返却
		return mv;
	}
	
	// POST用のパラメータを受け取る
	@RequestMapping(value = {"/formPost"}, method = {RequestMethod.POST})
	public ModelAndView postTest1(
			@ModelAttribute TestForm form) {
		
		// 生成
		ModelAndView mv = new ModelAndView();
		
		// テンプレートを指定
		mv.setViewName("test1/post");
		
		// modelに設定して画面に表示するようにする
		mv.addObject("form", form);
		
		// 返却
		return mv;
	}
	
	// GET用のパラメータを受け取る
	@RequestMapping(value = {"/formPost"}, method = {RequestMethod.GET})
	public ModelAndView getTest1(
			@ModelAttribute TestForm form) {
		
		// 生成
		ModelAndView mv = new ModelAndView();
		
		// テンプレートを指定
		mv.setViewName("test1/post");
		
		// modelに設定して画面に表示するようにする
		mv.addObject("form", form);
		
		// 返却
		return mv;
	}
	
}

※ModelAttributeを使用するように変更

画面

index.html
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Spring Sample Demo</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
	<h1>Spring Sample</h1>
	<div>
    	Spring Sample Test
	</div>
	<p>Now <span th:text="${now}"></span></p>
	<br />
	<form method="post" action="formPost">
		<span>name: </span>
		<input type="text" name="name" th:field="${form.name}" />
		<br />
		<span>age: </span>
		<input type="text" name="age" th:field="${form.age}" />
		<br />
		<input type="submit" value="send" />
	</form>
</body>
</html>
post.html
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Spring Sample Demo</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
	<h1>Spring Sample</h1>
	<div>
    	Post Data
	</div>
	<br />
	<form method="post" action="formPost">
		<span>name: </span>
		<span th:text="${form.name}"></span>
		<br />
		<span>age: </span>
		<span th:text="${form.age}"></span>
		<br />
	</form>
</body>
</html>

所感

単純な受取が少しだけ楽になった感じ…
システムが複雑になるにつれて便利になっていくと思うので忘れないようにしたい。