๐Ÿ’ป

์Šคํ”„๋ง ํ”„๋ ˆ์ž„์›Œํฌ - [Spring] ํƒ€์ผ์ฆˆ(Tiles) ์„ค์ •ํ•˜๊ธฐ ๋ณธ๋ฌธ

KITRI/SPRING

์Šคํ”„๋ง ํ”„๋ ˆ์ž„์›Œํฌ - [Spring] ํƒ€์ผ์ฆˆ(Tiles) ์„ค์ •ํ•˜๊ธฐ

๋˜ํšจ๋‹ˆ 2020. 8. 4. 10:59

ํƒ€์ผ์ฆˆ(Tiles)๋ž€?

 

ํƒ€์ผ์ฆˆ๋Š” ํƒ€์ผ๋“ค์„ ๋ถ™์ด๋Š” ๊ฒƒ๊ณผ ๊ฐ™์ด ๊ฐ๊ฐ์˜ ์˜์—ญ์„ ๋ถ™์—ฌ์„œ ํ•˜๋‚˜์˜ ์›นํŽ˜์ด์ง€์˜ ํ™”๋ฉด์„ ๊ตฌ์„ฑํ•˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ด๋‹ค.

์›น ํŽ˜์ด์ง€์˜ ์˜์—ญ์€ ํฌ๊ฒŒ header, content, left, nav, footer๋กœ ๋‚˜๋ˆ„์–ด์ง€๋ฉฐ, ์—ฌ๋Ÿฌ ์›น ํŽ˜์ด์ง€์—์„œ ์ด ์˜์—ญ๋“ค์„ ๊ณตํ†ต์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ๊ฐ๊ฐ์˜ ์˜์—ญ์„ ํƒ€์ผ์ฆˆ ํƒœ๊ทธ๋กœ ์ง€์ •ํ•˜์—ฌ ํŽธ๋ฆฌํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

pom.xml

tiles2๋ฅผ ์“ฐ๊ธฐ ์œ„ํ•ด์„œ pom.xml์— tiles-core, tiles-servlet, tiles-jsp ๊ฐ€์ ธ์˜ค๊ธฐ

