개발자 구겹이

스프링부트 jsp 렌더링 관련 설정 본문

java/springboot

스프링부트 jsp 렌더링 관련 설정

@layers9 2025. 5. 2. 19:25

jsp는 동적인 데이터를 웹화면에 출력되도록 하는 고전적인 방법인가보다

좀 더 업그레이드가 된 스프링부트 버전은 jsp보다 thymleaf가 추천되는거 같다

 

조금 겪어본 결과, 타임리프는 개발환경에서 결과를 느리게 확인할 수 있는고구마 타입의 것이다 그래서 jsp렌더링과 친해지기로 했다 

 

[스프링부트에서 jsp 렌더링을 위해 필요한 설정 사항들]1. 의존성 추가 + war 패키징 설정2. application.properties 설정 <- resource폴더 하위에 속함3. web.xml, root-context.xml, servlet-context.xml 추가 및 설정

 

Spring boot Project
1. 의존성 추가 + war 패키징 설정 

<packaging>war</packaging>

■ project 태그의 자식으로 packaging 태그를 추가해준다. jsp를 사용하기 위해서는 jar가 아닌 war로 패키징이 이루어지도록 해야 한다고 함


<dependency>
    <groupId>jakarta.servlet.jsp.jstl</groupId>
    <artifactId>jakarta.servlet.jsp.jstl-api</artifactId>
    <version>3.0.0</version>
</dependency>


<dependency>
    <groupId>org.glassfish.web</groupId>
    <artifactId>jakarta.servlet.jsp.jstl</artifactId>
    <version>3.0.1</version>
</dependency>


<dependency>
    <groupId>org.glassfish.web</groupId>
    <artifactId>jstl-impl</artifactId>
    <version>1.2</version>
    <scope>runtime</scope>
</dependency>

<dependency>
    <groupId>org.glassfish</groupId>
    <artifactId>jakarta.el</artifactId>
    <version>4.0.2</version>
</dependency>

 

■ javax > jakarta

 

 

2. application.properties 설정 <- resource폴더 하위에 속함

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

 

3. web.xml, root-context.xml, servlet-context.xml 추가 및 설정

web.xml <- webapp 폴더의 바로 하위 자식으로 추가

                  root-context, servlet-context.xml의 경로를 알려주기

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                             http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <!-- Spring ContextLoaderListener 설정 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Spring DispatcherServlet 설정 -->
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!-- Root context 설정 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

</web-app>

■ 관련해서 빈 생성 에러가 날 때, root, servlet   -context.xml 코드를 살펴보기