일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- c#
- DP
- 그리디 알고리즘
- JPA
- 백준
- 연관관계
- BFS
- 인프런
- python3
- 파이썬
- popleft
- appendleft
- C#강의
- mvc
- 프로그래머스
- Java
- 우선순위큐
- 브루투포스
- pypy3
- 완전탐색
- 합 구하기
- LCM
- 소수판별
- 소수찾기
- deque
- Python
- unity
- 누적합
- spring
- 1일1솔
Archives
- Today
- Total
jae_coding
(Spring, Java) MVC, 템플릿 엔진, port변경 본문
반응형
1. MVC
model, view, controller의 약자로 3가지의 역할로 구분한 개발 방법론.
1) model: 데이터를 가진 객체로서, 모델의 상태 변화에 따라 컨트롤러와 뷰에게 전달해줌.
2) view: 사용자가 볼 결과물을 생성하기 위해 모델로부터 정보를 가져오는 것.
3) controller: 사용자가 접근한 Url의 데이터를 파악하여 model에 전달한 후 view에서 반영하여 사용자에게 보여줌.
2. 템플릿 엔진
내장 서버인 톰켓 서버에서 static보다 우선 순위가 높은 controller를 거쳐 key 값이 있다면 Controller내의 key.html을 출력하게 된다. 그렇기 때문에 예제에서 hello-mvc가 Key이기 때문에 param을 통해서 localhost:{port}/key ? name = 입력하고싶은 string입력을 해준다.
결론 :hello-mvc가 있다면 hello-template을 반환하기 때문에 controller에서 viewResolver로 반환한 값의 html을 그려준다.
위 다이어그램은
http://localhost:8080/hello-mvc?name=spring
을 웹사이트에 입력해주었을 경우의 다이어 그램이다.
3. 포트번호 변경하기
src > main > resources > application.properties
의 내용을 변경하여 주면된다.
server.port=9090
저의 경우에는 9090포트를 사용하였다.
4. 코드
1) Controller
package hello.hellospring.controller;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class HelloController {
@GetMapping("hello")
public String hello(Model model){
// key: data, value: hello!!
model.addAttribute("data", "hello!");
// return name: hello (resources > templates > hello.html)
return "hello";
}
@GetMapping("hello-mvc")
// input: param을 통해서 localhost:{port}/key ? name = 입력하고싶은 string입력
public String hellowMvc(@RequestParam("name") String name, Model model){
model.addAttribute("name", name);
return "hello-template";
}
}
2) Template
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<body>
<p th:text="'hello ' + ${name}">hello! empty</p>
</body>
</html>
반응형
'Spring, java' 카테고리의 다른 글
[스프링 핵심 원리]객체 지향 설계 & 스프링 (0) | 2022.08.24 |
---|---|
Spring 매개변수 출력하기 (0) | 2022.08.18 |
(Spring, Java) Build and Run (0) | 2022.08.11 |
(Spring, Java) Welcome Page (static, dynamic) (0) | 2022.08.11 |
(Spring, java) 시작하기, 설정하기 (0) | 2022.08.11 |
Comments