[SpringBoot] 기존 servlet-context.xml | JSP, ViewResolver 등

호떡·2022년 10월 26일
0

SpringBoot 적용 전 | servlet-context.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:beans="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
		http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

	<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
	
	<!-- Enables the Spring MVC @Controller programming model -->
	<annotation-driven />

  
  	<!-- 👉 WebConfig.java 
			: 정적 리소스 설정-->
	<resources mapping="/resources/**" location="/resources/" />

  	<!-- 👉 WebConfig.java -->
  	<!-- 👉 application.properties -->
  	<!-- 📌 ViewResolver를 Bean 객체로 등록하는 것은 WebConfig.java에서,
 			ViewResolver property 설정은 application.properties에서 -->
	<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<beans:property name="prefix" value="/WEB-INF/views/" />
		<beans:property name="suffix" value=".jsp" />
	</beans:bean>
	
  
  
	<context:component-scan base-package="com.ssafy.board.controller" />
	
	
	
</beans:beans>

💡 REST를 이용할 때는 아래의 설정들이 필요없게 될 수 있다..?

SpringBoot 적용 후 | 정적 리소스, ViewResolver 객체 설정

WebConfig.java에 설정

package com.ssafy.board.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
	
	/**
	 *  application.properties에 등록해둔 것 가져오기
	 */
	@Value("${spring.mvc.view.prefix}")
	private String prefix;

	@Value("${spring.mvc.view.suffix}")
	private String suffix;

	
	/**
	 * ViewResolver를 Bean 객체로 등록
	 * prefix, suffix (property 설정)은 application.properties에 등록해둔 것 가져와서 쓸 것임
	 * @return
	 */
	@Bean
	public ViewResolver internalResourceViewResolver() {
		InternalResourceViewResolver resolver = new InternalResourceViewResolver();
		resolver.setPrefix(prefix);
		resolver.setSuffix(suffix);
		resolver.setViewClass(JstlView.class);  // pom.xml에 등록해둔 것

		return resolver;
	}
	
	
	/**
	 * 정적 리소스 등록
	 * swagger 등록
	 */
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		//넣지 않았을 때와 넣고 나서  (list2.jsp 에서 이미지를 읽을 수 있는지 없는지가 갈림 확인해볼것)
		registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");

		registry.addResourceHandler("/swagger-ui/**")
				.addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/");
	}
    
}

SpringBoot 적용 후 | ViewResolver 객체 속성 설정

application.properties에 설정

# ViewResolver property setting
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp

SpringBoot 적용 후 | pom.xml

SpringBoot에서는 JSP를 기본적으로 제공하고 있지 않기 때문에 pom.xml에 라이브러리를 추가해주어야 한다.

	<!-- JSP를 활용하기 위해 필요한 라이브러리 -->
	<dependency>
		<groupId>javax.servlet</groupId>
		<artifactId>jstl</artifactId>
	</dependency>
	<dependency>
		<groupId>org.apache.tomcat.embed</groupId>
		<artifactId>tomcat-embed-jasper</artifactId>
	</dependency>

아래의 라이브러리를 추가했을 때 버전 정보는 삭제하였다.
왜? 스프링 부트에서 따로 지정해둔 라이브러리들의 버전이 따로 있기 때문이다. 해당 버전을 사용하면 최고의 호환성을 낸다는 의미에서 지정해둔다고 생각하면 된다.

0개의 댓글