Application Context
XML, Annotation
Bean
<기본적인 home.jsp 를 이용해 날짜 출력>
: Servlet/JSP와 비교하여 차이점 위주로 이해하기
Spring Legacy Project -> Spring MVC Project 만들기
왼쪽 아래 New server -> Apache Tomcat v9.0 server ->
c:\tools 밑 폴더 선택
서버 - 톰캣 9.0
project 내 src -> webapp -> WEB-INF -> web.xml
인코딩 설정 추가
<!-- 추가 -->
<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>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
home.jsp
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" language="java" %>
http://localhost:8080/springweb/
=> 따로 관리하기 위해서
servlet-context.xml에 use-default-filters="false"
추가
<context:component-scan base-package="my.spring.springweb" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
=>
<?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 />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<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="my.spring.springweb" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
</beans:beans>
root-context.xml
스키마 추가 xmlns:context="http://www.springframework.org/schema/context"
<?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">
<!-- Root Context: defines shared resources visible to all other web components -->
<context:component-scan base-package="my.spring.springweb">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
</beans:beans>
① 계층형
=> 구조가 심플하지만 관리가 힘들다..
src/main/java
② Domain 타입
src/main/java
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>my.spring</groupId>
<artifactId>springweb</artifactId>
<name>springweb</name>
<packaging>war</packaging>
<version>1.0.0-BUILD-SNAPSHOT</version>
<!-- 수정한 부분 -->
<properties>
<java-version>1.8</java-version>
<org.springframework-version>5.3.23</org.springframework-version>
<org.aspectj-version>1.9.9.1</org.aspectj-version>
<org.slf4j-version>1.7.36</org.slf4j-version>
<log4j2.version>2.17.1</log4j2.version>
</properties>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework-version}</version>
<exclusions>
<!-- Exclude Commons Logging in favor of SLF4j -->
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- AspectJ -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${org.aspectj-version}</version>
</dependency>
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${org.slf4j-version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${org.slf4j-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${org.slf4j-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.15</version>
<exclusions>
<exclusion>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
</exclusion>
<exclusion>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
</exclusions>
<scope>runtime</scope>
</dependency>
<!-- @Inject -->
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<!-- Servlet -->
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</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>2.3.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- 여기도 주석
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
-->
<!-- Test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</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>2.3.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<additionalProjectnatures>
<projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
</additionalProjectnatures>
<additionalBuildcommands>
<buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
</additionalBuildcommands>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</configuration>
</plugin>
<!-- jdk 버전 수정 !! -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>${java-version}</source>
<target>${java-version}</target>
<compilerArgument>-Xlint:all</compilerArgument>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>org.test.int1.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
완료 => 서버 기동
servlet-context.xml
=> view resolver 설정 : Handler로부터 만약 String이 리턴되면
request하고, dispatcher Servlet이 controller method()에게 보냄
View resolver : ModelAndView 객체를 View 영역으로 전달하기 위해 알맞은 View 정보를 설정하는 역할
http://localhost:8080/springweb/resources/test.html
: 이 class type의 객체가 handler의 parameter로 주입
servlet을 동작시킬 때
-> HandlerMapping interface
: 구현체가 4개정도 제공 -> 1개만 기억하면 됨
: RequestMappingHandlerMapping
: @RequestMappingHandlerMapping
annotation을 이용
: class가 아닌 method를 handler로 지정
HandlerAdaptor interface로 되어있음
구현체로 RequestMappingHandlerAdapter
class를 많이 사용함
: View interface를 구현한 객체를 지칭
: Model이 가진 정보를 어떻게 표현해야 하는지에 대해 로직을 가지고 있는 Component
: 구현한 구현class가 여러개 있음
① InternalResource
: JSP
controller가 이 객체를 직접 만들 수 있음 - 그럼 view resolver를 거치지 않음View view = new InternalResourceView("hello.jsp"); return new ModelAndView(view, model);
Servlet ->response.sendRedirect();
controller => new ModelAndView(view, model)
MarshallingView -> XML
MappingJacksonJsonViewClass -> JSON
예제1
1. log4j
2. @Controller
3. @RequestMapping
4. InternalresourceView class
5. Model, ModelAndView class
console에 system.out.println()을 이용해 출력하는 건 여러가지 측면에서 좋지 않음
1) 성능면에서 좋지 않은
2) 관리적 측면에서도 좋지 않음
3) 운영시 로그 생성은 필수
spring에서는 log4j를 쉽게 이용할 수 있음 - 확장버전 log4j2 : 2.x.x 버전 / logback(spring boot)
먼저, log4j부터 살펴보고(설정파일이 있음-log4j.xml), log4j2로 변경
① log의 출력 위치를 지정 : <appender></appender>
(console출력, File출력, DB출력)
② log의 출력 형태를 지정 :<layout></layout>
③ logger : 메시지를 appender에 전달하는 역할
: log의 출력level에 따라서 출력여부를 결정(6단계)
: 여러 종류가 있음
① Console Appender(console)
② File Appender(File)
③ RollingFileAppender(용량제한File)
④ DailyRollingAppender(날짜별File)
%p : log level을 같이 출력
%m : 로그메세지 출력
%d : 로그 발생시간 출력(yyyy-mm-dd HH:mm:ss)
%c : 어떤 파일에서 발생했는지 출력
log level 6단계
FATAL : 가장 높은 단계, system에 심각한 문제가 있음
ERROR : 실행 중 Error가 발생
WARN : 오류의 원인이 될 수 있는 메세지 클릭 -warn()
INFO : application운영 - 운영 level -info()
DeBUG : 개발시 디버깅 용도 - sysout 대신 사용 -debug()
TRACE : DEBUG의 상세 버전