ํ•ด๋‹น url์˜ ๋‚ด์šฉ์„ 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>com.java</groupId>
	<artifactId>homgPage</artifactId>
	<name>06_MavenSpringMVC</name>
	<packaging>war</packaging>
	<version>1.0.0-BUILD-SNAPSHOT</version>
	<properties>
		<java-version>1.6</java-version>
		<org.springframework-version>3.1.1.RELEASE</org.springframework-version>
		<org.aspectj-version>1.6.10</org.aspectj-version>
		<org.slf4j-version>1.6.6</org.slf4j-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 -->
		<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>      
		
		<!-- Library ์ถ”๊ฐ€ -->
		<!-- JSTL -->
		<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
		<dependency>
		    <groupId>javax.servlet</groupId>
		    <artifactId>jstl</artifactId>
		    <version>1.2</version>
		</dependency>
		
		<!-- STANDARD -->
		<!-- https://mvnrepository.com/artifact/taglibs/standard -->
		<dependency>
		    <groupId>taglibs</groupId>
		    <artifactId>standard</artifactId>
		    <version>1.1.2</version>
		</dependency>
		
		<!-- AOP -->
		<!-- https://mvnrepository.com/artifact/aopalliance/aopalliance -->
		<dependency>
		    <groupId>aopalliance</groupId>
		    <artifactId>aopalliance</artifactId>
		    <version>1.0</version>
		</dependency>
		
		<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
		<dependency>
		    <groupId>org.aspectj</groupId>
		    <artifactId>aspectjweaver</artifactId>
		    <version>1.8.13</version>
		</dependency>
		
		<!-- https://mvnrepository.com/artifact/cglib/cglib -->
		<dependency>
		    <groupId>cglib</groupId>
		    <artifactId>cglib</artifactId>
		    <version>2.2.2</version>
		</dependency>
		
		<!-- DB / spring jdbc, mybatis, spring mybatis, oracle(tomcat server) C:\sohyunkim\spring\apache-tomcat-9.0.35\bin -->
		<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
		<dependency>
		    <groupId>org.springframework</groupId>
		    <artifactId>spring-jdbc</artifactId>
		    <version>3.0.5.RELEASE</version>
		</dependency>
				
		<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
		<dependency>
		    <groupId>org.mybatis</groupId>
		    <artifactId>mybatis</artifactId>
		    <version>3.4.6</version>
		</dependency>
		
		<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
		<dependency>
		    <groupId>org.mybatis</groupId>
		    <artifactId>mybatis-spring</artifactId>
		    <version>1.3.2</version>
		</dependency>
		
		
		<!-- Download / -->
		<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
		<dependency>
			<groupId>commons-io</groupId>
			<artifactId>commons-io</artifactId>
			<version>1.3.2</version>
		</dependency>
		
		<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
		<dependency>
		    <groupId>commons-fileupload</groupId>
		    <artifactId>commons-fileupload</artifactId>
		    <version>1.3.3</version>
		</dependency>
		
		<!-- template -->
		<!-- https://mvnrepository.com/artifact/org.apache.tiles/tiles-jsp -->
		<dependency>
		    <groupId>org.apache.tiles</groupId>
		    <artifactId>tiles-jsp</artifactId>
		    <version>2.2.2</version>
		</dependency>
		
		<!-- https://mvnrepository.com/artifact/org.apache.tiles/tiles-servlet -->
		<dependency>
		    <groupId>org.apache.tiles</groupId>
		    <artifactId>tiles-servlet</artifactId>
		    <version>2.2.2</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/org.apache.tiles/tiles-core -->
		<dependency>
		    <groupId>org.apache.tiles</groupId>
		    <artifactId>tiles-core</artifactId>
		    <version>2.2.2</version>
		</dependency>
		
		<!-- POOL -->
		<!-- https://mvnrepository.com/artifact/commons-pool/commons-pool -->
		<dependency>
		    <groupId>commons-pool</groupId>
		    <artifactId>commons-pool</artifactId>
		    <version>1.6</version>
		</dependency>
		
		<!-- https://mvnrepository.com/artifact/commons-dbcp/commons-dbcp -->
		<dependency>
		    <groupId>commons-dbcp</groupId>
		    <artifactId>commons-dbcp</artifactId>
		    <version>1.4</version>
		</dependency>
		
		<!-- https://mvnrepository.com/artifact/commons-collections/commons-collections -->
		<dependency>
		    <groupId>commons-collections</groupId>
		    <artifactId>commons-collections</artifactId>
		    <version>3.2.1</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>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</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>

 

WEB-INF/template ํด๋” ์ƒ์„ฑํ•œ ํ›„ footer.jsp, header.jsp, layout.jsp, menu.jsp ํŒŒ์ผ์„ ์ƒ์„ฑํ•ด์ค€๋‹ค.

 

ํ…œํ”Œ๋ฆฟ์—์„œ ํด๋” ์ž‘์„ฑ template 

content๋Š” ๋ณด๋‚ด์ฃผ๋Š” ๊ฒƒ์ด๊ธฐ๋•Œ๋ฌธ์— ์ž‘์„ฑ X

 

layout.jsp๋กœ ๊ฐ€์„œ ์ž‘์„ฑํ•ด์ค€๋‹ค.

 

servlet-context.xml

๋ทฐ ๋ฆฌ์กธ๋ฒ„๋ฅผ ์„ค์ •ํ•ด์ค€๋‹ค. tiles ๊ด€๋ จ ์„ค์ •์„ ์ž‘์„ฑํ•ด์ค€๋‹ค. 

Maven Dependencies > spring-webmvc-3.1.1.RELEASE.jar ์•ˆ์˜ org.springframework.web.servlet.view.tiles2

<?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"
	xmlns:aop="http://www.springframework.org/schema/aop"
	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
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.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/" />
	<resources mapping="/css/**" location="/css/" />
	<resources mapping="/javascript/**" location="/javascript/" />
	
    <!-- ์ฃผ์„์œผ๋กœ ๋‹ซ์•„์ฃผ๊ธฐ  -->
	<!-- 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> -->

	<!-- Tiles2 ์ถ”๊ฐ€ -->
	<!-- Tiles ์„ค์ • ํŒŒ์ผ -->
	<beans:bean id ="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
		<beans:property name="definitions">
			<beans:list>
				<beans:value>/WEB-INF/tiles/tiles.xml</beans:value>
			</beans:list>
		</beans:property>
	</beans:bean>
	
	<!-- Tiles2 ๋ทฐ ๋ฆฌ์กธ๋ฒ„ -->
	<beans:bean id ="tilesViewResolver" class="org.springframework.web.servlet.view.tiles2.TilesViewResolver">
		<beans:property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView"/>
	</beans:bean>
	
	<!-- DI -->
	<context:component-scan base-package="com.java..*" />
	
	<!-- AOP -->
	<aop:aspectj-autoproxy/>
	
	<!-- FileUpload -->
	<beans:bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver" id="multipartResolver">
	<beans:property name="maxUploadSize" value="502400"/>
	<!-- 102400 -->
	<beans:property name="defaultEncoding" value="utf-8"/>
	</beans:bean>
	
