본문 바로가기

PROJECT/게시판

[ 게시판 v.1 ] xml 설정

728x90
반응형

어찌 어찌 톰캣 다운 받고 다 준비 했는데 일단 xml 설정이 가장 고된거 같다.

 

그래도 처음이 힘들지 한번 길을 잘 닦아 놓으면 그 다음은 고속도로 인거 같다.

설정으로 빡치는 사람 전부 다 힘내기.. :(

 

아래 코드들은 내가 만든 게시판 설정 그대로 이다.


web.xml

  • web.xml 파일은 웹 어플리케이션의 배포 설명자로, 각 어플리케이션의 환경을 설정하는 역할을 한다
  • 서버가 처음 로딩될 때 읽어들이고, 해당 환경설정에 대해 tomcat에 적용하여 서버를 시작한다.
  • dispatcherServlet 설정, db설정과 같은 서브릿 설정에 대한 내용,
  • listener, filter 설정 및 welcome file list, error page 처리,
  • mome type매핑, session 의 유효시간 설정, servlet context의 초기 파라메터 설정 등이 있다.
  • web.xml파일은 맨 처음 <web-app> 태그로 시작되고,
  • xmlns 네임스페이스로 xml schema for java EE Deployment Descriptors 파일이 존재하는 위치를 선언해주고 있다.
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

	<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/spring/root-context.xml</param-value>
	</context-param>
	
	<!-- Creates the Spring Container shared by all Servlets and Filters -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!-- Processes application requests -->
	<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>

	<!-- encoding filter -->
	<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>

 


servlet-context.xml

  • servlet에서 보듯이 요청과 관련된 객체를 정의합니다.
<?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="com.myp.domain" />
</beans:beans>

root-context.xml

  • servlet-context.xml 과는 반대로 view와 관련되지 않은 객체를 정의합니다
  • 따라서 Service, Repository(DAO), DB등 비즈니스 로직과 관련된 설정을 해줍니다.
  • JSP와 관련없는 객체(bean)을 설정합니다.(service, repository)
  • 비지니스 로직을 위한 설정을 합니다.
  • 외부 jar 파일등으로 사용하는 클래스는 <bean>태그를 이용하여 작성합니다.
  • 공통 빈을 설정합니다.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
		http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
		http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
	
	<!-- Root Context: defines shared resources visible to all other web components -->

	<!-- 1. dataSource 등록(db 접속 정보) -->
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <!--JDBC 라이브러리 위치 설정-->
	        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
			<property name="url" value="jdbc:mysql://61.110.19.87:3306/TEST?characterEncoding=UTF-8&amp;serverTimezone=UTC" />
			<property name="username" value="test" />
			<property name="password" value="test!@#" />
	</bean>

      
	<!-- 2. DAO 인터페이스 사용을 위한 객체 설정-->
	<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate" destroy-method="clearCache">
			<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
	</bean>
	

	<!-- 3. sqlsessionfactory 커넥션 추가(Mybatis 사용) -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
			<property name="dataSource" ref="dataSource" /> <!--datasource 위치 참조-->
			<property name="configLocation" value="classpath:/mybatis-config.xml"></property>
			<property name="mapperLocations" value="classpath:mappers/**/*Mapper.xml"></property>
	</bean>

</beans>

 

mybatis-config.xml

  • 이 설정은 아무것도 건들지 않았다.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
	<mappers>
		<!-- <mapper resource="/resources/mappers/boardMapper.xml"/> -->
	</mappers>
</configuration>

 

728x90
반응형

'PROJECT > 게시판' 카테고리의 다른 글

[ 게시판 v.2 ] 전체적인 코드  (0) 2021.01.06
Spring 게시판 만들기  (0) 2021.01.06