→ 스프링 부트는 8080 포트 번호를 사용해 내장 웹 서버를 실행함!
서버 포트번호 변경하기
- application.properties 파일에서 별도 설정 필요
server.port = 7070
정적 파일
- ./src/main/resources/static은 정적 파일 위치
템플릿 엔진 (ex. JSP, Thymeleaf, FreeMarker, ...)
package sesac.sesacspringboot.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HelloController {
@GetMapping("/hi")
public String getHi(Model model) {
model.addAttribute("msg", "Hi~");
return "hi";
}
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p th:text="'안녕하세요. ' + ${msg}">안녕 반가워</p>
</body>
</html>
WAS란?
Spring Boot 설명 회고록
- Spring Boot의 주요 특징 중 하나
- WAS (Web Application Server): 웹 애플리케이션 실행 장치
<p th:text="'안녕하세요. ' + ${hello}">안녕 반가워</p>
<p th:utext="${uText}"></p>
<input type="text" th:value="${value}">
<p th:with="temp=${withValue}" th:text="${temp}"></p>
<a th:href="@{/{id}(id=${link})}">Link1 (hi)</a><br>
<a th:href="@{/hi}">Link2 (hi)</a><br>
<a th:href="@{http://www.google.com}">Google</a><br>
<img th:src="${imgSrc}">
<div th:[속성]="서버에서 받는 값 및 조건식" />
<span th:text="${hello}">message</span>
<button th:value="${hello}" />
<div th:with="temp=${hello}" th:text="${temp}" />
<div th:switch="${userRole}">
<p th:case="'admin'">Hello, Admin</p>
<p th:case="'user'">Hello, User</p>
<p th:case="*">Hello, Unknown</p>
</div>
<div>
<p th:if="${hello}=='web'" th:text="${hello}"></p>
<p th:unless="${hello}=='web'" th:text="unless입니다."></p>
</div>
String[] names = {"kim", "lee", "hong", "park", "shin"};
model.Attribute("names", names);
return "05_Thymeleaf";
<ul>
<li th:each="name:${names}" th:text="${name}"></li>
</ul>
RESTful 이란?
- REST의 원리를 따르는 시스템
- 원리를 따르기만 하면 다 RESTful이 아님
- 원리를 따르면서 REST API 설계 규칙을 올바르게 지킨 시스템이 RESTful한 시스템
Data Transfer Object
import lombok.Getter;
import lombok.Setter;
// 별도의 Getter, Setter 구현 없이 사용하게 해주는 어노테이션
@Getter
@Setter
public class DTOExample {
private String title;
private String content;
private String writer;
}
Value Object
DTO vs VO
@GetMapping(url 주소)
public String 함수이름() {
return 템플릿 파일 명;
}
@GetMapping(url 주소)
public String 함수 이름(@RequestParam(value="key") String key) {
return 템플릿 파일 명;
}
@GetMapping(url 주소/{value})
public String 함수 이름(@PathVariable String value) {
return 템플릿 파일 명;
}
@GetMapping(url 주소/{value}/{value2})
public String 함수 이름(@PathVariable String value, @PathVariable("value2") String abc) {
return 템플릿 파일 명;
}
@GetMapping("/get/response4/{name}/{age}")
public String getResponse4(@PathVariable(value="name") String n,
@PathVariable(required = false) String age,
Model model) {
model.addAttribute("name", n);
model.addAttribute("age", age);
return "response";
}
@PostMapping("/post/response1")
public String postResponse1(@RequestParam(value="name") String name, Model model) {
model.addAttribute("name", name);
return "response";
}
@GetMapping("response-string")
@ResponseBody
public String getString(@RequestParam("name") String name) { return "hello " + name; }
@PostMapping("/post/response3")
@ResponseBody
// return 하는 문자열의 template 파일을 불러오는 게 아니라
// return 하는 문자열 그대로 값을 전달하는 것
public String postResponse3(@RequestParam(value="name", required = false) String name, Model model) {
model.addAttribute("name", name);
return "response";
}
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class UserDTO {
private String name;
private String age;
}
@GetMapping("/dto/response1")
@ResponseBody
public String dtoResponse1(@ModelAttribute UserDTO userDTO) {
// 변수로 값을 하나씩 가져오는 게 아니라 객체에 값을 담아서 가져오고 싶을 때 사용하는 방법
// @ModelAttribute : html 폼 데이터를 컨트롤러에 전달할 때 객체에 매핑해주는 친구 (매핑 = setter 함수를 실행한다)
String msg = "이름 : " + userDTO.getName() + ", 나이 : " + userDTO.getAge();
return msg;
}
@PostMapping("/dto/response2")
@ResponseBody
public String dtoResponse2(UserDTO userDTO) {
// 아무것도 없는 상태 = @ModelAttribute 상태
String msg = "이름 : " + userDTO.getName() + ", 나이 : " + userDTO.getAge();
return msg;
}
@PostMapping("/dto/response3")
@ResponseBody
public String dtoResponse3(@RequestBody UserDTO userDTO) {
// @RequestBody : 요청의 본문에 있는 데이터(body)를 받아와서 객체에 매핑( 필드값에 값을 주입 )
// 전달받은 요청의 형식이 json 또는 xml 일 때만 실행이 가능
// 일반 폼 전송 = www-x-form-urlencoded -> 즉, Requestbody는 일반폼 전송의 형태를 처리할 수 없음
String msg = "이름 : " + userDTO.getName() + ", 나이 : " + userDTO.getAge();
return msg;
}
import lombok.Getter;
@Getter
public class UserVO {
public String name, age;
}
// Q. get 방식 - vo = null ( modelAttribute = setter 함수를 실행)
// post 방식 - vo = null
// post 방식 - vo - requestbody = 오류
@GetMapping("/vo/response1")
@ResponseBody
public String voResponse1(UserVO userVo) {
String msg = "이름 : " + userVo.getName() + ", 나이 : " + userVo.getAge();
return msg;
}
@PostMapping("/vo/response2")
@ResponseBody
public String voResponse2(UserVO userVo) {
String msg = "이름 : " + userVo.getName() + ", 나이 : " + userVo.getAge();
return msg;
}
@PostMapping("/vo/response3")
@ResponseBody
public String voResponse3(@RequestBody UserVO userVo) {
String msg = "이름 : " + userVo.getName() + ", 나이 : " + userVo.getAge();
return msg;
}
front 코드 (GitHub)
back 코드 (GitHub)
일반 폼 전송 시
DTO
VO
- RequestBody는 전달받은 요청의 형식이 json 또는 xml 일 때만 실행이 가능
- 일반 폼 전송 = www-x-form-urlencoded → 즉, Requestbody는 일반폼 전송의 형태를 처리할 수 없음
DTO
VO