퀴즈
1) HTML을 만드는것은 어렵다.
2) 템플릿은 Spring의 @Controller를 만들수 있게 한다.
3) 템플릿이 더 안전하다.
4) 많은 레이아웃과 시각적 요소들을 재사용할 수 있게 한다.
답: 웹서버가 http 요청을 받고 스프링 컨트롤러에서 메소드를 호출한다. 스프링 컨트롤러는 모델 오브젝트를 채우고 뷰 id를 가진 문자열을 반환한다. 뷰 id에 해당하는 템플릿은 모델 오브젝트의 데이터로 채워진다.
1) 컨트롤러는 템플릿과 데이터를 연결한다.
2) 웹 서버는 사용자가 사용할 수 있는 데이터와 액션을 정의한다.
3) 유저 데이터는 요청 URL안에 인코딩 되거나 요청 메세지의 body로 보내질 수 있다.
4) 컨트롤러 메소드는 모델 오브젝트를 반환하고 웹서버는 템플릿을 선택하는데에 이것을 사용한다.
1) 모델 데이터는 발견가능하고 이용자는 바뀐 이벤트에 대한 업데이트를 받는다.
2) 모든 유저 액션과 뷰 업데이트는 http 요청과 응답 오브젝트로 바뀐다. 컨트롤러는 템플릿을 고르고 모델데이터를 저장한다.
3) 한 구성 요소는 데이터를 검색하여 처리하고, 다른 구성 요소는 시각적 표시 데이터를 포맷하며, 컨트롤러는 처음 두 구성 요소 사이의 흐름을 처리한다. ->
4) 컨트롤러는 모델과 뷰의 마스터이다. 모든 상호 작용은 컨트롤러에 의해 정의되며, 컨트롤러는 모델을 업데이트하고 필요에 따라 보기를 반환한다. -> API 기반 MVC폼
1) 브라우저는 웹서버와 커뮤니케이션 하고, 웹서버는 데이터베이스에 정보를 업데이트하고 브라우저로 응답을 반환한다.
2) OS는 사용자들에게 파일 시스템 컨텐츠를 보여주기 위해 파일 익스플로러를 사용한다.
3) 모바일게임은 게임서버와 커뮤니케이션 하기 위해 터치스크린 인터페이스를 제공하고, 게임서버는 게임상태를 서버에 있는 메모리에 업데이트한다.
4) 핸드폰 시스템은 자동메뉴를 통해 신용카드 지불을 하게 한다.
5) 종업원은 고객의 주문을 받고 주방에 전달한다. 음식이 준비되면 고객에게 가져다준다.
<body>
<p>Welcome to the future!</p>
</body>
답:
p th:text=”${superGreatString}”
1)
<tr th:each="cat : ${cats}">
<td th:text="${cat.color}" />
<td th:text="${cat.maxSpeedMph}" />
</tr>
2)
th:text=”${cat.name} + ‘ is ‘ + {cat.color}”
정답은 아래와 같다.
th:text=”${cat.name + ‘ is ‘ + cat.color}”
3)
th:if="${cat.maxSpeedMph > 10}">Pretty fast
th:elseif="${cat.maxSpeedMph <= 10"> Not as fast
Thymeleaf에서는 elseif가 없고 unless나 switch 등을 사용해야 한다.
4)
<form action="#" th:action="@{'/cat'}" th:object="${cat}" method="POST">
<label for="newCatText">Name your cat: </label>
<input type="text" id="newCatName" name="newCatName" th:field="*{name}">
<input type="submit">
</form>
<body>
<form action="#">
<input type="submit" value="Visit me">
</form>
<h1>Hello, homepage!</h1>
<h1>Welcome back!</h1>
</body>
<body>
<form action="#" th:action="@{'/simplehome'}" method="POST">
<input type="submit" value="Visit me">
</form>
<h1 th:if="${firstVisit}">Hello, homepage!</h1>
<h1 th:unless="${firstVisit}">Welcome back!</h1>
</body>
th:action은 endpoint 지정
POST 메소드 지정
<body>
<form action="#" th:action="@{'/home'}" th:object="${messageForm}" method="GET">
<label for="highFiveText">Send a high five to someone: </label>
<input type="text" id="highFiveText" name="highFiveText" th:field="*{text}">
<input type="submit">
</form>
<h1 th:each="msg : ${greetings}" th:text="${msg}" th:unless="${msg.contains('Goodbye')}">Hello, homepage!</h1>
</body>
@RequestMapping("/home")
public class HomeController {
private MessageListService messageListService;
public HomeController(MessageListService messageListService) {
this.messageListService = messageListService;
}
@GetMapping()
public String lowFive(MessageForm messageForm, Model model) {
messageListService.addMessage("low five.");
model.addAttribute("greetings", messageListService.getMessages());
return "home";
}
@PostMapping()
public String highFive(MessageForm messageForm, Model model) {
messageListService.addMessage("high five, " + messageForm.getText() + "!");
model.addAttribute("greetings", messageListService.getMessages());
return "home";
}
}
답: 처음 페이지를 로딩했을 때 low five를 보여주고, 버튼을 눌렀을때 low five를 두번 보여준다.
GET 메소드이기때문에 GetMapping으로 들어가고, GetMapping과 PostMapping 메소드는 같은 data model을 이용한다.
<body>
<form action="#">
<label for="animalText">Enter an Animal: </label>
<input type="text" id="animalText" name="animalText">
<label for="adjective">Enter an Adjective:</label>
<input type="text" id="adjective" name="adjective">
<input type="submit">
</form>
<h1>Hello, homepage!</h1>
<h1>I think that's enough!</h1>
</body>
<body>
<form action="#" th:action="@{'/animal'}" th:object="${messageForm}" method="POST">
<label for="animalText">Enter an Animal: </label>
<input type="text" id="animalText" name="animalText" th:field="*{animalName}">
<label for="adjective">Enter an Adjective:</label>
<input type="text" id="adjective" name="adjective" th:field="*{adjective}">
<input type="submit">
</form>
<h1 th:unless="${#lists.size(greetings) > 5}" th:each="msg : ${greetings}" th:text="${msg}">Hello, homepage!</h1>
<h1 th:if="${#lists.size(greetings) > 5}">I think that's enough!</h1>
</body>
1)
@PostMapping
public String uploadFile(@RequestParam MultiPartFile file, Model model)
2)
@PostMapping
public String uploadFile(@ModelAttribute FileUploadForm fileuploadForm, Model model)
3)
@GetMapping
public String uploadFile(@RequestParam File file, Model model)
4)
@PostMapping
public String uploadFile(@RequestParam InputStream fileStream, Model model)
5)
@PostMapping
public String uploadFile(@ModelAttribute String filePath, Model model)
1) Spring MVC는 대용량 파일을 업로드하기에 효율적인 방법은 없다.
2) Spring MVC는 브라우저 쿠키 데이터를 보내는데에 효율적인 방법은 없다.
3) Spring MVC 컨트롤러 메소드에서 HTTP 요청 헤더를 추출하는데 효율적인 방법은 없다.
4) Spring MVC 컨트롤러 메소드에서 URL 경로 segent를 변수로 추출하는 효율적인 방법은 없다.
대용량 : MultiPartFile