2022 01 26 - oneso123456789/2022 GitHub Wiki
RedirectAttributes์์ ์๊พธ s๋ฅผ ๋๋ฝํจ s๊ฐ ๋ถ์ด์ผํจ
๊ทธ๋ฆฌ๊ณ ์ ๋ฒ์ DTO๋ฅผ ๊ณต๋ถํ ๋ DTO์ ๋ค๋ฅธ์ด๋ฆ์ด VO๋ผ๊ณ ํ์ง๋ง ์ ํํ๋ ๋ค๋ฅธ๊ฒ์
๋ํ ์ํค๋ง๊ณ gitbook์ ์ด์ฉํด๋ณผ์ง๋ ๊ณ ๋ฏผํด๋ด์ผํจ
-
๋ฐ์ดํฐ ๊ทธ ์์ฒด๋ก ์๋ฏธ ์๋ ๊ฒ์ ๋ด๊ณ ์๋ ๊ฐ์ฒด์ด๋ค.
-
DTO์ ๋์ผํ ๊ฐ๋ ์ด๋ ์ฐจ์ด์ ์ ReadโOnly ์์ฑ ๊ฐ์ฒด์ด๋ค.
-
๊ฐ๋จํ ๋ ๋ฆฝ์ฒด( Entity )๋ฅผ ์๋ฏธํ๋ ์์ ๊ฐ์ฒด๋ฅผ ์๋ฏธํ๋ค.
-
๊ด๊ณ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ๋ ์ฝ๋์ ๋์๋๋ ์๋ฐํด๋์ค์ด๋ค.
-
ํํ๋ Database๋ ์ฝ๋๋ฅผ ๊ตฌ์ฑํ๋ ํ๋๋ค์ VO์ Attribute๋ก ํ๊ณ ํด๋น ๋ณ์์ ์ ๊ทผ ํ ์ ์๋ Getter์ Setter ๋ฉ์๋์ ์กฐํฉ์ผ๋ก ํด๋์ค๋ฅผ ํ์ฑ๋์ด์ง ํด๋์ค์ด๋ค.
-
๊ฑฐ์ ๋ถ๋ณ์ฑ์ ๊ฐ์ง๊ณ equals()๋ก ๋น๊ตํ ๋ ๊ฐ์ฒด์ ๋ชจ๋ ๊ฐ์ ๋น๊ตํด์ผ ํ๋ค.
-
์ ์ก๋๋ ๋ฐ์ดํฐ์ ์ปจํ ์ด๋์ด๋ค.
-
VO์ ๋์ผํ๊ฒ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ์ฌ ์ฌ์ฉํ๋๋ก ํ๋ ๋ถ๋ถ์์ ํ์ํ๋ค.
-
VO์ ๋น๊ต๋ฅผ ํ์ฌ ๋ณด๋ฉด DTO๋ ๊ฐ์ ์์คํ ์์ ์ฌ์ฉ๋๋ ๊ฒ์ด ์๋ ๋ค๋ฅธ ์์คํ ์ผ๋ก ์ ๋ฌํ๋ ์์ ์ ์ฒ๋ฆฌํ๋ ๊ฐ์ฒด์ด๋ค.
-
Layer๊ฐ์ ํต์ ์ฉ๋๋ก ์ค๊ฐ๋ ๊ฐ์ฒด๋ฅผ ๋งํ๊ธฐ๋ ํ๋ค.
-
ํ์ฌ์ ๊ฐ๋ฐ ํ๊ฒฝ์์ ๋ณดํต ๋ฐ์ดํฐ๋ ๋ค์๊ณผ ๊ฐ์ด ํ๋ฆ์ผ๋ก ์ด๋ํ๋ค.
-
์๋ฒ ์ธก : Database Column Data -> DTO -> API(JSON or XML) -> Client
-
ํด๋ผ์ด์ธํธ ์ธก : Server -> API(JSON or XML) -> DTO -> View or Local Database System
-
VO : ์ฌ์ฉ ๋๋ ๊ฐ์ด ๊ฐ์ฒด๋ก ํํ ๋๋ฉฐ, ๊ฐ ๋ณ๊ฒฝ์ด ์๋ ๊ฒฝ์ฐ๋ฅผ ๋งํ๋ค.
-
DTO : ๋ฐ์ดํฐ์ ์ ์ก์ ์ํ ๊ฐ์ฒด์ด๋ฉฐ, ๋น์ง๋์ค ๋ก์ง๊น์ง ๋ด์์ ์ฌ์ฉํ๊ธฐ๋ ํ๋ค.
์ถ์ฒ: https://ijbgo.tistory.com/9 [ํ๋ ๊ฐ๋ฐ์]
WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/] in DispatcherServlet with name 'appServlet'
์ค๋ฅ๊ฐ ์ผ๊ธฐ๋จ 2022-01-26 23์12๋ถ
์๋ง servlet-conxtext.xmlํ์ผ์ ๋ณด๋์ค ์๋์น ์๊ฒ ์ค๋ฅ๊ฐ ์๊ธด๊ฑฐ ๊ฐ์
ํด๊ฒฐ๋ฐฉ๋ฒ์ servlet-context.xmlํ์ผ์ ์คํ์ด์ค๋ฐ๋ฅผ ๋๋ฌ์ ๊ณต๋ฐฑ๋ฌธ์๋ฅผ ๋ฃ์ด์คํ ๋ค์ ์ง์ดํ ์ ์ฅํ๋ ํด๊ฒฐํจ
Model ํ์
๊ณผ ๋๋ถ์ด์ ์คํ๋ง MVC๊ฐ ์๋์ผ๋ก ์ ๋ฌํด ์ฃผ๋ ํ์
์ค์๋ RedirectAttribute ํ์
์ด ์กด์ฌํจ
RedirectAttribute๋ ์กฐ๊ธ ํน๋ณํ๊ฒ๋ ์ผํ์ฑ์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ๋ ์ฉ๋๋ก ์ฌ์ฉํจ
RedirectAttribute๋ ๊ธฐ์กด์ Servlet์์๋ response.sendRedirect()๋ฅผ ์ฌ์ฉํ ๋์ ๋์ผํ ์ฉ๋๋ก ์ฌ์ฉํจ
Servlet์์ redirect๋ฐฉ์
response.sendRedirect("/home?name=crow&age=26);
์คํ๋ง MVC๋ฅผ ์ด์ฉํ๋ redirect์ฒ๋ฆฌ
rttr.addFlashAttribute("name", "crow");
rttr.addFlashAttribute("age", 26);
return "redirect:/";
RedirectAttribute๋ Model๊ณผ ๊ฐ์ด ํ๋ผ๋ฏธํฐ๋ก ์ ์ธํด์ ์ฌ์ฉํ๊ณ , addFlashAttribute(์ด๋ฆ,๊ฐ) ๋ฉ์๋๋ฅผ ์ด์ฉํด์ ํ๋ฉด์ ํ๋ฒ๋ง ์ฌ์ฉํ๊ณ
๋ค์์๋ ์ฌ์ฉ๋์ง ์๋ ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ๊ธฐ ์ํด์ ์ฌ์ฉํจ
์คํ๋ง MVC์ ๊ตฌ์กฐ๊ฐ ๊ธฐ์กด์ ์์๊ณผ ์ธํฐํ์ด์ค์์ ์ด๋ ธํ ์ด์ ์ ์ฌ์ฉํ๋ ๋ฐฉ์์ผ๋ก ๋ณํ ์ดํ์ ๊ฐ์ฅ ํฐ ๋ณํ ์ค ํ๋๋ ๋ฆฌํด ํ์ ์ด ์์ ๋ก์์ง ๋ถ๋ถ์
Controller์ ๋ฉ์๋๊ฐ ์ฌ์ฉํ ์ ์๋ ๋ฆฌํด ํ์ ์ ์ฃผ๋ก ๋ค์๊ณผ ๊ฐ์
- String: jsp๋ฅผ ์ด์ฉํ๋ ๊ฒฝ์ฐ์๋ jsp ํ์ผ์ ๊ฒฝ๋ก์ ํ์ผ์ด๋ฆ์ ๋ํ๋ด๊ธฐ ์ํด์ ์ฌ์ฉํจ
- void: ํธ์ถํ๋ URL๊ณผ ๋์ผํ ์ด๋ฆ์ jsp๋ฅผ ์๋ฏธํจ
- VO, DTO ํ์ :์ฃผ๋ก JSON ํ์ ์ ๋ฐ์ดํฐ๋ฅผ ๋ง๋ค์ด์ ๋ฐํํ๋ ์ฉ๋๋ก ์ฌ์ฉํจ
- ResponseEntity ํ์ : response ํ ๋ Http ํค๋ ์ ๋ณด์ ๋ด์ฉ์ ๊ฐ๊ณตํ๋ ์ฉ๋๋ก ์ฌ์ฉํจ
- Model, ModelAndView: Model๋ก ๋ฐ์ดํฐ๋ฅผ ๋ฐํํ๊ฑฐ๋ ํ๋ฉด๊น์ง ๊ฐ์ด ์ง์ ํ๋ ๊ฒฝ์ฐ์ ์ฌ์ฉํจ(์ต๊ทผ์๋ ๋ง์ด ์ฌ์ฉํ์ง ์์ 2018๋ ๋ ๊ธฐ์ค์ 2022๋ ๋์ ๋ชจ๋ฆ)
- HttpHeaders: ์๋ต์ ๋ด์ฉ ์์ด Http ํค๋ ๋ฉ์์ง๋ง ์ ๋ฌํ๋ ์ฉ๋๋ก ์ฌ์ฉํจ
๋ฉ์๋์ ๋ฆฌํด ํ์ ์ void๋ก ์ง์ ํ๋ ๊ฒฝ์ฐ ์ผ๋ฐ์ ์ธ ๊ฒฝ์ฐ์๋ ํด๋น URL์ ๊ฒฝ๋ก ๊ทธ๋๋ jsp ํ์ผ์ ์ด๋ฆ์ผ๋ก ์ฌ์ฉํจ
๋ฑํ ์ค๋ช
ํ ๋ถ๋ถ์์ด @GetMapping์ "/ex05"๋ก ์ง์ ํด์ฃผ๊ณ ๋ฉ์๋๋ฅผ ๋ง๋ ํ log.info๋ก /ex05๋ผ๋ ๋ก๊ทธ๋ฅผ ๋จ๊ฒจ์ค
๊ทธ๋ฆฌ๊ณ ํด๋น ๊ฒฝ๋ก์ ์ ์์ 404์๋ฌ๊ฐ ํ์๋จ ์ด์ ๋ 'WEB-INF/views/sample/ex05.jsp`๊ฒฝ๋ก์ ex05ํ์ผ์ด ์๊ธฐ๋๋ฌธ์
์ด๊ฒ์ servlet-context.xml์ ์๋์ ์ค์ ๊ณผ ๊ฐ์ด ๋ง๋ฌผ๋ ค URL ๊ฒฝ๋ก๋ฅผ View๋ก ์ฒ๋ฆฌํ๊ธฐ ๋๋ฌธ์ ์๊น
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" /> (prefix๋ ์ ๋์ฌ)
<beans:property name="suffix" value=".jsp" /> (suffix๋ ์ ๋ฏธ์ฌ)
</beans:bean>
void ํ์
๊ณผ ๋๋ถ์ด์ ๊ฐ์ฅ ๋ง์ด ์ฌ์ฉํ๋ ๊ฒ์ผ๋ก String ํ์
์
์ํฉ์ ๋ฐ๋ผ ๋ค๋ฅธ ํ๋ฉด์ ๋ณด์ฌ์ค ํ์๊ฐ ์์๊ฒฝ์ฐ ์ ์ฉํ๊ฒ ์ฌ์ฉ
if~ else์ ๊ฐ์ ์ฒ๋ฆฌ๊ฐ ํ์ํ ์ํฉ
์ผ๋ฐ์ ์ผ๋ก String ํ์
์ ํ์ฌ ํ๋ก์ ํธ์ ๊ฒฝ์ฐ JSP ํ์ผ์ ์ด๋ฆ์ ์๋ฏธํจ
๋ํ ํ๋ก์ ํธ ์์ฑ ์ ๊ธฐ๋ณธ์ผ๋ก ๋ง๋ค์ด์ง HomeController์ ์ฝ๋๋ฅผ ๋ณด๋ฉด String์ ๋ฐํ ํ์
์ผ๋ก ์ฌ์ฉํจ
String ํ์
์๋ ๋ค์๊ณผ ๊ฐ์ ํน๋ณํ ํค์๋๋ฅผ ๋ถ์ฌ์ ์ฌ์ฉํ ์ ์์
- redirect: ๋ฆฌ๋ค์ด๋ ํธ ๋ฐฉ์์ผ๋ก ์ฒ๋ฆฌํ๋ ๊ฒฝ์ฐ
- forward: ํฌ์๋ ๋ฐฉ์์ผ๋ก ์ฒ๋ฆฌํ๋ ๊ฒฝ์ฐ
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model){
logger.info("Welcome home! the client locale is {}.", locale);
Date date = new Date();
DateFormat dateformat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
String formattedDate = dateFormat.format(date);
model.addattribute("serverTime", formattedDate );
return "home";
}
home ๋ฉ์๋์ ํ๋ผ๋ฏธํฐ๋ Locale์ Model๋ก ๊ทธ์ค Locale๋ ์ง์ญ์ ์ธ์ด,๋๋ผ๋ฑ์ ๋ด๊ณ ์๋ ์ ๋ณด์
logger.info๋ฅผ ์ด์ฉํด์ ๋ก๊ทธ์ ํ์ํ๋ค๋ ๋ฉ์์ง๋ฅผ ๋จ๊ธฐ๋๋ ์ด๋ ์ฌ์ฉํด์ ๋๋ผ๋ฅผ ํ์ํด์ค
INFO : com.crow.controller.HomeController - Welcome home! The client locale is ko_KR.
๋ํ ๋ ์ง๋ฅผ ํ์ํ๊ธฐ ์ํด์ Date์ DateFormat์ ์ฌ์ฉํจ
ํฌ๋งท์ getDateTimeInstance๋ฅผ ์ด์ฉํด์ ๋ ์ง์ ์๊ฐ๊น์ง ๊ฐ์ ธ์์ค๋ค ๋ํ ๋ ์ง๋ฅผ ๋กฑ ํํ๋ก ์๊ฐ์ญ์ ๋กฑ ํํ๋ก ํด์ฃผ๊ณ
๋ง์ง๋ง์ผ๋ก ๋ก์ปฌ์ ํ๊ตญ์ผ๋ก ํ์ํด์ค๋ค
(๋ ์ง๋ง ๊ฐ์ ธ์ค๋ ค๋ฉด getDateInstance๋ฅผ ์ด์ฉ ์๊ฐ๋ง ๊ฐ์ ธ์ฌ๊บผ๋ฉด getTimeInstance)
DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, locale)
The time on the server is 2022๋
1์ 27์ผ ๋ชฉ์์ผ ์ค์ 2์ 06๋ถ 55์ด KST.
DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
The time on the server is 2022๋
1์ 27์ผ (๋ชฉ) ์ค์ 2์ 07๋ถ 55์ด.
DateFormat.FULL๊ณผ DateFormat.LONG์ ์ฝ๊ฐ์ ์ฐจ์ด๊ฐ ์กด์ฌํ์ง๋ง ๊ธฐ๋ณธ๊ฐ์ธ LONG์ ์ฌ์ฉํด๋ ๋ณ๋ก ๋ถํธํ์ ์ ์์ด๋ณด์ธ๋ค
๋ํ ์ด ํฌ๋งท์ Stringํ์ ๋ณ์ formattedDate์ ๋ฃ์ด์ค์ model์ addAttribute๋ก'serverTime'์์ฑ์ ๋ฃ์ด์ค๋ค.
๊ทธ๋ค ๊ฒฐ๊ณผ๊ฐ์ผ๋ก "home"์ ๋ฆฌํดํด์ค๋ค
home() ๋ฉ์๋๋ 'home'์ด๋ผ๋ ๋ฌธ์์ด์ ๋ฆฌํดํ๊ธฐ ๋๋ฌธ์ ๊ฒฝ๋ก๋ '/WEB-INF/views/home.jsp'๊ฒฝ๋ก๊ฐ ๋จ
์ฐธ๊ณ :https://promobile.tistory.com/197
Controller์ ๋ฉ์๋ ๋ฆฌํด ํ์
์ VO(Value Object)๋ DTO(Data Transfer Objcet)ํ์
๋ฑ
๋ณตํฉ์ ์ธ ๋ฐ์ดํฐ๊ฐ ๋ค์ด๊ฐ ๊ฐ์ฒด ํ์
์ผ๋ก ์ง์ ํ ์ ์๋๋ฐ, ์ด ๊ฒฝ์ฐ๋ ์ฃผ๋ก JSON ๋ฐ์ดํฐ๋ฅผ ๋ง๋ค์ด ๋ด๋ ์ฉ๋๋ก ์ฌ์ฉํจ
์ฐ์ ์ด๋ฅผ ์ํด์ jackson-databind ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ pom.xml์ ์ถ๊ฐํจ
๊ฐ๋ฐ์ ๋๊ตฌ๋ฅผ ํตํด์ ์ดํด๋ณด๋ฉด(๊ฐ๋ฐ์ ๋๊ตฌ ๋คํธ์ํฌ์ชฝ) ์๋ฒ์์ ์ ์กํ๋ MIME ํ์
์ด Content-Type: application/json;charset=UTF-8
์ผ๋ก ์ฒ๋ฆฌ๋จ
๋ง์ผ Jackson-databind ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ํฌํจ๋์ง ์์๋ค๋ฉด 500 internal Server Error๋ฅผ ๋ณด๊ฒ๋จ
์คํ๋ง MVC๋ ๋ฆฌํด ํ์
์ ๋ง๊ฒ ๋ฐ์ดํฐ๋ฅผ ๋ณํํด ์ฃผ๋ ์ญํ ์ ์ง์ ํ ์ ์๋๋ฐ ๊ธฐ๋ณธ์ ์ผ๋ก JSON์ ์ฒ๋ฆฌ๊ฐ ๋๋ฏ๋ก ๋ณ๋์ ์ค์ ์ด ํ์๊ฐ ์์
์คํ๋ง 3๋ฒ์ ๊น์ง๋ ๋ณ๋์ Converter๋ฅผ ์์ฑํด์ผ ํ์
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.4</version>
</dependency>
2.9.4 ๋ฒ์ ์ ๋ง์ ๋ณด์๋ฌธ์ ๊ฐ ์์ง๋ง ์ผ๋จ ์์ ์ ๊ฐ์๊ฒฐ๊ณผ๋ฅผ ๊ฐ์ถ๋๊ฒ ์ค์ํ๋ฏ๋ก ์ผ๋จ ์ฌ์ฉํ๊ธฐ๋กํจ
@GetMapping("/ex06")
public @ResponseBody SampleDTO ex06() {
log.info("ex06......");
SampleDTO dto = new SampleDTO();
dto.setAge(26);
dto.setName("Crow");
return dto;
}
๋ฉ์๋์ ๋ฆฌํดํ์
์ SampleDTO๊ณ ํ๋ผ๋ฏธํฐ๋ ์์
SampleDTO์์ @ResponseBody ์ด๋
ธํ
์ด์
์ ์ ์ธํด ์ค์ผ์ง๋ง JSONํํ๋ก ํ๋ก์ ํธ ๊ฒฝ๋ก/ex06์ JSONํํ๋ก ๊ฒฐ๊ณผ๋ฅผ ๋ณด์ฌ์ค
๋๋ฝ์ ๊ทธ๋ฅ ์ด๊น์์ด 404 ์๋ฌํ์๊ฐ๋ธ
URL๋ก ํธ์ถ์ ๋ก๊ทธ๋ฅผ ๊ธฐ๋กํ๊ณ SampleDTO๋ฅผ importํด์ set๋ฉ์๋๋ก age์ name์ ๊ฐ์ ๋ฃ์ด์ค