Spring MVC - ์์ฒญ & ์๋ต
์์ฒญ ๊ฒฝ๋ก ๋งคํ
โข
๋ ๋ฒจ ๋งคํ
โฆ
ํด๋์ค ๋ ๋ฒจ ๋งคํ
โฆ
๋ฉ์๋ ๋ ๋ฒจ ๋งคํ
โข
๊ฒฝ๋ก ํจํด ๋งคํ
โข
์์ฒญ ๋ฉ์๋ ๋งคํ
โข
ํ๋ผ๋ฏธํฐ ๋งคํ
โข
ํค๋ ๋งคํ
โข
ContentType ๋งคํ
โข
Accept ๋งคํ
๋ชจ๋ธ
โข
Model ์ธํฐํ์ด์ค
โข
๋ชจ๋ธ ๋ฑ๋ก
โข
@ModelAttribute
โข
@RequestParam
โข
RedirectAttributes
์๋ต ์ฒ๋ฆฌ
โข
void
โข
String
โข
ModelAndView
โข
ํด๋์ค
โข
์ปฌ๋ ์
โข
ResponseEntity
์์ฒญ ์ฒ๋ฆฌ
โข
์ฃผ์ ์ปจํธ๋กค๋ฌ ๋ฉ์๋ ๋งค๊ฐ๋ณ์
โข
์ฃผ์ ์์ฒญ ๋ฐ์ดํฐ ์ฒ๋ฆฌ ์ด๋
ธํ
์ด์
โข
ํผ ์์ฒญ ์ฒ๋ฆฌ
โข
ํ์ผ ์
๋ก๋
โข
AJAX ํ์ผ ์
๋ก๋
์์ฒญ ๊ฒฝ๋ก ๋งคํ
@RequestMapping : ์์ฒญ ๊ฒฝ๋ก ๋งคํ ์ด๋ ธํ ์ด์
โข
ํ์ ์ ๋ณด : ์์ฒญ ๊ฒฝ๋ก(URL)
โข
์ฃผ์ ์์ฑ
โฆ
value : ์์ฒญ ๊ฒฝ๋ก
โฆ
method : ์์ฒญ ๋ฉ์๋
โช
RequestMethod.GET
โช
RequestMethod.POST
@RequestMapping ์์ฑ
์์ฑ | ์ค๋ช
|
value ๋๋ path | ์์ฒญ์ ๋งคํํ URL ํจํด์ ์ง์ ํฉ๋๋ค. |
method | HTTP ์์ฒญ ๋ฉ์๋๋ฅผ ์ง์ ํฉ๋๋ค. ๊ธฐ๋ณธ์ ์ผ๋ก ๋ชจ๋ HTTP ๋ฉ์๋๋ฅผ ํ์ฉํ๋ฉฐ ๋ฐฐ์ด๋ก ์ฌ๋ฌ ๋ฉ์๋๋ฅผ ์ง์ ํ ์ ์์ต๋๋ค. ์: RequestMethod.GET, RequestMethod.POST ๋ฑ. |
params | ์์ฒญ ํ๋ผ๋ฏธํฐ์ ์กฐ๊ฑด์ ์ง์ ํฉ๋๋ค. ํน์ ํ๋ผ๋ฏธํฐ์ ์กด์ฌ ์ฌ๋ถ๋ ๊ฐ์ ๋ฐ๋ผ ์์ฒญ์ ์ฒ๋ฆฌํ ์ ์์ต๋๋ค. ์: params = "paramName=paramValue" |
headers | HTTP ํค๋์ ์กฐ๊ฑด์ ์ง์ ํฉ๋๋ค. ํน์ ํค๋์ ์กด์ฌ ์ฌ๋ถ๋ ๊ฐ์ ๋ฐ๋ผ ์์ฒญ์ ์ฒ๋ฆฌํ ์ ์์ต๋๋ค. ์: headers = "Content-Type=application/json" |
consumes | ์์ฒญ์ ์ปจํ
์ธ ์ ํ์ ์ง์ ํฉ๋๋ค. ํด๋ผ์ด์ธํธ๊ฐ ๋ณด๋ด๋ ๋ฐ์ดํฐ์ ์ปจํ
์ธ ์ ํ์ ๋ฐ๋ผ ์์ฒญ์ ์ฒ๋ฆฌํ ์ ์์ต๋๋ค. ์: consumes = "application/json" |
produces | ์๋ต์ ์ปจํ
์ธ ์ ํ์ ์ง์ ํฉ๋๋ค. ์๋ฒ์์ ์์ฑํ๋ ์๋ต์ ์ปจํ
์ธ ์ ํ์ ์ ํํ ์ ์์ต๋๋ค. ์: produces = "application/json" |
name | ํธ๋ค๋ฌ ๋ฉ์๋์ ์ด๋ฆ์ ์ง์ ํฉ๋๋ค. ์ด๋ฆ์ ๋ฉ์๋์ ์ฐ๊ฒฐ๋๋ฉฐ XML ์ค์ ์์ ์ฌ์ฉ๋ ์ ์์ต๋๋ค. |
๋ ๋ฒจ ๋งคํ
ํด๋์ค ๋ ๋ฒจ ๋งคํ
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/board")
public class BoardController {
// ์ด ๋ฉ์๋๋ "/board"์ ์ฐ๊ฒฐ๋ ๊ฒฝ๋ก์ ํ์ ๊ฒฝ๋ก์ ๋ฐ๋ผ ๋งคํ๋ฉ๋๋ค.
@RequestMapping("/list")
public String list() {
// ๊ฒ์ํ ๋ชฉ๋ก์ ๋ณด์ฌ์ฃผ๋ ๋ก์ง์ ์ํ
return "board/list";
}
}
Java
๋ณต์ฌ
๋ฉ์๋ ๋ ๋ฒจ ๋งคํ
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class BoardController {
@RequestMapping("/board/list")
public String list() {
// ๊ฒ์ํ ๋ชฉ๋ก์ ๋ณด์ฌ์ฃผ๋ ๋ก์ง์ ์ํ
return "board/list";
}
}
Java
๋ณต์ฌ
๊ฒฝ๋ก ํจํด ๋งคํ
: ์์ฒญ ๊ฒฝ๋ก์ ๋ณ์๋ฅผ ์ง์ ํ์ฌ ๋งคํํ๋ ๋ฐฉ๋ฒ
๋ฐฉ๋ฒ
@RequestMapping("/๊ฒฝ๋ก/{๋ณ์}")
public String test(@PathVariable("๋ณ์๋ช
") String ํ๋ผ๋ฏธํฐ๋ช
) {
return "/site/test";
}
Java
๋ณต์ฌ
โข
BoardController.java
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/board")
public class BoardController {
// "/board/read/{id}" ๊ฒฝ๋ก์ ์ฐ๊ฒฐ
@RequestMapping("/read/{id}")
public String read(@PathVariable("id") int id) {
// id ๋ณ์๋ฅผ ์ฌ์ฉํ์ฌ ๊ฒ์๋ฌผ์ ์กฐํํ๋ ๋ก์ง์ ์ํ
// ์๋ฅผ ๋ค์ด, id์ ํด๋นํ๋ ๊ฒ์๋ฌผ์ ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ๊ฐ์ ธ์์ ํ๋ฉด์ ํ์
return "/board/read";
}
}
Java
๋ณต์ฌ
์์ฒญ ๋ฉ์๋ ๋งคํ
: ์์ฒญ ๋ฉ์๋๋ฅผ ๋งคํ ์กฐ๊ฑด์ผ๋ก ๋งคํํ๋ ๋ฐฉ๋ฒ
๋ฐฉ๋ฒ
/*
RequestMethod ์ด๊ฑฐํ์
- RequestMethod.GET
- RequestMethod.POST
- RequestMethod.PUT
- RequestMethod.DELETE
*/
@RequestMapping(value = "/๊ฒฝ๋ก", method = RequestMethod.๋ฉ์๋๋ฐฉ์)
public String insert(Board board) {
return "/board/insert";
}
Java
๋ณต์ฌ
โข
BoardController.java
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/board")
public class BoardController {
@RequestMapping(value = "/insert", method = RequestMethod.GET)
public String insert() {
return "/board/insert";
}
@RequestMapping(value = "/insert", method = RequestMethod.POST)
public String insertPro(Board board, Model model) {
return "redirect:/board/list";
}
}
Java
๋ณต์ฌ
ํ๋ผ๋ฏธํฐ ๋งคํ
: ์์ฒญ ํ๋ผ๋ฏธํฐ๋ฅผ ๋งคํ ์กฐ๊ฑด์ผ๋ก ๋งคํํ๋ ๋ฐฉ๋ฒ
๋ฐฉ๋ฒ
@RequestMapping(value = "/๊ฒฝ๋ก", method = RequestMethod.๋ฉ์๋๋ฐฉ์, params="ํ๋ผ๋ฏธํฐ๋ช
")
public String read() {
return "/board/read";
}
Java
๋ณต์ฌ
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/board")
public class BoardController {
@RequestMapping(value = "/read", method = RequestMethod.GET, params="boardNo")
public String read(Model model, @RequestParam("boardNo") int boardNo) {
return "board/read";
}
}
Java
๋ณต์ฌ
ํค๋ ๋งคํ
: ์์ฒญ ๋ฉ์์ง์ ํค๋๋ฅผ ๋งคํ ์กฐ๊ฑด์ผ๋ก ์์ฒญ ๊ฒฝ๋ก๋ฅผ ๋งคํํ๋ ๋ฐฉ๋ฒ
๋ฐฉ๋ฒ
@RequestMapping(value = "/๊ฒฝ๋ก", headers="ํค๋๋ช
=ํค๋๊ฐ")
public String updatePro() {
return "redirect:/board/list";
}
Java
๋ณต์ฌ
X-HTTP-Method-Override ํค๋
: HTTP ์์ฒญ ๋ฉ์๋๋ฅผ ๋ช
์์ ์ผ๋ก ์ง์ ํ ์ ์๋ ๋ฐฉ๋ฒ์ ์ ๊ณตํฉ๋๋ค.
X-HTTP-Method-Override=PUT
X-HTTP-Method-Override=DELETE
Java
๋ณต์ฌ
ํด๋ผ์ด์ธํธ์์ ํค๋์ PUT, DELETE ํค๋ ๊ฐ์ ์ง์ ํ๊ณ ์์ฒญ์ ๋ณด๋ด๋ฉด, ์๋ฒ์ธก์์ POST ๋งคํ์์ ํค๋ ๋งคํ ์กฐ๊ฑด์ ์ฃผ์ด์ PUT, DELETE ๋ฐฉ์์ผ๋ก ์จ ์์ฒญ๋ POST ๋งคํ์ผ๋ก ์ฒ๋ฆฌํ ์ ์๋ค.
Spring Framework ์์ X-HTTP-Method-Override ์ฌ์ฉ
HiddenHttpMethodFilter ๋ฅผ ์ฌ์ฉํ๋ฉด <form> ํ๊ทธ์ POST ์์ฒญ์์
_method ํ๋ผ๋ฏธํฐ๋ก PUT, DELETE ๋ฅผ ์ง์ ํ์ฌ ์ฐํ์ ์ผ๋ก PUT, DELETE ์์ฒญ์ ํ ์ ์๋ค.
โข
web.xml
<filter>
<filter-name>httpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>httpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
XML
๋ณต์ฌ
ํด๋ผ์ด์ธํธ์์ ํผ ์์ฒญ ์ฝ๋
<h3>PUT ์์ฒญ</h3>
<form action="<%= request.getContextPath() %>/request/board" method="post">
<!-- form ํ๊ทธ์์๋ PUT ๋ฐฉ์ ์์ฒญ์ ์ง์ํ์ง ์๋๋ค. -->
<!-- Spring ์ HiddenMethodFilter ๋ฅผ ๋ฑ๋กํ๋ฉด, ์ฐํ์ ์ผ๋ก PUT ์์ฒญ์ ํ ์ ์๋ค. -->
<!-- _method ๋ฅผ X-HTTP-Method-Override ํค๋๋ก ์ง์ ํ๋ค. -->
<!-- X-HTTP-Method-Override ํค๋ - ๊ฐ : PUT -->
<input type="hidden" name="_method" value="PUT" />
<input type="text" name="no" placeholder="๋ฒํธ ์
๋ ฅ" />
<input type="submit" value="์์ " />
</form>
Java
๋ณต์ฌ
ํด๋ผ์ด์ธํธ์์ PUT ์ผ๋ก ๋ช
์ํ ์์ฒญ์ POST ๋ก ์ฒ๋ฆฌ
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.bind.annotation.RequestHeader;
@Controller
@RequestMapping("/board")
public class BoardController {
@RequestMapping(value = "/update", method = RequestMethod.POST, headers = "X-HTTP-Method-Override=PUT")
public String updatePro() {
return "redirect:/board/list"; // board/list.jsp ๋ก ๋ฆฌ๋ค์ด๋ ํธ
}
}
Java
๋ณต์ฌ
ContentType ๋งคํ
๋ฐฉ๋ฒ
@RequestMapping(value = "/๊ฒฝ๋ก", consumes="์ปจํ
์ธ ํ์
")
public String insertPro() {
return "redirect:/board/list";
}
Java
๋ณต์ฌ
Accept ๋งคํ
: Aceept ํค๋ ๋ฅผ ๋งคํ ์กฐ๊ฑด์ผ๋ก ์ฌ์ฉํ๋ ์์ฒญ ๊ฒฝ๋ก ๋งคํ ๋ฐฉ๋ฒ
Aceept ํค๋
: ์๋ต ์ปจํ
์ธ ํ์
์ ์ง์ ํ๋ ํค๋ ์ ๋ณด
ํด๋ผ์ด์ธํธ๊ฐ ์๋ฒ์ ์ํ๋ ์ปจํ
์ธ ํ์
์ ์๋ ค์ฃผ๊ธฐ ์ํด์ ์ฌ์ฉ
์์ฒญ ๋ฉ์์ง์ Accept ํค๋ ๊ฐ์ ๋งคํ ์กฐ๊ฑด์ผ๋ก ์ง์ ํ๊ธฐ ์ํด์๋ produces ์์ฑ์ ์ฌ์ฉํฉ๋๋ค.
๋ฐฉ๋ฒ
@RequestMapping(value = "/๊ฒฝ๋ก", produces="์๋ต์ปจํ
์ธ ํ์
")
public ResponseEntity<List<Board>> listJSON() {
List<Board> boardList = boardService.list();
return new ResponseEntity<List<Board>>(boardList, HttpStatus.OK);;
}
Java
๋ณต์ฌ
JSON ํ์์ ์๋ตํ๊ธฐ ์ํ ์์กด์ฑ
jackson-databind
Jackson ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ์ค์ํ ๋ชจ๋ ์ค ํ๋๋ก, Java ๊ฐ์ฒด์ JSON ๋ฐ์ดํฐ ๊ฐ์ ๋ณํ์ ์ํํ๋ ๋ฐ ์ฌ์ฉ๋๋ Java ๋ผ์ด๋ธ๋ฌ๋ฆฌ์
๋๋ค.
โข
pom.xml
<!-- JSON ๋ฐ์ดํฐ ๋ฐ์ธ๋ฉ์ ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.15.2</version>
</dependency>
XML
๋ณต์ฌ
XML ํ์์ ์๋ตํ๊ธฐ ์ํ ์์กด์ฑ
jackson-dataformat-xml
Jackson ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ํ ๋ถ๋ถ์ผ๋ก์, Java ๊ฐ์ฒด์ XML ๋ฐ์ดํฐ ๊ฐ์ ์ํธ ๋ณํ์ ์ง์ํ๋ Java ๋ผ์ด๋ธ๋ฌ๋ฆฌ์
๋๋ค.
โข
pom.xml
<!-- XML ๋ฐ์ดํฐ ๋ฐ์ธ๋ฉ์ ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ -->
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.15.2</version>
</dependency>
XML
๋ณต์ฌ
JSON ์ผ๋ก Accept ๋งคํ (produces)
@RequestMapping(value = "/board/list", produces="application/json")
public ResponseEntity<List<Board>> listJSON() {
List<Board> boardList = boardService.list();
return new ResponseEntity<List<Board>>(boardList, HttpStatus.OK);
}
Java
๋ณต์ฌ
XML ๋ก Accept ๋งคํ (produces)
@RequestMapping(value = "/board/list", produces="application/xml")
public ResponseEntity<List<Board>> listXML() {
List<Board> boardList = boardService.list();
return new ResponseEntity<List<Board>>(boardList, HttpStatus.OK);
}
Java
๋ณต์ฌ
๋ชจ๋ธ
โข
Model ์ธํฐํ์ด์ค
โข
๋ชจ๋ธ ๋ฑ๋ก
โข
@ModelAttribute
โข
@RequestParam
โข
RedirectAttributes
Model
์ปจํธ๋กค๋ฌ์์ ๋ฑ๋กํ ๋ฐ์ดํฐ๋ฅผ ๋ทฐ์ ์ ๋ฌํ๋ ์ธํฐํ์ด์ค
๋ชจ๋ธ ๋ฑ๋ก
โข
Model ์ธํฐํ์ด์ค์ addAttribute() ๋ฉ์๋๋ก ๋ฐ์ดํฐ๋ฅผ ๋ฑ๋กํ์ฌ, ๋ทฐ์ ์ ๋ฌํ๋ค.
addAttribute() ๋ฉ์๋๋ก ๋ชจ๋ธ์ ๊ฐ์ฒด๋ฅผ ๋ฑ๋กํ๋ค.
model.addAttribute("์์ฑ๋ช
", ๊ฐ); //
Java
๋ณต์ฌ
โข
view.jsp ์์ ${ } ํํ์ธ์ด๋ก ์ ๊ทผํ ์ ์๋ค.
${์์ฑ๋ช
}
Java
๋ณต์ฌ
โข
์์ฑ๊ฐ์ด ๊ฐ์ฒด์ธ ๊ฒฝ์ฐ, getter ๋ฉ์๋๊ฐ ์ ์๋์ด ์์ผ๋ฉด ๊ฐ์ฒด.๋ณ์ ํํ๋ก ์ ๊ทผํ ์ ์๋ค.
${์์ฑ๋ช
.๋ณ์}
Java
๋ณต์ฌ
๋ณ์๋ช
์ ์๋ตํ๋ฉด ๊ฐ์ฒด๋ช
๊ณผ ์ผ์นํ๋ ์ด๋ฆ์ผ๋ก ์ ๊ทผํ ์ ์๋ค.
model.addAttribute(๊ฐ์ฒด๋ช
);
Java
๋ณต์ฌ
โข
view.jsp
${๊ฐ์ฒด๋ช
}
Java
๋ณต์ฌ
โข
ModelController.java
/**
* ์์ฒญ ๊ฒฝ๋ก : /model/person
* @param model
* @param person
* @return
*
* ์ปจํธ๋กค๋ฌ ๋ฉ์๋์์ ๋งค๊ฐ๋ณ์์ ๊ฐ์ฒด๋ฅผ ์ง์ ํ๋ฉด,
* * ์๋ ๊ฐ์ฒด ์์ฑ : ํด๋น ๊ฐ์ฒด๋ฅผ ์คํ๋ง์์ ์๋์ผ๋ก ์์ฑํด์ ์ฃผ์
ํด์ค๋ค.
* * ์๋ ๋ฐ์ธ๋ฉ : ์์ฒญ ํ๋ผ๋ฏธํฐ ๊ฐ์ ๊ฐ์ฒด์ ๋ณ์์ ์๋์ผ๋ก ๋งคํํด์ค๋ค.
* ex) /model/person?name=ALOHA&age=20
* Person.name <---- "ALOHA"
* Person.age <---- 20
*/
@RequestMapping("/model/person") // method: GET(๊ธฐ๋ณธ๊ฐ)
public String person(Model model, Person person) {
// Model ์ธํฐํ์ด์ค์ addAttribute() ๋ฉ์๋๋ก ๋ฐ์ดํฐ๋ฅผ ๋ฑ๋กํ์ฌ, ๋ทฐ์ ์ ๋ฌํ๋ค.
// Model
// : ์ปจํธ๋กค๋ฌ์์ ๋ฑ๋กํ ๋ฐ์ดํฐ๋ฅผ ๋ทฐ์ ์ ๋ฌํ๋ ์ธํฐํ์ด์ค
// Person person = new Person();
// person.setName("๊น์กฐ์");
// person.setAge(20);
// addAttribute("๋ณ์๋ช
", ๊ฐ์ฒด) ๋ฉ์๋๋ก ๋ชจ๋ธ์ ๊ฐ์ฒด๋ฅผ ๋ฑ๋กํ๋ค.
// model.addAttribute("person", person);
model.addAttribute(person); // ๋ณ์๋ช
์ ์๋ตํ๋ฉด ๊ฐ์ฒด๋ช
๊ณผ ์ผ์นํ๋ ์ด๋ฆ์ผ๋ก ์ ๊ทผํ ์ ์๋ค.
// view ์์ ${} (ํํ์ธ์ด) ๋ก ์ฌ์ฉํ ์ ์๋ค.
// ${person.name}, ${person.age}
// getter ๋ฉ์๋๊ฐ ์ ์๋์ด ์์ด์ผ ์์๊ฐ์ด ๋ทฐ์์ ์ ๊ทผํ ์ ์๋ค.
return "model/person"; // view : model/person.jsp
}
Java
๋ณต์ฌ
โข
model/person.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>/model/person</title>
</head>
<body>
<h1>์ด๋ฆ : ${person.name}</h1>
<h1>๋์ด : ${person.age}</h1>
<hr>
<form action="${ pageContext.request.contextPath }/model/person" method="POST">
<input type="text" name="name" value="${person.name}" /> <br>
<input type="text" name="age" value="${person.age}" /> <br>
<input type="submit" value="๋ฑ๋ก" />
</form>
</body>
</html>
HTML
๋ณต์ฌ
@ModelAttribute
๋งค๊ฐ๋ณ์๋ฅผ ๋ชจ๋ธ์ ์๋์ผ๋ก ๋ฑ๋กํด์ฃผ๋ ์ด๋
ธํ
์ด์
/**
* ์์ฒญ ๊ฒฝ๋ก : /model/person2?name=๊น์กฐ์&age=20
* * ์์ฒญ ํ๋ผ๋ฏธํฐ์ ๋ณ์๋ช
๋ค์ ๊ฐ์ฒด์ ํ๋(๋ฉค๋ฒ๋ณ์)๋ก ์๋์ผ๋ก ๋งคํํด์ค๋ค.
* @param person
* @return
*/
@RequestMapping("/model/person2")
public String person2(@ModelAttribute Person person) {
// @ModelAttribute : ๋งค๊ฐ๋ณ์๋ฅผ ๋ชจ๋ธ์ ์๋์ผ๋ก ๋ฑ๋กํด์ฃผ๋ ์ด๋
ธํ
์ด์
// model.addAttribute("person", person);
return "model/person"; // view : model/person.jsp
}
Java
๋ณต์ฌ
@RequestParam
์์ฒญ ๋งค๊ฐ๋ณ์(parameter)๋ฅผ ๋ฉ์๋์ ๋งค๊ฐ๋ณ์๋ก ๋ฐ์ธ๋ฉํ๋ ์ด๋
ธํ
์ด์
/**
* ์์ฒญ ๊ฒฝ๋ก : /model/person
* method : POST
*
* @param person
* @return
*/
@RequestMapping(value = "/model/person", method = RequestMethod.POST)
public String personPost( Model model
, @RequestParam("name") String personName
, @RequestParam("age") int personAge ) {
// @RequestParam("์์ฒญํ๋ผ๋ฏธํฐ๋ช
") ๋ฐ์ดํฐํ์
๋ฉ์๋๋งค๊ฐ๋ณ์๋ช
// : ์์ฒญ ๋งค๊ฐ๋ณ์(parameter)๋ฅผ ๋ฉ์๋์ ๋งค๊ฐ๋ณ์๋ก ๋ฐ์ธ๋ฉํ๋ ์ด๋
ธํ
์ด์
// * ์์ฒญํ๋ผ๋ฏธํฐ๋ช
= ๋ฉ์๋๋งค๊ฐ๋ณ์๋ช
์ผ์นํ๋ฉด, ์๋ต๊ฐ๋ฅ
Person person = new Person();
person.setName(personName);
person.setAge(personAge);
// forward ๋ฐฉ์์ผ๋ก ํ์ด์ง ์ด๋ ์, ๋ชจ๋ธ์ ๋ฐ์ดํฐ ์ฌ์ฉ๊ฐ๋ฅ
model.addAttribute(person);
return "model/person"; // view : model/person.jsp
}
Java
๋ณต์ฌ
RedirectAttributes
๋ฆฌ๋ค์ด๋ ํธ ๋ฐฉ์์ผ๋ก ํ์ด์ง ์ด๋ ์, ์ผํ์ฑ์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ๋ ์ธํฐํ์ด์ค
/**
* ์์ฒญ ๊ฒฝ๋ก : /model/person
* method : POST
*
* @param person
* @return
*/
@RequestMapping(value = "/person", method = RequestMethod.POST)
public String personPost(Model model, Person person, RedirectAttributes rttr) {
// RedirectAttributes ์ธํฐํ์ด์ค
// : ๋ฆฌ๋ค์ด๋ ํธ ๋ฐฉ์์ผ๋ก ํ์ด์ง ์ด๋ ์, ์ผํ์ฑ์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ๋ ์ธํฐํ์ด์ค
// forward ๋ฐฉ์์ผ๋ก ํ์ด์ง ์ด๋ ์, ๋ชจ๋ธ์ ๋ฐ์ดํฐ ์ฌ์ฉ๊ฐ๋ฅ
model.addAttribute(person);
// redirect ๋ฐฉ์์ผ๋ก ํ์ด์ง ์ด๋ ์,
// RedirectAttributes ์ธํฐํ์ด์ค๋ฅผ ํตํด์ ์ผํ์ฑ์ผ๋ก ๋ฐ์ดํฐ ์ ๋ฌ ๊ฐ๋ฅ
rttr.addFlashAttribute("person", person);
// ๋ฆฌ๋ค์ด๋ ํธ : redirect:์์ฒญ๊ฒฝ๋ก
// * ์ต์ด ์์ฒญ ๊ฐ์ฒด๋ ์ฌ๋ผ์ง๋ค.
return "redirect:/model/person"; // view : model/person.jsp
}
Java
๋ณต์ฌ
์๋ต ์ฒ๋ฆฌ
โข
void
โข
String
โข
ModelAndView
โข
ํด๋์ค
โข
์ปฌ๋ ์
โข
ResponseEntity
ResponseController.java
@Controller
//@RequestMapping("/response")
public class ResponseController {
private static final Logger logger
= LoggerFactory.getLogger(ResponseController.class);
@Autowired
private BoardService boardService;
// (๋๋จธ์ง ์ฝ๋๋ ์๋ ์ฝ๋๋ธ๋ญ ํ๋์จ
}
Java
๋ณต์ฌ
void
์์ฒญ ๊ฒฝ๋ก์ ๊ฐ์ ๊ฒฝ๋ก์ view ์๋ตํ๋ค.
/**
* ์์ฒญ ๊ฒฝ๋ก : /response/index
* ์๋ต : /response/index.jsp
*/
@RequestMapping("/response/index")
public void response() {
logger.info("void ํ์
- /response/index");
logger.info("/response/index.jsp ๋ทฐ๋ฅผ ์๋ต");
}
Java
๋ณต์ฌ
String
๋ทฐ ์ด๋ฆ์ ์ง์ ํ์ฌ ์๋ตํ๋ค.
/**
* ์์ฒญ ๊ฒฝ๋ก : /response/view
* ์๋ต : /response/index.jsp
* @return
*/
@RequestMapping("/response/view")
public String responseView() {
logger.info("String ํ์
- /response/index");
logger.info("/response/index.jsp ๋ทฐ๋ฅผ ์๋ต");
logger.info("view ์ด๋ฆ์ ๋ฐํ๊ฐ์ผ๋ก ์ง์ ");
return "response/index";
}
Java
๋ณต์ฌ
ModelAndView
๋ทฐ์ ๋ชจ๋ธ ๋ฐ์ดํฐ๋ฅผ ์ง์ ํ์ฌ ํจ๊ป ๋ฐํ ์ฒ๋ฆฌํ ์ ์๋ ์คํ๋งํ๋ ์์ํฌ ํด๋์ค
/**
* ์์ฒญ ๊ฒฝ๋ก : /response/model/view
* ์๋ต : /response/index.jsp
* @return
*/
@RequestMapping("/response/model/view")
public ModelAndView responseModeAndView() {
// ModelAndView
// ๋ทฐ์ ๋ชจ๋ธ ๋ฐ์ดํฐ๋ฅผ ์ง์ ํ์ฌ ํจ๊ป ๋ฐํ ์ฒ๋ฆฌํ ์ ์๋ ์คํ๋งํ๋ ์์ํฌ ํด๋์ค
logger.info("ModelAndView ํ์
- /response/model/view");
logger.info("/response/index.jsp ๋ทฐ๋ฅผ ์๋ต");
logger.info("๋ชจ๋ธ๊ณผ ๋ทฐ๋ฅผ ModelAndView ๊ฐ์ฒด์ ์ง์ ํ์ฌ ์๋ต");
ModelAndView mv = new ModelAndView();
// ๋ทฐ์ด๋ฆ ์ง์
mv.setViewName("/response/index"); // view : /response/index.jsp
// ๋ชจ๋ธ ๋ฑ๋ก
Board board = new Board("์ ๋ชฉ","์์ฑ์","๋ด์ฉ");
mv.addObject("board", board);
mv.addObject("message", "ModelAndView ์ปจํธ๋กค๋ฌ ์๋ต...");
// model : board, message
return mv;
}
Java
๋ณต์ฌ
ํด๋์ค
์ปจํธ๋กค๋ฌ ๋ฉ์๋์ ํด๋์ค๋ฅผ ๋ฐํํ์
์ผ๋ก ์ง์ ํ๋ ๊ฒฝ์ฐ, ์ผ๋ฐ์ ์ JSON ๋๋ XML ๋ฐ์ดํฐ ํ์์ผ๋ก ์๋ตํ๋ค.
/**
* ์์ฒญ ๊ฒฝ๋ก : /response/data/board
* ์๋ต : board (JSON/XML)
* @ResponseBody : ์๋ตํ๋ ๋ฐ์ดํฐ๋ฅผ ์๋ต ๋ฉ์์ง์ body(๋ณธ๋ฌธ) ์ ์ง์ ํ๋ ์ด๋
ธํ
์ด์
* @return
*/
@ResponseBody
@RequestMapping("/response/data/board")
public Board responseBoard() {
Board board = new Board("์ ๋ชฉ","์์ฑ์","๋ด์ฉ");
return board;
}
Java
๋ณต์ฌ
Chrome ๋ธ๋ผ์ฐ์ ์ ๊ฒฝ์ฐ Accept ํค๋
Accept
text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7
HTML
๋ณต์ฌ
Accept ํค๋ ์ ๋ณด๊ฐ html, xml ์ฐ์ ์์๋ก ๋์ด ์์ด์, ๋ณ๋ค๋ฅธ ํค๋ ์ ๋ณด ์ค์ ์์ด ์์ฒญํ๋ฉด ํด๋์ค๋ฅผ ์๋ตํ๋ ๊ฒฝ์ฐ๋ Accept ํค๋ ์ ๋ณด์ ๋ง์ถฐ ์ฐ์ ์์์ ๋ฐ๋ผ ๋ณํํ ์ ์๋ ํ์
์ผ๋ก ์๋ตํ๋ค.
์ปฌ๋ ์
์ปจํธ๋กค๋ฌ ๋ฉ์๋์ ์ปฌ๋ ์
์ ๋ฐํํ์
์ผ๋ก ์ง์ ํ๋ ๊ฒฝ์ฐ, ์ผ๋ฐ์ ์ JSON ๋๋ XML ๋ฐ์ดํฐ ํ์์ผ๋ก ์๋ตํ๋ค.
/**
* ๋ธ๋ผ์ฐ์ ์์ Accept ํค๋์ ๊ฐ์ด ์ฐ์ ์์
* - text/html
* - application/xhtml+xml
* - application/xml
* - ...
* @return
* @throws Exception
*/
@ResponseBody
// @RequestMapping("/response/data/board/list")
@RequestMapping(value = "/response/data/board/list", produces = "application/json")
public List<Board> responseBoardList() throws Exception {
// produces = "application/json"
// - Accept:application/xml ์ผ๋ก ์์ฒญ์ด ์๋, json ์ผ๋ก ์๋ตํ ์ ์๋ค.
List<Board> boardList = boardService.list();
return boardList;
}
Java
๋ณต์ฌ
@ResponseBody
// @RequestMapping(value = "/resopnse/data/map")
@RequestMapping(value = "/response/data/map", produces = "application/json")
public Map<String, Board> responseMap() throws Exception {
Map<String, Board> map = new HashMap<String, Board>();
map.put("board1", new Board("b1", "์ ๋ชฉ1","์์ฑ์1","๋ด์ฉ1"));
map.put("board2", new Board("b2", "์ ๋ชฉ2","์์ฑ์2","๋ด์ฉ2"));
map.put("board3", new Board("b3", "์ ๋ชฉ3","์์ฑ์3","๋ด์ฉ3"));
return map;
}
Java
๋ณต์ฌ
ResponseEntity
ResponseEntity<Void>
@ResponseBody
@RequestMapping("/response/data/entity/void")
public ResponseEntity<Void> responseEntityVoid() {
// ResponseEntity
// : ์คํ๋ง ํ๋ ์์ํฌ์์ ์๋ต ํค๋,๋ณธ๋ฌธ,์ํ์ฝ๋ ๋ฑ์ ์บก์ํํ๋ ๊ฐ์ฒด
// ResponseEntity<Void>
// : ํค๋์ ๋ณด, ์ํ์ฝ๋๋ฅผ ์ง์ ํ์ฌ ์ฌ์ฉํ ์ ์๋ค.
// HttpStatus ์ด๊ฑฐํ์
// : ์ํ์ฝ๋๋ฅผ ๊ฐ์ง๊ณ ์๋ ์คํ๋งํ๋ ์์ค์ ์ด๊ฑฐํ์
// - OK : 200
// - NOT_FOUND : 404
// - INTERNAL_SERVER_ERROR : 500
return new ResponseEntity<Void>(HttpStatus.OK);
}
Java
๋ณต์ฌ
ResponseEntity<String>
@ResponseBody
@RequestMapping("/response/data/entity/string")
public ResponseEntity<String> responseEntityString() {
// new ResponseEntity<๋ฐํํ์
>(์๋ต๋ฉ์์ง, ์ํ์ฝ๋);
return new ResponseEntity<String>("SUCCESS", HttpStatus.OK);
}
Java
๋ณต์ฌ
ResponseEntity<Board>
@ResponseBody
@RequestMapping("/response/data/entity/board")
public ResponseEntity<Board> responseEntityBoard() {
Board board = new Board("b01","์ ๋ชฉ","์์ฑ์","๋ด์ฉ");
return new ResponseEntity<Board>(board, HttpStatus.OK);
}
Java
๋ณต์ฌ
ResponseEntity<List<Board>>
@ResponseBody
@RequestMapping("/response/data/entity/board/list")
public ResponseEntity<List<Board>> responseEntityBoardList() throws Exception {
List<Board> boardList = boardService.list();
// return new ResponseEntity<>(boardList, HttpStatus.OK); // <> ์๋ต๊ฐ๋ฅ
return new ResponseEntity<List<Board>>(boardList, HttpStatus.OK);
}
Java
๋ณต์ฌ
ResponseEntity<Map<String, Board>>
@ResponseBody
@RequestMapping("/response/data/entity/map")
public ResponseEntity<Map<String, Board>> responseEntityBoardMap() throws Exception {
List<Board> boardList = boardService.list();
Map<String, Board> map = new HashMap<String, Board>();
int i = 1;
for (Board board : boardList) {
map.put("board" + i++, board);
}
return new ResponseEntity<>(map, HttpStatus.OK);
}
Java
๋ณต์ฌ
ResponseEntity<byte[]>
/**
* ํ์ผ ๋ค์ด๋ก๋
*
* @return
* @throws Exception
*/
@ResponseBody
@RequestMapping("/response/data/file")
public ResponseEntity<byte[]> responseFile(HttpServletRequest request) throws Exception {
String path = request.getSession().getServletContext().getRealPath("/WEB-INF/upload/test.png");
logger.info("path : " + path);
// ํ์ผ ๊ฒฝ๋ก
// String filePath = "E:\\TJE\\UPLOAD\\test.jpg";
String filePath = request.getSession().getServletContext().getRealPath("/WEB-INF/upload/test.png");
String fileName = "test.png";
// ํค๋์ ๋ณด
HttpHeaders headers = new HttpHeaders();
headers.setContentType( MediaType.IMAGE_JPEG ); // ์ด๋ฏธ์ง๋ก ์๋ต
// headers.setContentType( MediaType.APPLICATION_OCTET_STREAM); // ์ผ๋ฐ ํ๋ก๊ทธ๋จ ์๋ต
// headers.add("ํค๋๋ช
", "๊ฐ")
// Content-Disposition
// - inline : ์นํ์ด์ง์์ ์ถ๋ ฅ(๊ธฐ๋ณธ๊ฐ)
// - attachment : ์ฒจ๋ถํ์ผ (๋ค์ด๋ก๋)
headers.add("Content-Disposition", "attachment; filename=" + fileName); // ๋ค์ด๋ก๋ ์ฌ๋ถ, ํ์ผ๋ช
์ง์
byte[] fileData = null;
try {
FileInputStream fis = new FileInputStream(filePath);
// commons-io ๋ผ์ด๋ธ๋ฌ๋ฆฌ
// toByteArray() : ํ์ผ์ ๋ฐ์ดํธ์ฝ๋๋ก ๋ณํ
fileData = IOUtils.toByteArray(fis);
} catch (Exception e) {
// TODO: handle exception
}
// new ResponseEntity<๋ฐํํ์
>(์๋ต๋ฉ์์ง, ํค๋,์ํ์ฝ๋);
return new ResponseEntity<byte[]>(fileData, headers, HttpStatus.OK);
}
Java
๋ณต์ฌ
์์ฒญ ์ฒ๋ฆฌ
โข
์ฃผ์ ์ปจํธ๋กค๋ฌ ๋ฉ์๋ ๋งค๊ฐ๋ณ์
โฆ
Model
โฆ
ํด๋์ค
โฆ
์ปฌ๋ ์
โฆ
HttpServletRequest
โฆ
HttpServletResponse
โฆ
RedirectAttribute
โข
์ฃผ์ ์์ฒญ ๋ฐ์ดํฐ ์ฒ๋ฆฌ ์ด๋
ธํ
์ด์
โฆ
@RequestParam
โฆ
@PathVariable
โฆ
@RequestHeader
โฆ
@RequestBody
โฆ
@CookieValue
โข
ํผ ์์ฒญ ์ฒ๋ฆฌ
โฆ
checkbox
โฆ
date
โข
ํ์ผ ์
๋ก๋
โข
AJAX ํ์ผ ์
๋ก๋
์ฃผ์ ์ปจํธ๋กค๋ฌ ๋ฉ์๋ ๋งค๊ฐ๋ณ์
โข
Model
โข
ํด๋์ค
โข
์ปฌ๋ ์
โข
HttpServletRequest
โข
HttpServletResponse
โข
RedirectAttribute
๋งค๊ฐ๋ณ์ | ์ค๋ช
|
Model | ์ปจํธ๋กค๋ฌ์์ ๋ทฐ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ๊ธฐ ์ํ ์ธํฐํ์ด์ค |
ํด๋์ค | ์์ฒญ ํ๋ผ๋ฏธํฐ๋ฅผ ๊ฐ์ฒด๋ก ๋ฐ์ธ๋ฉํ์ฌ ์ฒ๋ฆฌํ๋ ์ปค๋งจ๋ ๊ฐ์ฒด |
์ปฌ๋ ์
| List, Set, Map ๋ฑ์ ์ปฌ๋ ์
ํ์
์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์ฒ๋ฆฌ |
HttpServletRequest | HTTP ์์ฒญ ์ ๋ณด๋ฅผ ์ฒ๋ฆฌํ๊ธฐ ์ํ ์๋ธ๋ฆฟ ์์ฒญ ๊ฐ์ฒด |
HttpServletResponse | HTTP ์๋ต ์ ๋ณด๋ฅผ ์ฒ๋ฆฌํ๊ธฐ ์ํ ์๋ธ๋ฆฟ ์๋ต ๊ฐ์ฒด |
RedirectAttribute | ๋ฆฌ๋ค์ด๋ ํธ ์ ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ๊ธฐ ์ํ ๊ฐ์ฒด |
์ฃผ์ ์์ฒญ ๋ฐ์ดํฐ ์ฒ๋ฆฌ ์ด๋ ธํ ์ด์
โข
@RequestParam
โข
@PathVariable
โข
@RequestHeader
โข
@RequestBody
โข
@CookieValue
์ด๋
ธํ
์ด์
| ์ค๋ช
|
@RequestParam | HTTP ์์ฒญ ํ๋ผ๋ฏธํฐ๋ฅผ ๋ฉ์๋์ ๋งค๊ฐ๋ณ์๋ก ๋ฐ์ธ๋ฉ.
URL์ ์ฟผ๋ฆฌ ์คํธ๋ง์ด๋ ํผ ๋ฐ์ดํฐ๋ฅผ ์ฒ๋ฆฌ |
@PathVariable | URL ๊ฒฝ๋ก์ ์ผ๋ถ๋ฅผ ๋ฉ์๋์ ๋งค๊ฐ๋ณ์๋ก ๋ฐ์ธ๋ฉ.
RESTful API์์ ์ฃผ๋ก ์ฌ์ฉ |
@RequestHeader | HTTP ์์ฒญ ํค๋๋ฅผ ๋ฉ์๋์ ๋งค๊ฐ๋ณ์๋ก ๋ฐ์ธ๋ฉ |
@RequestBody | HTTP ์์ฒญ ๋ณธ๋ฌธ์ ๋ฉ์๋์ ๋งค๊ฐ๋ณ์๋ก ๋ฐ์ธ๋ฉ.
JSON/XML ๋ฑ์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ฒด๋ก ๋ณํ |
@CookieValue | HTTP ์ฟ ํค ๊ฐ์ ๋ฉ์๋์ ๋งค๊ฐ๋ณ์๋ก ๋ฐ์ธ๋ฉ |
ํผ ์์ฒญ ์ฒ๋ฆฌ
โข
checkbox
โข
date
ํ์ผ ์ ๋ก๋
AJAX ํ์ผ ์ ๋ก๋
์์ฒญ ํค๋ ๊ฐ์ ธ์ค๊ธฐ
/**
* ์์ฒญ ํค๋ ๊ฐ์ ธ์ค๊ธฐ
* @return
* * @RequestHeader("ํค๋๋ช
") ํ์
๋ณ์๋ช
*/
@ResponseBody
@RequestMapping(value = "/header", method = RequestMethod.GET)
public String header(@RequestHeader("Accept") String accept
,@RequestHeader("User-Agent") String userAgent
,HttpServletRequest request) {
// @RequestHeader ๋ฅผ ํตํ ํค๋ ์ ๋ณด ๊ฐ์ ธ์ค๊ธฐ
logger.info("[GET] - /request/header");
logger.info("@RequestHeader ๋ฅผ ํตํ ํค๋ ์ ๋ณด ๊ฐ์ ธ์ค๊ธฐ");
logger.info("Accept - " + accept);
logger.info("User-Agent - " + userAgent);
// request ๊ฐ์ฒด๋ก๋ถํฐ ํค๋ ๊ฐ์ ธ์ค๊ธฐ
String requestAccept = request.getHeader("Accept");
String requestUserAgent = request.getHeader("User-Agent");
logger.info("request ๊ฐ์ฒด๋ก๋ถํฐ ํค๋ ๊ฐ์ ธ์ค๊ธฐ");
logger.info("Accept - " + requestAccept);
logger.info("User-Agent - " + requestUserAgent);
return "SUCCESS";
}
Java
๋ณต์ฌ
๊ธฐ๋ณธ ์์ฒญ ์ฒ๋ฆฌ
โข
์์ฒญ ํค๋ ๊ฐ์ ธ์ค๊ธฐ
โข
์์ฒญ ๋ณธ๋ฌธ ๊ฐ์ ธ์ค๊ธฐ
RequestController.java
@Controller // Controller ์ง์ ํ๊ณ ๋น ๋ฑ๋ก
@RequestMapping("/request") // [ํด๋์ค ๋ ๋ฒจ ์์ฒญ๊ฒฝ๋ก ๋งคํ] "/request" ์์ฒญ ๊ฒฝ๋ก๋ก ๋งคํ
public class RequestController {
// log ๊ฐ์ฒด
private static final Logger logger
= LoggerFactory.getLogger(RequestController.class);
}
Java
๋ณต์ฌ
์์ฒญ ํค๋ ๊ฐ์ ธ์ค๊ธฐ
/**
* ์์ฒญ ํค๋ ๊ฐ์ ธ์ค๊ธฐ
* @return
* * @RequestHeader("ํค๋๋ช
") ํ์
๋ณ์๋ช
*/
@ResponseBody
@RequestMapping(value = "/header", method = RequestMethod.GET)
public String header(@RequestHeader("Accept") String accept
,@RequestHeader("User-Agent") String userAgent
,HttpServletRequest request) {
// @RequestHeader ๋ฅผ ํตํ ํค๋ ์ ๋ณด ๊ฐ์ ธ์ค๊ธฐ
logger.info("[GET] - /request/header");
logger.info("@RequestHeader ๋ฅผ ํตํ ํค๋ ์ ๋ณด ๊ฐ์ ธ์ค๊ธฐ");
logger.info("Accept - " + accept);
logger.info("User-Agent - " + userAgent);
// request ๊ฐ์ฒด๋ก๋ถํฐ ํค๋ ๊ฐ์ ธ์ค๊ธฐ
String requestAccept = request.getHeader("Accept");
String requestUserAgent = request.getHeader("User-Agent");
logger.info("request ๊ฐ์ฒด๋ก๋ถํฐ ํค๋ ๊ฐ์ ธ์ค๊ธฐ");
logger.info("Accept - " + requestAccept);
logger.info("User-Agent - " + requestUserAgent);
return "SUCCESS";
}
Java
๋ณต์ฌ
์์ฒญ ๋ณธ๋ฌธ ๊ฐ์ ธ์ค๊ธฐ
/**
* ์์ฒญ ๋ณธ๋ฌธ ๊ฐ์ ธ์ค๊ธฐ
* @param board
* @return
* * @RequestBody
* : HTTP ์์ฒญ ๋ฉ์์ง์ ๋ณธ๋ฌธ(body) ๋ด์ฉ์ ๊ฐ์ฒด๋ก ๋ณํํ๋ ์ด๋
ธํ
์ด์
* ์ฃผ๋ก, ํด๋ผ์ด์ธํธ์์ json ํ์์ผ๋ก ๋ณด๋ธ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ฒด๋ก ๋ณํํ๊ธฐ ์ํด ์ฌ์ฉํ๋ค.
* * ์๋ต๊ฐ๋ฅ (์ฃผ๋ก ์๋ตํ๊ณ ์ด๋ค.)
*
* 415 ์๋ฌ - ์ง์๋์ง ์๋ ๋ฏธ๋์ด ํ์
* (Unsupported Media Type)
* : ํด๋ผ์ด์ธํธ๊ฐ ๋ณด๋ธ ์ปจํ
์ธ ํ์
์ ์์ฒญ์ ์๋ฒ๊ฐ ์ฒ๋ฆฌํ ์ ์์ ๋ ๋ฐ์ํ๋ ์๋ฌ
* [ํด๋ผ์ด์ธํธ] ( application/x-www-form-urlencoded )
* โ
* [ ์ ๋ฒ ] ( application/json )
* * @RequestBody ๋ฅผ ์ฐ๋ฉด, ๋ณธ๋ฌธ์ ์ปจํ
์ธ ํ์
์ application/json ์ ๊ธฐ๋ณธ์ผ๋ก ์ง์
*
* * ๋น๋๊ธฐ ๋๋ thunder client ๋ก ํ
์คํธ ๊ฐ๋ฅ
* Content-Type : application/json
* body { "title" : "์ ๋ชฉ", "writer" : "์์ฑ์", "content" : "๋ด์ฉ" }
*/
@ResponseBody
@RequestMapping(value = "/body", method = RequestMethod.POST)
public String requestBody(@RequestBody Board board) {
logger.info("[POST] - /request/body");
logger.info(board.toString());
return "SUCCESS";
}
Java
๋ณต์ฌ
form ์์ฒญ์ธ ๊ฒฝ์ฐ
ํ๋ผ๋ฏธํฐ ์ด๋ฆ๊ณผ ๊ฐ์ฒด์ ํ๋๋ช
์ ๋งค์นญํ์ฌ Board board ํ์์ผ๋ก ๊ฐ์ฒด๋ฅผ ์๋ ๋ฐ์ธ๋ฉ ํฉ๋๋ค.
JSON ์์ฒญ์ธ ๊ฒฝ์ฐ,
@RequestBody Board board ํ์์ผ๋ก ์ด๋
ธํ
์ด์
์ ๋ช
์ํ์ฌ JSON ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ฒด๋ก ๋ณํํ์ฌ ๋ฐ์ธ๋ฉํด์ผ ํฉ๋๋ค.
์์ฒญ ์ ํ | ์ฒ๋ฆฌ ๋ฐฉ์ | ์์ |
Form ์์ฒญ | ํ๋ผ๋ฏธํฐ ์ด๋ฆ๊ณผ ๊ฐ์ฒด์ ํ๋๋ช
์ ์๋ ๋งค์นญํ์ฌ ๋ฐ์ธ๋ฉ | public String method(Board board) { ... } |
JSON ์์ฒญ | @RequestBody ์ด๋
ธํ
์ด์
์ ์ฌ์ฉํ์ฌ JSON์ ๊ฐ์ฒด๋ก ๋ณํ | public String method(@RequestBody Board board) { ... } |
ํ์ผ ์ ๋ก๋
โข
pom.xml
<!-- commons-io -->
<!-- ํ์ผ ์
๋ก๋/๋ค์ด๋ก๋๋ฅผ ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
<!-- annotation -->
<!-- @Resource ์ด๋
ธํ
์ด์
๋ฑ -->
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
XML
๋ณต์ฌ
โข
web.xml
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- โ
ํ์ผ์
๋ก๋ ๊ธฐ๋ฅ ํ์ฑํ -->
<multipart-config />
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
XML
๋ณต์ฌ
โข
servlet-context.xml
<!-- โ
MultipartResolver ๋น ๋ฑ๋ก -->
<beans:bean id="multipartResolver" class="org.springframework.web.multipart.support.StandardServletMultipartResolver">
</beans:bean>
<!-- โ
์
๋ก๋ ๊ฒฝ๋ก ๋น ๋ฑ๋ก -->
<beans:bean id="uploadPath" class="java.lang.String">
<beans:constructor-arg value="C:\\upload">
</beans:constructor-arg>
</beans:bean>
XML
๋ณต์ฌ
โข
file.jsp
<h3>/file.jsp/h3>
<form action="${ pageContext.request.contextPath }/file" method="POST" enctype="multipart/form-data">
<input type="file" name="file" /> <br>
<input type="submit" value="๋ฑ๋ก" />
</form>
HTML
๋ณต์ฌ
โข
Controller.java
@Resource(name = "uploadPath")
private String uploadPath;
// ํ์ผ ์
๋ก๋
@ResponseBody
@RequestMapping("/file")
public String fileUpload(MultipartFile file) throws IOException {
logger.info("/file");
logger.info("uploadPath : " + uploadPath);
if( file == null ) return "FAIL";
logger.info("originalFileName : " + file.getOriginalFilename());
logger.info("size : " + file.getSize());
logger.info("contentType : " + file.getContentType());
byte[] fileData = file.getBytes();
// ํ์ผ ์
๋ก๋
String filePath = uploadPath;
String fileName = file.getOriginalFilename();
File uploadFile = new File(filePath, fileName);
FileCopyUtils.copy(fileData, uploadFile); // ํ์ผ ์
๋ก๋
return "SUCCESS";
}
Java
๋ณต์ฌ
AJAX ํ์ผ ์ ๋ก๋
ajax.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>AJAX ํ์ผ ์
๋ก๋</h1>
<form action="${ pageContext.request.contextPath }/request/ajax" method="POST"
enctype="multipart/form-data">
<table class="board">
<tr>
<td class="label">์ ๋ชฉ</td>
<td class="data">
<input type="text" name="title" id="title" class="input-text">
</td>
</tr>
<tr>
<td class="label">์์ฑ์</td>
<td class="data">
<input type="text" name="writer" id="writer" class="input-text">
</td>
</tr>
<tr>
<td class="label">๋ด์ฉ</td>
<td class="data">
<textarea name="content" id="content" cols="30" rows="10"
class="input-textarea"></textarea>
</td>
</tr>
<tr>
<td class="label">ํ์ผ</td>
<td class="data">
<input type="file" name="file" id="file" multiple>
</td>
</tr>
</table>
<div class="btn-box">
<div class="item">
<a href="javascript:;" class="btn" id="btn-insert">๋ฑ๋ก</a>
</div>
</div>
</form>
<!-- script -->
<script src="https://code.jquery.com/jquery-3.7.0.min.js"></script>
<script>
// ๋ฑ๋ก ๋ฒํผ - ํด๋ฆญ ์ด๋ฒคํธ
$('#btn-insert').on('click', function() {
// ๋ฑ๋ก ์์ฒญ
let formData = new FormData() // ํผ ๋ฐ์ดํฐ ๊ฐ์ฒด
let title = $('#title').val()
let writer = $('#writer').val()
let content = $('#content').val()
formData.append('title', title)
formData.append('writer', writer)
formData.append('content', content)
// ํ์ผ ์
๋ ฅ ๊ฐ์ ธ์ค๊ธฐ
let file = $('#file')[0]
let files = file.files
// ํ์ผ ๋ฐ์ดํฐ ์ถ๊ฐ
for (let i = 0; i < files.length; i++) {
formData.append('file', files[i])
}
let url = 'http://localhost:8080/spring/request/ajax'
$.ajax({
url : url,
type : 'POST',
data : formData,
contentType : false,
processData : false, // ๋ฐ์ดํฐ ์ปจํ
์ธ ํ์
์๋ ๋ณํ ์ฌ๋ถ
// ๊ธฐ๋ณธ๊ฐ(true)
// true -> contentType : application/x-www-form-urlencoded
// false -> contentType : ๋ฐ์ดํฐ๋ฅผ ์๋์ผ๋ก ์ฒ๋ฆฌํ์ง ์๊ณ ์ง์ ์ง์
success : function(response) {
if( response == 'SUCCESS' ){
alert('๋ฑ๋ก์ด ์๋ฃ๋์์ต๋๋ค.')
}
},
error : function(request, status, error){
alert("code:"+request.status+"\n"+"message:"+request.responseText+"\n"+"error:"+error);
}
})
})
</script>
</body>
</html>
HTML
๋ณต์ฌ
์ปจํธ๋กค๋ฌ ๋ฉ์๋
@ResponseBody
@RequestMapping(value = "/ajax", method = RequestMethod.POST)
public String ajaxUploadPost(Board board) throws IOException {
logger.info("/request/ajax");
logger.info("uploadPath : " + uploadPath);
logger.info(board.toString());
List<MultipartFile> fileList = board.getFile();
if( fileList == null ) return "FAIL";
if( !fileList.isEmpty() )
for (MultipartFile file : fileList) {
logger.info("originalFileName : " + file.getOriginalFilename());
logger.info("size : " + file.getSize());
logger.info("contentType : " + file.getContentType());
byte[] fileData = file.getBytes();
String filePath = uploadPath;
String fileName = file.getOriginalFilename();
File uploadFile = new File(filePath, fileName);
FileCopyUtils.copy(fileData, uploadFile); // ํ์ผ ์
๋ก๋
}
return "SUCCESS";
}
Java
๋ณต์ฌ