jar -> war
web 의존성 추가
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Spring14Application.class);
}
}
도메인 클래스
public class Event {
private String name;
private LocalDateTime starts;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public LocalDateTime getStarts() {
return starts;
}
public void setStarts(LocalDateTime starts) {
this.starts = starts;
}
}
컨트롤러 클래스
@Controller
public class EventController {
@GetMapping("/event")
public String getEvents(Model model){
Event event1 = new Event();
event1.setName("스프링 웹 MVC 스터디 1");
event1.setStarts(LocalDateTime.of(2019, 1, 15, 10, 0));
Event event2 = new Event();
event2.setName("스프링 웹 MVC 스터디 2");
event2.setStarts(LocalDateTime.of(2019, 1, 22, 12, 0));
List<Event> events = List.of(event1, event2);
model.addAttribute("events", events);
model.addAttribute("message", "Happy New Year!");
return "events/list";
}
}
jsp 생성(WEB-INF/jsp/events/list.jsp)
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>이벤트 목록</h1>
<h2>${message}</h2>
<table>
<tr>
<th>이름</th>
<th>시작</th>
</tr>
<c:forEach items="${events}" var="event">
<tr>
<td>${event.name}</td>
<td>${event.starts}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
그러나 스프링부트는 JSP를 권장하지 않는다.
“If possible, JSPs should be avoided. There are several known limitations when using them with embedded servlet containers.”
제약 사항
참고
java -jar를 사용해서 실행하기
서블릿 컨테이너에 배포하기
참고