관리 메뉴

공부한것들을 정리하는 블로그 입니다.

7. MVC Model1 아키텍처로 게시판 개발 : web.xml, representation-layer.xml, index.jsp 본문

(2019) 사이드 프로젝트/BoardWeb(게시판-MVC1,MVC2,스프링MVC)

7. MVC Model1 아키텍처로 게시판 개발 : web.xml, representation-layer.xml, index.jsp

호 두 2019. 6. 20. 14:14
반응형

 

MV패턴에 대해서는 생략하고 바로 진행하도록 하겠습니다. (MV패턴은 구글링하면 좋은 자료가 많습니다)

 

기능(서비스로직 구현)
- 로그인 login.jsp, login_proc.jsp
- 로그아웃 logout_proc.jsp
- 글 목록 조회 getBoardList.jsp
- 글 상세 조회 getBoard.jsp
- 글 입력 insert.jsp, insert_proc.jsp
- 글 수정 update.jsp, update_proc.jsp
- 글 삭제 delete_proc.jsp


흐름
(request)

login.jsp            view
↓ 로그인
login_proc.jsp       controller

getBoardList.jsp     v+c

getBoard.jsp         v+c

insert.jsp              v

insert_proc.jsp       v+c
 
getBoardList.jsp     v+c 

 

update.jsp            v

update_proc.jsp     v+c
 
getBoardList.jsp     v+c 

 

delete_proc.jspc     c

getBoardList.jsp     v+c

logout_proc.jsp      c 기능(서비스로직 구현)

 

 

 

1. web.xml 설정

- DispatcherServlet 으로 presentation-layer.xml 설정파일을 참조하도록 하였으며, *.do로 실행되는 요청을 MVC모델의 Controller로써 applicationContext.xml 설정파일을 참조하도록 하였습니다.

- 인코딩을 위한 filter 엘리먼트를 추가하였습니다. 보통은 보안(인증) 관련된 부분도 감싸서 처리할 수 있도록 추가해야 하지만 잠시 미루고 우선 게시판의 뼈대를 구축하는것을 우선하도록 하겠습니다.

 

<?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">
	
	<filter>
		<filter-name>characterEncoding</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>characterEncoding</filter-name>
		<url-pattern>*.do</url-pattern>
	</filter-mapping>
	
	<servlet>
		<servlet-name>action</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/config/presentation-layer.xml</param-value>
		</init-param>
	</servlet>
	
	<servlet-mapping>
		<servlet-name>action</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>
	
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>
	
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

</web-app>

 

 

2. applicationContext.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"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="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-4.2.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
		
		
	<context:component-scan base-package="com.springbook.biz"></context:component-scan>
	
	<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
	
	<!-- DataSource setting -->
	<context:property-placeholder location="classpath:config/database.properties" />
	
	<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	</bean>
	
	<!-- Spring JDBC -->
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource" />
	</bean>
	
	<!--  Transaction setting -->
	<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>
	<!-- <bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean> -->
	
	<!-- Transaction advice setting -->
	<tx:advice id="txAdvice" transaction-manager="txManager">
		<tx:attributes>
			<tx:method name="get*" read-only="true" />
			<tx:method name="*" />
		</tx:attributes>
	</tx:advice>
	
	<!-- AOP 설정을 통한 트랜잭션 적용 -->
	<aop:config>
		<aop:pointcut id="txPointcut" expression="execution(* com.springbook.biz..*(..))" />
		<aop:advisor pointcut-ref="txPointcut" advice-ref="txAdvice" />
	</aop:config>
	
</beans>

 

 

3. presentation-layer.xml 설정

- 하단의 다국어 설정을 을 선언하였습니다.

- 상단의 package 경로 설정, mvc annotation-driven을 선언해 주는 부분은 주석으로 처리하였습니다. 이 부분은 추후 실습에서 BoardController.java를 작성할 때 구현할 예정입니다.

- viewResolver도 마찬가지로 주석처리 하였습니다. 지금은 넘어가도 좋습니다.

 

<?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:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.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-4.2.xsd">

	<!-- <context:component-scan base-package="com.springbook.view" /> -->
	
	<!-- <mvc:annotation-driven></mvc:annotation-driven> -->

	<!-- viewResolver -->
	<!-- <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/" />
		<property name="suffix" value=".jsp" />
	</bean> -->


	<!--  파일 업로드 설정 -->
<!-- 	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<property name="maxUploadSize" value="100000" />
	</bean> -->
	
	<!--  예외 처리 설정 -->
<!-- 	<bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
		<property name="exceptionMappings">
				<props>
					<prop key="java.lang.ArithmeticException">
						common/arithmeticError.jsp
					</prop>
					<prop key="java.lang.NullPointerException">
						common/nullPointerError.jsp
					</prop>
				</props>
		</property>
		<property name="defaultErrorView" value="common/error.jsp" />
	</bean> -->
	
	<!--  다국어 설정 -->
	<!-- MessageSource 등록 -->
	<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
		<property name="basenames">
			<list>
				<value>message.messageSource</value>
			</list>
		</property>
	</bean>
	
	<!-- LocaleResolver 등록 -->
	<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"></bean>
	
	<!--  LocaleChangeIntercepter 등록 -->
	<mvc:interceptors>
		<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
			<property name="paramName" value="lang" />
		</bean>
	</mvc:interceptors>
</beans>

 

 

4. index.jsp

- index 페이지는 추후 변경 될 소지가 많으므로 우선은 대충 작성해놓습니다.

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Main Page</title>
</head>
<body>
	<center>
		<h1>게시판 프로그램</h1>
		<hr>
			<a href="login.jsp">로그인</a><br><br><br>
			<a href="getBoardList.do">글 목록 바로가기</a><br><br><br>
			<!-- <a href="dataTransform.do">글 목록 변환 처리</a> -->
		<hr>
	</center>
</body>
</html>

 

 

 

 

반응형
Comments