Spring 04

・Buttonで画面の遷移
@Controller
public class NewController {
    @RequestMapping("/")
    public ModelAndView index (ModelAndView mav){
        mav.setViewName("index");
        return mav;
    }   
    @RequestMapping("/other")
    public String other (){

        return "redirect:/";
    }   
    @RequestMapping("/new")
    public String user(){

        return "forward:/";
    }   
}

redirect:/
localhost:8080/otherに遷移したら、localhost:8080/に転送します。

forward:/
localhost:8080/newに遷移します。

<form method="post" action="result">
actionで指定のページへ遷移します。

 


・POSTでボタンを押すと、データを保存します。
@RequestMapping(value = { "/result" }, method = { RequestMethod.POST })
    public ModelAndView postTest3() {

        ModelAndView mv = new ModelAndView();

        mv.setViewName("result");

        return mv;
    }

結果を保存したら、成功メッセージが表示されるページに遷移します。
   
@RequestMapping(value = "/result",method = RequestMethod.POST)
    public UserEntity create(UserEntity entity)
    {
        return userJPA.save(entity);
    }
データを保存することです。


結果:passwordの保存は成功したが、user_idの保存は失敗しました。
原因:まだわかりません。

URLで実行したら、機能は正常です。しかし、POSTで実行するのはうまくできません。

 

・あった状況

POSTとGET同じURLに移動して、違う結果をもらいます。(値をもらう問題だと思います。)

POSTはエラーページに遷移します。
GETは正常な画面に遷移します。

POSTで新しい画面に遷移して、GETと同じ画面に遷移します。(上記と矛盾しているんですが、実際に発生しまいました。)

ChromeとEdgeは配列が表示しますが、Eclipseのブラウザはダウンロードの画面が表示されます。
JSONのファイルはChromeとEdgeに表示することができると思います。ですから、この結果をもらいました。


GETとPOSTの使い方は違うので、同じ移動方法を使わないほうがいいと思います。

@RestController
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    private UserJPA userJPA;


    @RequestMapping(value ="/list" ,method = RequestMethod.GET)
    public List<UserEntity> list(){
        return userJPA.findAll();
    }

   
    @RequestMapping(value = { "/result" }, method = { RequestMethod.POST })
    public ModelAndView postTest3() {

        ModelAndView mv = new ModelAndView();

        mv.setViewName("result");

        return mv;
    }

}
赤い部分は、localhost:8080/user/listに遷移したら、DataBaseのデータが表示されます。

<form method="post" action="user/result">
青い部分は、POSTで値を渡して、GETの部分を実行できます。

これから、POSTとGETの機能を分別します。しないと、混乱するかもしれません。