</beans:beans>

 

WEB-INF/tiles ํด๋”๋ฅผ ์ƒ์„ฑํ•ด์ค€๋‹ค. 

tiles.xmlํŒŒ์ผ์„ ์ƒ์„ฑํ•œ๋‹ค. 

 

https://tiles.apache.org/framework/tiles-core/dtddoc/tiles-config_3_0.dtd.html

 

DTD for the Tiles Definition File, Version 3.0

Project Tiles has retired. For details please refer to its Attic page. DTD for the Tiles Definition File, Version 3.0 tiles-config_3_0.dtd: Elements - Entities - Source | Intro - Index FRAMES / NO FRAMES DTD for the Tiles Definition File, Version 3.0 Usa

tiles.apache.org

์œ„์˜ ๋งํฌ์—์„œ ๋ณต์‚ฌํ•œ Doctype์„ ์‚ฝ์ž…

<!DOCTYPE tiles-definitions PUBLIC "-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN" "http://tiles.apache.org/dtds/tiles-config_3_0.dtd">

 

tiles.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tiles-definitions PUBLIC "-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN" "http://tiles.apache.org/dtds/tiles-config_3_0.dtd">

<tiles-definitions>
   <definition name="layout" template="/WEB-INF/template/layout.jsp">
      <put-attribute name="header" value="/WEB-INF/template/header.jsp"/>
      <put-attribute name="menu" value="/WEB-INF/template/menu.jsp"/>
      <put-attribute name="content" value=""/>
      <put-attribute name="foot" value="/WEB-INF/template/footer.jsp"/>
   </definition>
   
   <!-- content -->
   <definition name="*.tiles" extends="layout">
   		<put-attribute name="content" value="/WEB-INF/views/{1}.jsp"></put-attribute>
   </definition>

   <definition name="*/*.tiles" extends="layout">
   		<put-attribute name="content" value="/WEB-INF/views/{1}/{2}.jsp"></put-attribute>
   </definition>

   <definition name="*/*/*.tiles" extends="layout">
   		<put-attribute name="content" value="/WEB-INF/views/{1}/{2}/{3}.jsp"></put-attribute>
   </definition>
   
   <!-- tiles ์ œ์™ธ ์‹œํ‚จ๋‹ค -->
   <definition name="empty" template="/WEB-INF/template/empty.jsp">
   		<put-attribute name="content" value=""/>
   </definition>

   <definition name="*/*.empty" extends="empty">
   		<put-attribute name="content" value="/WEB-INF/views/{1}/{2}.jsp"/>
   </definition>
</tiles-definitions>

์ด๋ ‡๊ฒŒ ์„ค์ •ํ•˜๊ณ  ๋‚œ ํ›„ content๋Š” ๋น„์›Œ๋‘๊ณ  layout.jsp๋กœ ๊ฐ€์„œ ์ž‘์„ฑํ•ด์ค€๋‹ค. 

 

menu.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<c:set var="root" value="${pageContext.request.contextPath}"/>
	<c:set var = "id" value="${id}" scope="session"/>
	
	<a href="${root}/member/test.do">Spring MVC Start!!!</a>
	
	<c:if test="${memberLevel==null}">
		<h3>ํšŒ์›๊ด€๋ฆฌ</h3>
			<a href="${root}/member/register.do">ํšŒ์›๊ฐ€์ž…</a>
		<h3>๋กœ๊ทธ์ธ</h3>
			<a href="${root}/member/login.do">๋กœ๊ทธ์ธ</a>
	</c:if>
	
	<c:if test="${memberLevel!=null}">
    	<a href="${root}/member/delete.do">ํšŒ์›ํƒˆํ‡ด</a>
    	<a href="${root}/member/update.do">ํšŒ์›์ˆ˜์ •</a>
  		<a href="${root}/member/logout.do">๋กœ๊ทธ์•„์›ƒ</a>	
  		
  		<c:if test="${memberLevel=='MA'}">
  			<h3>๊ด€๋ฆฌ์ž ํŽ˜์ด์ง€</h3>
  			<a href="">ํšŒ์›๊ด€๋ฆฌ</a>
  		</c:if>
   </c:if>
   
   <br/><br/>
   
   <h3>ํŒŒ์ผ ๊ฒŒ์‹œํŒ</h3>
   <a href="${root}/fileBoard/write.do">ํŒŒ์ผ๊ฒŒ์‹œํŒ ์“ฐ๊ธฐ</a>
  	<br/><br/>
  	
  	<a href="${root}/fileBoard/list.do">ํŒŒ์ผ ๋ฆฌ์ŠคํŠธ</a> 
