기본 콘텐츠로 건너뛰기

3.08 Framework(8) : modelAndView return & spring return

modelAndView return & spring return



 - modelAndView에서 dispatcher servlet으로 데이터와 화면을 이동할 때
  어떠한 방식으로 데이터와 화면을 넘길 것인지 생각
 - 방법은 2가지


※ return 전략 예시

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package spring.web.user;
 
import javax.servlet.http.HttpSession;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
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.servlet.ModelAndView;
 
import spring.domain.User;
import spring.service.user.UserService;
 
 
@Controller
public class UserController {
    //field
    @Autowired
    @Qualifier("userService")
    private UserService userService;
    
    //constructor method
    public UserController() {
        System.out.println("유저 컨트롤러 기본 생성자 호출~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
    }
    
//권한 확인 / 내비게이션
@RequestMapping("/logon.do")                    //   ' / ' 조심
public ModelAndView logon() {
    
    System.out.println("[ Logon() start....!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!]");
    
    //클라이언트에 전달할 메시지 생성
    String message = "[Logon()] 아이디, 패스워드 3자리 이상 입력!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!]";
    
    //모델 / 뷰 정보를 갖는 model and view 생성
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.setViewName("/user/logon.jsp");
    modelAndView.addObject("message", message);
    
    System.out.println("[Logon() end...!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!]\n");
    
    return modelAndView;
}
 
 
//비즈니스 로직 수행 / 내비게이션
@RequestMapping("/home.do")
public ModelAndView home(HttpSession session) {
    
    System.out.println("[ Home() start....!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!]");
    
    String message="[Home()] WELCOME!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!]";
    
    //모델 / 뷰 정보를 갖는 model and view 생성
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.setViewName("/user/home.jsp");
    modelAndView.addObject("message", message);
    
    System.out.println("[Home() end...!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!]\n");
    
    return modelAndView;
}
 
//단순 내비게이션
@RequestMapping(value="/logonAction.do", method=RequestMethod.GET)
public ModelAndView logonAction() {
    
    System.out.println("[ LogonAction() method = RequestMethod.GET start....!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!]");
    
    //모델 / 뷰 정보를 갖는 model and view 생성
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.setViewName("/logon.do");
    
    System.out.println("[ LogonAction() method = RequestMethod.GET end...!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!]\n");
    
    return modelAndView;
}
 
//비즈니스 로직 수행 / 내비게이션
@RequestMapping(value="/logonAction.do", method=RequestMethod.POST)
public ModelAndView logonAction(@ModelAttribute("user") User user, HttpSession session) throws Exception {
    System.out.println("[ LogonAction() method = RequestMethod.POST start....!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!]");
    
    //컨트롤러 :: 권한 / 인증 처리
    //if(session.isNew() || session.getAttribute("sessionUser") ==null) {
        //session.setAttribute("sessionUser", user);
    //}
    //User sessionUser = (User)session.getAttribute("sessionUser");
    
    //컨트롤러 ::  내비게이션
    String viewName = "/user/logon.jsp";
    
    //if(user.isActive()) {
    //    viewName = "/user/home.jsp";
    //}else {
    
    //컨트롤러 :: 비즈니스 로직 처리
    //비즈니스 레이어의 DAO 생성 및 메서드 호출
    //UserDao userDao = new UserDao();
    //userDao.getUser(user);
    User returnUser = userService.getUser(user.getUserId());
    if(returnUser.getPassword().equals(user.getPassword())) {
        returnUser.setActive(true);
        user = returnUser;
    }
    
    //컨트롤러 :: 비즈니스 로직 처리한 결과로 내비게이션
    if(user.isActive()) {
        viewName = "/user/home.jsp";
        session.setAttribute("sessionUser", user);
    }
//}
System.out.println("[action : "+viewName+"]");
 
    //클라이언트에 전달할 메시지 생성
    String message = null;
    if(viewName.equals("/user/home.jsp")) {
        message="[LogonAction()] WELCOME!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!]";
    }else {
        message="[LogonAction()] 아이디, 패스워드 3자리 이상 입력!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!]";
    }
    //모델 / 뷰 정보를 갖는 model and view 생성
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.setViewName(viewName);
    modelAndView.addObject("message", message);
    
    System.out.println("[LogonAction() end...!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!]\n");
    
    return modelAndView;
}
 
@RequestMapping("/logout.do")
public ModelAndView logout(HttpSession session) {
    
    System.out.println("[ Logout() start....!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!]");
    
    //로그온 정보 삭제
    session.removeAttribute("sessionUser");
    
    //클라이언트에 전달할 메시지 생성
    String message = "[Logout()] 아이디, 패스워드 3자리 이상 입력!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!]";
    
    //모델 / 뷰 정보를 갖는 model and view 생성
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.setViewName("/user/logon.jsp");
    modelAndView.addObject("message", message);
    
    System.out.println("[Logout() end...!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!]\n");
    
    return modelAndView;
}
}//class over
 
cs

1. ModelAndView return
 1) ModelAndView instance 생성
 2) setViewName 메서드를 이용하여 이동할 화면 지정
 3) addObject 메서드를 이용하여 담을 정보 표현


2. Spring return
 1) Client에 전달할 메시지 또는 viewName 지정, 표시
 2) Model(data) 지정