SpringBoot入門


2.MVCに基づくプログラム作成

2-6 ビューからのデータ入力

②DemoControllerを以下のように修正指定します。

package com.example.demo; 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 DemoController { @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("text1")String str, ModelAndView mav) { mav.setViewName("index"); mav.addObject("msg", "こんにちは" + str + "さん!"); mav.addObject("value", str); return mav; } }

ブラウザからURLを指定された場合には、GETで処理されるので、indexメソッドが処理 され、次にHTMLから送信された場合には、POSTを指定していますので、sendメソッドが 処理されます。