</body>
</html>

 

footer.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h3 style="color:red">FOOT</h3>
</body>
</html>

 

header.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h3 style="color:blue">header</h3>
</body>
</html>

 

layout.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<!-- header -->
	<tiles:insertAttribute name="header"/>
	<hr color="red"/>
	<!-- menu -->
	<tiles:insertAttribute name="menu"/>
	<hr color="pink"/>
	
	<!-- ๊ณ„์† ๋ฐ”๋€Œ๋Š” ๋‚ด์šฉ๋ถ€ -->
	<tiles:insertAttribute name="content"/>
	<hr color="blue"/>
	
	<!-- foot -->
	<tiles:insertAttribute name="foot"/>
</body>
</html>

 

Controller ํŒŒ์ผ๋กœ ๊ฐ€์„œ ํ™•์žฅ์ž๋ฅผ .tiles ๋กœ ๋ฐ”๊ฟ”์ฃผ์–ด์•ผ์ง€ ํ…œํ”Œ๋ฆฟ์ด ์ ์šฉ๋œ๋‹ค. ์ฒ˜์Œ๋ถ€ํ„ฐ ํ…œํ”Œ๋ฆฟ์„ ๊ฑธ๊ณ  ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

 

[com.java.homePage] HomeController.java

package com.java.homePage;

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {
	
	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
	
	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String home(Locale locale, Model model) {
		logger.info("Welcome home! The client locale is {}.", locale);
		
		Date date = new Date();
		DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
		
		String formattedDate = dateFormat.format(date);
		
		model.addAttribute("serverTime", formattedDate );
		
		return "home.tiles";
	}
	
}

 

[com.java.member.controller] MemberController.java

package com.java.member.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springframework.web.servlet.view.InternalResourceView;

import com.java.aop.HAspect;
import com.java.member.dto.MemberDto;
import com.java.member.service.MemberService;

@Controller
public class MemberController{	//Command
	@Autowired
	private MemberService memberService;
	
	@RequestMapping(value="/member/test.do", method=RequestMethod.GET)
	public ModelAndView testing(HttpServletRequest request, HttpServletResponse response) {
		System.out.println("OK");
		
		//Service - Dao / Dto - Service
		//์ตœ์ƒ์œ„ ํด๋ž˜์Šค๋Š” View
		/*
		InternalResourceView view = new InternalResourceView("/WEB-INF/member/test.jsp");
		ModelAndView mav = new ModelAndView();
		mav.addObject("msg", "hi");
		mav.setView(view);
		*/
		
		//๋‹ค์–‘ํ•œ ์ƒ์„ฑ์ž๊ฐ€ ์žˆ๊ธฐ์— ๊ทธ์— ๋งž์ถฐ์„œ ์‚ฌ์šฉ!!!
		//ModelAndView mav = new ModelAndView(view);
		ModelAndView mav = new ModelAndView("member/test.tiles");
		mav.addObject("msg", "hi");
		
		return mav;
	}
	
	@RequestMapping(value="/member/register.do", method=RequestMethod.GET)
	public ModelAndView memberRegister(HttpServletRequest request, HttpServletResponse response) {
		return new ModelAndView("member/register.tiles");
	}
	
	@RequestMapping(value="/member/registerOk.do", method=RequestMethod.POST)
	public ModelAndView memberRegisterOk(HttpServletRequest request, HttpServletResponse response, MemberDto memberDto) {
		HAspect.logger.info(HAspect.logMsg + memberDto.toString());
		
		ModelAndView mav = new ModelAndView();
		mav.addObject("memberDto", memberDto);
		
		memberService.memberRegisterOk(mav);
		
		return mav;
	}
	
