Day081

RISK_TAKER·2023년 5월 25일
0

Spring Project 관련 설정

google > mvn repository

  1. Java Servlet API » 4.0.1 > maven

  2. JavaServer Pages(TM) API » 2.3.3 > maven

  3. JSTL » 1.2

	<dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
	</dependency>
  1. Spring Web MVC » 5.2.24.RELEASE

pom.xml

	<properties>		
		<javax.servlet-version>4.0.1</javax.servlet-version>
		<javax.servlet.jsp-version>2.3.3</javax.servlet.jsp-version>
		<javax.servlet.jsp.jstl-version>1.2</javax.servlet.jsp.jstl-version>
		<org.springframework-version>5.2.24.RELEASE</org.springframework-version>
	</properties>

	<dependencies>

		<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>${javax.servlet-version}</version>
			<scope>provided</scope>
		</dependency>

		<!--
		https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
		<dependency>
			<groupId>javax.servlet.jsp</groupId>
			<artifactId>javax.servlet.jsp-api</artifactId>
			<version>${javax.servlet.jsp-version}</version>
			<scope>provided</scope>
		</dependency>

		<!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/jstl -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>${javax.servlet.jsp.jstl-version}</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>

	</dependencies>

WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="4.0"
	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_4_0.xsd">
                       
	<servlet>
		<servlet-name>appServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
		</init-param>
		
		<load-on-startup>1</load-on-startup>
	</servlet>
	
	<servlet-mapping>
		<servlet-name>appServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/spring/root-context.xml</param-value>
	</context-param>
	
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	
	<filter>
		<filter-name>encodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
		<init-param>
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	
	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
</web-app>

/WEB-INF/spring/appServlet/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
			 					 http://www.springframework.org/schema/mvc/spring-mvc.xsd
			 					 http://www.springframework.org/schema/beans
			 					 http://www.springframework.org/schema/beans/spring-beans.xsd
			 					 http://www.springframework.org/schema/context
			 					 http://www.springframework.org/schema/context/spring-context.xsd">

	<annotation-driven/>
	
	<context:component-scan base-package="com.app.controller"></context:component-scan>
  
  	<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<beans:property name="prefix" value="/WEB-INF/views/"></beans:property>
		<beans:property name="suffix" value=".jsp"></beans:property>
	</beans:bean>
  
	<resources location="/resources/" mapping="/**"></resources>
	
</beans:beans>

/WEB-INF/spring/root-context.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
	  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	  xsi:schemaLocation="http://www.springframework.org/schema/beans
	  				      http://www.springframework.org/schema/beans/spring-beans.xsd">

</beans>

ViewResolver

  • Controller에서 화면에 대한 정보를 return할 때 앞에 붙는 경로나 뒤에 붙는 확장자를 자동으로 붙여주어 편하게 해주는 역할을 한다.
  • /WEB-INF/spring/appServlet/servlet-context.xml 파일에 다음을 작성한다.
	<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<beans:property name="prefix" value="/WEB-INF/views/"></beans:property>
		<beans:property name="suffix" value=".jsp"></beans:property>
	</beans:bean>

webapp/resources 폴더 생성

이미지나 영상 같은 자료들을 모아두는 경로

  • /WEB-INF/spring/appServlet/servlet-context.xml 에 아래의 코드를 작성한다.
<resources location="/resources/" mapping="/**"></resources>

Annotation

  • @Controller : 컨트롤러 클래스명 바로 위에 작성한다.

  • @GetMapping("/login") = @RequestMapping(value = "/login", method = RequestMethod.GET)

  • @PostMapping("/login") = @RequestMapping(value = "/login", method = RequestMethod.POST)

0개의 댓글