[SpringBoot2.4.3] ページ移動(フォワード/リダイレクト)

Controller

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HelloController {
	
	@RequestMapping("/")
	public ModelAndView index(ModelAndView mav) {
		mav.setViewName("index");
		return mav;
	}
	@RequestMapping("/other")
	public String other() {
		return "redirect:/";
	}
	@RequestMapping("/home")
	public String home() {
		return "forward:/";
	}
}


forwardを使う用途がわからんが、実装方法はわかった。

[SpringBoot2.4.3] フォームコントロール

	<form method="post" action="/">
		<div>
			<input type="checkbox" id="check1" name="check1">
			<label for="check1">チェック</label>
		</div>
		<div>
			<input type="radio" id="radioA" name="radio1" value="male">
			<label for="radioA">男性</label>
		</div>
		<div>
			<input type="radio" id="radioB" name="radio1" value="femail">
			<label for="radioB">女性</label>
		</div>
		<div>
			<select id="select1" name="select1" size="4">
				<option value="Windows">Windows</option>
				<option value="Mac">Mac</option>
				<option value="Linux">Linux</option>
			</select>
		</div>
		<div>
			<select id="select2" name="select2" size="4" multiple="multiple">
				<option value="Android">Android</option>
				<option value="iphone">iPhone</option>
				<option value="Winfone">Windows Phone</option>
			</select>
		</div>
		<input type="submit" value="Click">
	</form>

Controller

import org.springframework.stereotype.Controller;
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;

@Controller
public class HelloController {
	
	@RequestMapping(value="/", method=RequestMethod.GET)
	public ModelAndView index(ModelAndView mav) {
		mav.setViewName("index");
		mav.addObject("msg", "フォームを送信してください。");
		return mav;
	}
	
	@RequestMapping(value="/", method=RequestMethod.POST)
	public ModelAndView send(
			@RequestParam(value="check1",required=false) boolean check1,
			@RequestParam(value="radio1",required=false) String radio1,
			@RequestParam(value="select1",required=false) String select1,
			@RequestParam(value="select2",required=false) String[] select2,
			ModelAndView mav) {
		
		String res = "";
		try {
			res = "check:" + check1 + " radio:" + radio1 + " select:" + select1 + "\nselect2:";
		} catch(NullPointerException e) {}
		try {
			res += select2[0];
			for(int i = 1; i < select2.length; i++) 
				res += ", " + select2[i];
			} catch (NullPointerException e) {
				res += "null";
			}
		mav.addObject("msg", res);
		mav.setViewName("index");
		return mav;
	}
}

そこそこ来てる? 学習途中はどこら辺にいるかさっぱり見当がつかないなー

[SpringBoot2.4.3] RestControllerを利用する

RestControllerはビューを使わずコントローラーだけでアプリの基本部分を作成できる

package com.example.demo;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
	
	@RequestMapping("/")
	public String index() {
		return "Hello Spring-Boot World!";
	}
}

http://localhost:8080/
Hello Spring-Boot World!

### パラメータを渡す

package com.example.demo;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
	
	@RequestMapping("/{num}")
	public String index(@PathVariable int num) {
		int res = 0;
		for(int i = 1; i <= num; i++)
			res += i;
		return "total: " + res;
	}
}

http://localhost:8080/5
total: 15

おおお、一周回って基礎やると理解しやすいな。

### DataObject

package com.example.demo;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
	
	String[] names = {"hpscript","hanako","taro","sachiko","ichiro"};
	String[] mails = {"info@hpscript.com", "hanako@gmail.com", "taro@gmail.com","sachiko@gmail.com","ichiro@gmail.com"};
	
	@RequestMapping("/{id}")
	public DataObject index(@PathVariable int id) {
		return new DataObject(id, names[id],mails[id]);
	}
	
}
class DataObject{
	
	private int id;
	private String name;
	private String value;
	
	public DataObject(int id, String name, String value) {
		super();
		this.id = id;
		this.name = name;
		this.value = value;
	}
	
	public int getId() {return id;}
	public void setId(int id) {this.id = id;}
	
	public String getName() {return name;}
	public void setName(String name) {this.name = name;}
	
	public String getValue() {return value;}
	public void setValue(String value) {this.value = value;}
}

http://localhost:8080/2
{“id”:2,”name”:”taro”,”value”:”taro@gmail.com”}
http://localhost:8080/0
{“id”:0,”name”:”hpscript”,”value”:”info@hpscript.com”}

おおお、なんか変に感傷的になるな〜