BackEnd/Spring
[Spring] Spring 기본 4 (MVC)
best
2016. 4. 12. 11:19
1. Controller에서의 RequestMethod
- RequestMapping에서 RequestMethod를 GET으로 한다면 get으로만 들어울 수 있다는 것을 의미한다.
( 브라우저의 요청의 경우 Get이므로 URL을 쳐서 들어올 수 있다. )
- RequestMapping에서 POST로 한다면 이는 URL로 쳐서 들어올 수 없고, 폼으로만 들어 올 수 있다는 것을 의미한다.
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 |
package com.ktds.jmj.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* 서블릿을 대체할 클래스
* Controller라고 붙인다.
*/
@Controller
public class IndexController {
//RequestMethod.GET은 get으로만 들어올 수 있다는 정의이다.
//브라우저의 요청이 get이니까 URL을 쳐서 들어올 수 있다.
@RequestMapping(value="/home", method=RequestMethod.GET )
public String index() {
return "mainPage";
}
//POST는 이제 URL로 못 쳐서 들어오고 폼으로만 들어올 수 있다는 의미이다.
@RequestMapping(value="/login", method=RequestMethod.POST)
public String login() {
// WEB-INF/view/login/login.jsp
return "login/login";
}
}
|
cs |
2. ModelAndView
- View는 어떤 페이지를 보여줄 것인가를 의미한다.
- Model은 view에게 어떤 데이터를 보내줄 것인가를 의미한다.
- 결국 ModelAndView는 보여주고자 하는 페이지에 어떤 데이터를 보낼 것인가를 담고 있다.
- String과 ModelAndView는 데이터 전송 여부의 차이점이 있다.
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 |
package com.ktds.jmj.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class ArticleController {
@RequestMapping("/list")
public ModelAndView articleList() {
ModelAndView view = new ModelAndView();
view.setViewName("article/list");//어떤 페이지를 보여줄 것인가. jsp의 이름
//request.setAttribute("Key", "Value"); 와 같다.
view.addObject("title", "제목");
view.addObject("number", "1");
view.addObject("author", "전민정");
return view;
}
@RequestMapping("/detail/{articleNumber}")
public ModelAndView detail(@PathVariable int articleNumber) {
ModelAndView view = new ModelAndView();
view.setViewName("article/detail");
view.addObject("articleNumber", articleNumber);
return view;
}
}
|
cs |
3. 데이터를 전송하면 jsp에서는
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 |
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
list 페이지 입니다.
${ title }
${ number }
${ author }
</body>
</html> |
cs |
이와 같이 받는다.
두번째의 경우에는 주소를 입력할 때 /detail/100을 입력하면 articleNumber로 100의 값이 넘어간다.
1
2
3
4
5
6
7
8
9
10
11
12
13 |
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
${ articleNumber }
URL에 들어와있는 Parameter가 넘어온다.
</body>
</html> |
cs |