Spring Boot JSP 가이드 - 설정부터 Form 처리, 유효성 검증까지

Published: (December 30, 2025 at 11:20 PM EST)
3 min read
Source: Dev.to

Source: Dev.to

Introduction

Spring Boot는 기본적으로 Thymeleaf를 권장하지만, 레거시 프로젝트나 특정 요구사항에 따라 JSP를 사용해야 할 때도 있습니다. 이 글에서는 Spring Boot에서 JSP를 설정하고 활용하는 방법을 알아봅니다.

Dependencies

Gradle

implementation 'org.apache.tomcat.embed:tomcat-embed-jasper'
implementation 'javax.servlet:jstl'

Maven

<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <scope>provided</scope>
</dependency>

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
</dependency>

Application Properties

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

Note: src/main/webapp 디렉터리가 클래스패스에 포함되어야 합니다.

JSP Setup

Simple JSP with EL

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Welcome</title>
</head>
<body>
    <h2>안녕하세요, ${name}님!</h2>
</body>
</html>

JSTL Taglibs

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>

Table Example

<table>
    <thead>
        <tr>
            <th>설명</th>
            <th>마감일</th>
            <th>상태</th>
        </tr>
    </thead>
    <tbody>
        <c:forEach var="todo" items="${todos}">
            <tr>
                <td>${todo.desc}</td>
                <td>${todo.targetDate}</td>
                <td>${todo.status}</td>
            </tr>
        </c:forEach>
    </tbody>
</table>

<c:out value="${message}" />

<select name="status">
    <option value="completed">완료</option>
    <option value="inProgress">진행중</option>
</select>

Spring Form Tag

<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>

<form:form modelAttribute="todo" method="post">
    <table>
        <tr>
            <td>설명</td>
            <td><form:input path="desc" /></td>
        </tr>
        <tr>
            <td>마감일</td>
            <td><form:input path="targetDate" /></td>
        </tr>
    </table>
    <input type="submit" value="저장" />
</form:form>

Hidden Field (전송되어야 하는 값)

<form:hidden path="id" />

Controller Example

@Controller
public class TodoController {

    @GetMapping("/add-todo")
    public String showAddTodoPage(ModelMap model) {
        model.addAttribute("todo", new Todo(0, "", "Default Desc", new Date(), false));
        return "todo";
    }

    @PostMapping("/add-todo")
    public String addTodo(ModelMap model, Todo todo) {
        // Todo 저장 로직
        service.addTodo(todo);
        return "redirect:/list-todos";
    }
}

Validation

Entity with Constraints

public class Todo {
    private int id;
    private String user;

    @Size(min = 10, message = "최소 10자 이상 입력해주세요.")
    private String desc;

    @NotNull(message = "마감일은 필수입니다.")
    @Future(message = "마감일은 미래 날짜여야 합니다.")
    private Date targetDate;
}

Controller with @Valid

@PostMapping("/add-todo")
public String addTodo(ModelMap model,
                     @Valid Todo todo,
                     BindingResult result) {

    if (result.hasErrors()) {
        return "todo"; // 에러가 있으면 폼 페이지로 돌아감
    }

    service.addTodo(todo);
    return "redirect:/list-todos";
}

Displaying Validation Errors

<form:errors path="desc" cssClass="error" />
<form:errors path="targetDate" cssClass="error" />

Custom Date Binding

@Controller
public class TodoController {

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
    }
}

Date Formatting in JSP

<fmt:formatDate value="${todo.targetDate}" pattern="dd/MM/yyyy" />

Datepicker Integration

Dependency

<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>bootstrap-datepicker</artifactId>
    <version>1.0.1</version>
</dependency>

JSP Usage

<script>
    $('#targetDate').datepicker({
        format: 'dd/mm/yyyy'
    });
</script>

Conclusion

Spring Boot에서 JSP를 사용하려면 추가 설정이 필요하지만, JSTL과 Spring Form 태그를 활용하면 강력한 웹 페이지를 구축할 수 있습니다. 서버‑사이드 유효성 검증을 통해 데이터 무결성을 보장하고, Fragment를 활용하여 코드 중복을 줄이세요. 다만, 새로운 프로젝트에서는 Thymeleaf 사용을 권장합니다.

Back to Blog

Related posts

Read more »

Spring Security 시작하기 - 기본 설정과 인증

기본 설정 의존성 추가 Spring Security를 사용하려면 의존성만 추가하면 됩니다. 추가하는 것만으로 기본 보안이 활성화됩니다. Maven xml org.springframework.boot spring-boot-starter-security Gradle gradle imple...