	@RequestMapping(value="/member/idCheck.do", method=RequestMethod.GET)
	public ModelAndView memberidCheck(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("request", request);
		
		memberService.memberidCheck(mav);
		
		return mav;
	}
	
	@RequestMapping(value="/member/zipcode.do", method=RequestMethod.GET)
	public ModelAndView memberZipcode(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("request", request);
		
		memberService.memberZipcode(mav);
		
		return mav;
	}
	
	@RequestMapping(value="/member/login.do", method=RequestMethod.GET)
	public ModelAndView memberLogin(HttpServletRequest request, HttpServletResponse response) {
		return new ModelAndView("member/login.tiles");
	}
	
	@RequestMapping(value="/member/loginOk.do", method=RequestMethod.POST)
	public ModelAndView memberLoginOk(HttpServletRequest request, HttpServletResponse response) {
		
		ModelAndView mav = new ModelAndView();
		mav.addObject("request", request);
		memberService.memberloginOk(mav);
		
		return mav;
	}
	
	@RequestMapping(value="/member/main.do", method=RequestMethod.GET)
	public ModelAndView main(HttpServletRequest request, HttpServletResponse response) {
		
		ModelAndView mav = new ModelAndView();
		mav.addObject("request", request);
		memberService.main(mav);
		
		return mav;
	}
	
	@RequestMapping(value="/member/logout.do", method=RequestMethod.GET)
	public ModelAndView memberLogout(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("request", request);
		memberService.memberloginOut(mav);
		return mav;
	}
	
	@RequestMapping(value="/member/update.do", method=RequestMethod.GET)
	public ModelAndView memberUpdate(HttpServletRequest request, HttpServletResponse response) {
		//select ์ถ”๊ฐ€
		
		ModelAndView mav = new ModelAndView();
		mav.addObject("request", request);
		memberService.memberUpdate(mav);
		
		return mav;
	}
	
	@RequestMapping(value="/member/updateOk.do", method=RequestMethod.POST)
	public ModelAndView memberUpdateOk(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("request", request);
		//memberService.memberUpdate(mav);
		return mav;
	}	
}

 

[com.java.member.service] MemberServiceImp.java

package com.java.member.service;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;

import com.java.aop.HAspect;
import com.java.member.dao.MemberDao;
import com.java.member.dto.MemberDto;
import com.java.member.dto.ZipcodeDto;

//๊ตฌํ˜„ ํด๋ž˜์Šค

@Component
public class MemberServiceImp implements MemberService {
	@Autowired
	private MemberDao memberDao;

	@Override
	public void memberRegisterOk(ModelAndView mav) {
		Map<String, Object> map = mav.getModelMap();
		MemberDto memberDto = (MemberDto)map.get("memberDto");
		
		memberDto.setMemberLevel("BA");
		
		int check = memberDao.memberinsert(memberDto);
		HAspect.logger.info(HAspect.logMsg + check);
		
		mav.addObject("check", check);
		mav.setViewName("member/registerOk.tiles");
		
	}
	
	@Override
	public void memberidCheck(ModelAndView mav) {
		Map<String, Object> map = mav.getModelMap();
		HttpServletRequest request = (HttpServletRequest)map.get("request");
		
		String id = request.getParameter("id");
		HAspect.logger.info(HAspect.logMsg + id);
		
		int check = memberDao.memberidCheck(id);
		HAspect.logger.info(HAspect.logMsg + check);
	
		mav.addObject("check", check);
		mav.addObject("id", id);
		mav.setViewName("member/idCheck.empty");
	}
	
	@Override
	public void memberZipcode(ModelAndView mav) {
		Map<String, Object> map = mav.getModelMap();
		HttpServletRequest request = (HttpServletRequest) map.get("request");
		
		String dong = request.getParameter("dong");
		
		if(dong!=null) {
			List <ZipcodeDto> zipcodeDto = memberDao.zipcode(dong);
			HAspect.logger.info(HAspect.logMsg + zipcodeDto.size());
			
			mav.addObject("zipcodeList", zipcodeDto);
		}
		mav.setViewName("member/zipcode.empty");
	}
	
