- 레이아웃 템플릿 엔진으로 레이아웃을 구성하는데 좀 더 세분화되고 관리하기 쉬워 유지보수에 용이
pom.xml
<!-- Tiles -->
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-extras</artifactId>
<version>3.0.8</version>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-servlet</artifactId>
<version>3.0.8</version>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-jsp</artifactId>
<version>3.0.8</version>
</dependency>
Maven build 실행
- 해당 프로젝트 우클릭 > Run As 클릭 > Maven build 클릭
Goals:compile 작성 > Apply 클릭 > Run 클릭
Run Configurations 다른 방법으로 여는 방법
더보기
해당 프로젝트 우클릭 > Run As > Run Configurations ... 클릭
Goals:compile 작성 > Apply 클릭 > Run 클릭
- Console에 BUILD SUCCESS 출력하면 성공
servlet-context.xml
setOrder("2") => tiles를 작동한 후에 ViewResolver를 작동하겠다라는 의미
사용자에게 응답 결과를 보여 주려고 컨트롤러가 모델을 반환하고 디스패처 서블릿이 JSP 파일을 찾을 수 있음
컨트롤러에서 설정한 뷰 이름으로 실제 사용할 뷰를 선택하는 뷰 리졸버 객체를 생성
컨트롤러가 설정한 뷰 이름 앞뒤로 prefix 프로퍼티와 suffix 프로퍼티를 추가한 값이 실제로 사용될 뷰의 경로가 됨.
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
<beans:property name="order" value="2"/>
</beans:bean>
<!-- Tiles 설정 시작 -->
<beans:bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
<beans:property name="definitions">
<beans:list>
<beans:value>/WEB-INF/spring/tiles-config.xml</beans:value>
</beans:list>
</beans:property>
</beans:bean>
<!-- UrlBasedViewResolver tilesViewResolver = new UrlBasedViewResolver(); -->
<beans:bean id="tilesViewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<beans:property name="viewClass"
value="org.springframework.web.servlet.view.tiles3.TilesView" />
<beans:property name="order" value="1" />
</beans:bean>
<!-- Tiles 설정 끝 -->
tiles-config.xml
xml 파일 위치
spring 폴더 우클릭 > New > Other... >MyBatis XML Mapper 클릭 > Next 클릭>
tiles-config.xml 작성> Finish
definition name ⇒ */* 또는 * 인 경우
<?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 설정 -->
<tiles-definitions>
<!-- 메인 layout -->
<definition name="main-layout" template="/WEB-INF/views/tiles/mainTemplate.jsp">
<put-attribute name="header" value="/WEB-INF/views/tiles/mainHeader.jsp"/>
<put-attribute name="aside" value="/WEB-INF/views/tiles/aside.jsp"/>
<put-attribute name="body" value=""/>
<put-attribute name="footer" value="/WEB-INF/views/tiles/footer.jsp"/>
</definition>
<!--
mav.setViewName("lprod/create");
/WEB-INF/views/lprod/create.jsp
-->
<definition name="*/*" extends="main-layout">
<put-attribute name="body" value="/WEB-INF/views/{1}/{2}.jsp"></put-attribute>
</definition>
<!-- 메인 layout -->
<definition name="sub-layout" template="/WEB-INF/views/tiles/mainTemplate.jsp">
<put-attribute name="header" value="/WEB-INF/views/tiles/mainHeader.jsp"/>
<put-attribute name="aside" value="/WEB-INF/views/tiles/aside.jsp"/>
<put-attribute name="body" value=""/>
<put-attribute name="footer" value="/WEB-INF/views/tiles/footer.jsp"/>
</definition>
<!--
mav.setViewName("formHome");
/WEB-INF/views/formHome.jsp
-->
<definition name="*" extends="sub-layout">
<put-attribute name="body" value="/WEB-INF/views/{1}.jsp"></put-attribute>
</definition>
</tiles-definitions>
definition name="*/*" 예시
controller 에서 mav.setViewName("lprod/create"); 작성시
/WEB-INF/views/lprod/create.jsp 로 컴파일 됨
더보기
<?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 설정 -->
<tiles-definitions>
<!-- 메인 layout -->
<definition name="main-layout" template="/WEB-INF/views/tiles/mainTemplate.jsp">
<put-attribute name="header" value="/WEB-INF/views/tiles/mainHeader.jsp"/>
<put-attribute name="aside" value="/WEB-INF/views/tiles/aside.jsp"/>
<put-attribute name="body" value=""/>
<put-attribute name="footer" value="/WEB-INF/views/tiles/footer.jsp"/>
</definition>
<definition name="*/*" extends="main-layout">
<put-attribute name="body" value="/WEB-INF/views/{1}/{2}.jsp"></put-attribute>
</definition>
</tiles-definitions>
definition name="*" 예시
controller 에서 mav.setViewName("books"); 작성시
/WEB-INF/views/books.jsp 로 컴파일 됨
더보기
<?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 설정 -->
<tiles-definitions>
<!-- 메인 layout -->
<definition name="main-layout" template="/WEB-INF/views/tiles/mainTemplate.jsp">
<put-attribute name="header" value="/WEB-INF/views/tiles/mainHeader.jsp"/>
<put-attribute name="aside" value="/WEB-INF/views/tiles/aside.jsp"/>
<put-attribute name="body" value=""/>
<put-attribute name="footer" value="/WEB-INF/views/tiles/footer.jsp"/>
</definition>
<definition name="*" extends="main-layout">
<put-attribute name="body" value="/WEB-INF/views/{1}.jsp"></put-attribute>
</definition>
</tiles-definitions>
definition 경로 폴더 + jsp 생성하기
body 확장하기
jsp 사용
jsp에서 tiles 사용할려면 JSTL 필요
<%@ taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles"%>
mainTemplate에 있는 <tiles:insertAttribute name="" />으로 각 jsp를 불러오는 방법이다.
tiles-config 에서 선언한 name값들을 가져와야 한다.
<tiles:insertAttribute name="put-attribute_name"/>
원본
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>kr.or</groupId>
<artifactId>ddit</artifactId>
<name>springProj</name>
<packaging>war</packaging>
<version>1.0.0-BUILD-SNAPSHOT</version>
<properties>
<java-version>1.8</java-version>
<org.springframework-version>5.2.5.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>
<!-- Database 라이브러리 시작 -->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<!-- XML로 쿼리를 작성하게 해주는 라이브러리 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<!-- 스프링과 mybatis를 연동하게 해주는 라이브러리 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<!-- 스프링에서 JDBC(Java DataBase Connectivitiy) -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-dbcp2 -->
<!-- dbcp : database connection pool => 커넥션객체를 미리 만들어놓고 쓰고/반납
최근에는 hikaricp를 사용하는 경우도 있음
-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.7.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.bgee.log4jdbc-log4j2/log4jdbc-log4j2-jdbc4 -->
<!-- 로깅을 위한 라이브러리. 쿼리를 console이나 파일 로그로 볼 수 있음 -->
<dependency>
<groupId>org.bgee.log4jdbc-log4j2</groupId>
<artifactId>log4jdbc-log4j2-jdbc4</artifactId>
<version>1.16</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.oracle.database.jdbc/ojdbc6 -->
<!-- ojdbc6 : oracle java database connectivity 드라이버 -->
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.4</version>
</dependency>
<!-- Database 라이브러리 시작 -->
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<!--
1) 자바빈 클래스(VO)의 getter/setter메소드, toString 자동처리 =>POJO 약해짐
2) log.info() 메소드로 sysout대신 로그를 console에 출력
1. 개요
메이븐을 사용하게 되면 POM 파일을 다뤄야 합니다.
POM은 Project Object Model의 약자인데...
pom.xml이라는 파일이 바로 POM 파일이고 이 파일을 통해 dependency라고 부르는 의존성 설정을 하게 됩니다...
그리고 각 dependency는 scope을 가지고 있어요...
종류는 compile, runtime, provided 등의 종류가 있지요...
2. compile
compile은 컴파일때 필요한 것입니다... 기본값이기 때문에 만일 scope 설정을 안하면 compile이 기본입니다...
3. runtime
runtime은 런타임때 필요한 것입니다. 컴파일할 때는 필요가 없지만 실행할 때는 필요한 것이지요.
JDBC 드라이버 같은게 되겠습니다.
4. provided
provided는 컴파일때 필요하긴 하지만 런타임때는 JDK 혹은 컨테이너가 제공하는 것입니다.
마지막 패키징 시 포함되지 않음
서블릿이나 JSP 관련 API 같은거죠.
즉 WAS에서 제공하는 servlet-api.jar를 사용하는 경우 (대부분이겠죠) 입니다.
만약 운영환경에서 servlet-api.jar 중복으로 인한 문제가 발생한다면 꼭 provided로 바꿔주셔야 합니다.
5. test
test는 조금 특별한데 테스트 코드를 컴파일할때 필요하지만 배포때는 제외가 됩니다. (따라서 굳이 운영 환경에 JUnit 의존성을 삭제할 필요가 없어요)
easymock, JUnit 같은거죠.
6. system
system은 provided와 유사하지만 JAR 파일을 직접 사용합니다...
이 때는 JAR 파일의 위치를 지정하는 systemPath 엘리먼트가 따라와야 해요...
-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
<scope>provided</scope>
</dependency>
<!-- 타일즈 시작 -->
<!-- https://mvnrepository.com/artifact/org.apache.tiles/tiles-extras -->
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-extras</artifactId>
<version>3.0.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.tiles/tiles-servlet -->
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-servlet</artifactId>
<version>3.0.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.tiles/tiles-jsp -->
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-jsp</artifactId>
<version>3.0.8</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>
tiles-config.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 설정 -->
<tiles-definitions>
<!-- 메인 layout -->
<definition name="main-layout" template="/WEB-INF/views/tiles/mainTemplate.jsp">
<put-attribute name="header" value="/WEB-INF/views/tiles/mainHeader.jsp"/>
<put-attribute name="aside" value="/WEB-INF/views/tiles/aside.jsp"/>
<put-attribute name="body" value=""/>
<put-attribute name="footer" value="/WEB-INF/views/tiles/footer.jsp"/>
</definition>
<!--
mav.setViewName("lprod/create");
/WEB-INF/views/lprod/create.jsp
-->
<definition name="*/*" extends="main-layout">
<put-attribute name="body" value="/WEB-INF/views/{1}/{2}.jsp"></put-attribute>
</definition>
</tiles-definitions>
'JAVA > spring' 카테고리의 다른 글
컨트롤러 응답 (0) | 2024.05.04 |
---|---|
컨트롤러 요청 매핑 (0) | 2024.04.29 |
ckeditor5 사용법 (0) | 2024.04.26 |
CRUD 용어 정리(예제 BOOK) (0) | 2024.04.24 |
spring 프로젝트 환경설정 (0) | 2024.04.22 |