Notice
Recent Posts
Recent Comments
Link
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Archives
Today
Total
관리 메뉴

KSI일기장

231031 ModelAndView, Model 본문

Spring

231031 ModelAndView, Model

MyDiaryYo 2023. 10. 31. 17:35

 

ModelAndView Model로 생각하면 된다.

차이점은 

Model은 메서드에 파라미터로 넣어주고 String형태로 반환하고  

값을 넣을 때 addAttribute()를 사용한다

 

ModelAndView는 값을 넣을 때 addObject("변수명", "데이터값")를 사용하고

값을 보낼 view를 결정하는것은 setViewName("view 경로")를 사용한다

@GetMapping("/login")
    public ModelAndView loginView(@RequestParam(value = "error", required = false) String error,
                                  @RequestParam(value = "exception", required = false) String exception,
                                  @AuthenticationPrincipal CustomUserDetails member,
                                  HttpServletRequest request) {

        ModelAndView mav = new ModelAndView();

        if(member != null){
            mav.setViewName("redirect:/main");
        }
        else{
            mav.addObject("error", error);
            mav.addObject("exception", exception);
            mav.setViewName("/login/adminLogin");
        }
        mav.addObject("languageCode", getCommonCodeList("LANGUAGE"));

        return mav;
    }