	@Override
	public void memberlogin(ModelAndView mav) {
		// TODO Auto-generated method stub
		
	}
	
	
	@Override
	public void memberloginOk(ModelAndView mav) {
		Map<String, Object> map = mav.getModelMap();
		HttpServletRequest request = (HttpServletRequest)map.get("request");
		
		String id = request.getParameter("id");
		String password=request.getParameter("password");
		int check = memberDao.loginOk(id, password);
		HAspect.logger.info(HAspect.logMsg + check);
		
		mav.addObject("id", id);
		mav.addObject("password", password);
		mav.addObject("memberLevel", "BB");
		mav.setViewName("member/loginOk.tiles");
	}
	
	@Override
	public void main(ModelAndView mav) {
		mav.setViewName("member/main.tiles");
	}
	
	@Override
	public void memberloginOut(ModelAndView mav) {
		Map<String, Object> map = mav.getModelMap();
		HttpServletRequest request = (HttpServletRequest)map.get("request");
		
		String id = request.getParameter("id");
		String password=request.getParameter("password");
		int check = memberDao.loginOk(id, password);
		HAspect.logger.info(HAspect.logMsg + check);
		
		mav.addObject("id", id);
		mav.addObject("password", password);
		mav.addObject("memberLevel", "BB");
		mav.setViewName("member/logout.tiles");
		
	}
	@Override
	public void memberUpdate(ModelAndView mav) {
		Map<String, Object> map = mav.getModelMap();
		HttpServletRequest request = (HttpServletRequest)map.get("request");
		
		String id = request.getParameter("id");
		String password=request.getParameter("password");
		//int check = memberDao.loginOk(id, password);
		HAspect.logger.info(HAspect.logMsg + id + "@@" + password);
		
		mav.addObject("id", id);
		mav.setViewName("member/update.tiles");
		
	}
	
	@Override
	public void memberUpdateOk(ModelAndView mav) {
		Map<String, Object> map = mav.getModelMap();
		HttpServletRequest request = (HttpServletRequest)map.get("request");
		
		String id = request.getParameter("id");
		String password=request.getParameter("password");
		int check = memberDao.loginOk(id, password);
		HAspect.logger.info(HAspect.logMsg + check);
		
		mav.addObject("id", id);
		mav.addObject("password", password);
		mav.addObject("memberLevel", "BB");
		mav.setViewName("member/updateOk.tiles");
		
	}	
}

 

 

idCheck.jsp, zipcode.jsp ์˜ ๊ฒฝ์šฐ์—๋Š” template์ด ์ ์šฉ๋˜๋ฉด ์•ˆ๋˜๊ธฐ๋•Œ๋ฌธ์—

tempateํด๋”์— empty.jspํŒŒ์ผ์„ ์ƒ์„ฑํ•ด์ค€๋‹ค.

 

empty.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<tiles:insertAttribute name="content"/>
</body>
</html>

 

tiles.xml

์—์„œ .empty ํŒŒ์ผ์— ๋Œ€ํ•œ ์„ค์ •์„ ์ถ”๊ฐ€๋กœ ํ•ด์ค€๋‹ค.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tiles-definitions PUBLIC "-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN" "http://tiles.apache.org/dtds/tiles-config_3_0.dtd">

<tiles-definitions>
   <definition name="layout" template="/WEB-INF/template/layout.jsp">
      <put-attribute name="header" value="/WEB-INF/template/header.jsp"/>
      <put-attribute name="menu" value="/WEB-INF/template/menu.jsp"/>
      <put-attribute name="content" value=""/>
      <put-attribute name="foot" value="/WEB-INF/template/footer.jsp"/>
   </definition>
   
   <!-- content -->
   <definition name="*.tiles" extends="layout">
   		<put-attribute name="content" value="/WEB-INF/views/{1}.jsp"></put-attribute>
   </definition>

   <definition name="*/*.tiles" extends="layout">
   		<put-attribute name="content" value="/WEB-INF/views/{1}/{2}.jsp"></put-attribute>
   </definition>

   <definition name="*/*/*.tiles" extends="layout">
   		<put-attribute name="content" value="/WEB-INF/views/{1}/{2}/{3}.jsp"></put-attribute>
   </definition>
   
   <!-- tiles ์ œ์™ธ ์‹œํ‚จ๋‹ค -->
   <definition name="empty" template="/WEB-INF/template/empty.jsp">
   		<put-attribute name="content" value=""/>
   </definition>

   <definition name="*/*.empty" extends="empty">
   		<put-attribute name="content" value="/WEB-INF/views/{1}/{2}.jsp"/>
   </definition>
</tiles-definitions>
๋ฐ˜์‘ํ˜•
Comments