Struts 2.1.8 과 Tiles 2.1.4 버전을 이용하는 예제

Tiles 프레임워크의 기본개념 및 다른 프레임워크와 연동하지 않고 사용하는 예는 여기를 참조하세요.
Spring 2.5.6 과 Tiles 2.0.5 사용예제는 여기를 참조하세요.

apache.org 사이트에서 struts 2.1.8_all.zip 파일과 Tiles 2.1.4.zip 파일을 다운로드하여 압축을 해제하고 struts-2.1.8.blank.war 파일을 찾는다.




Eclipse 3.5를 실행하여 Project Explorer에서 마우스 우측을 클릭 > import > war > struts-2.1.8.blank.war 을 선택하여 Eclipse에 프로젝트를 생성한다.

생성된 프로젝트에 디폴트로 포함된 라이브러리 만으로는 Tiles 를 사용하기 위해서는 부족하므로 다음과 같은 라이브러리를 추가로 import 해 주어야 한다.

Struts 2.1.8에 포함된 라이브러리
- commons-beanutils-1.7.0.jar
- commons-digester-2.0.jar
- commons-logging-1.0.4.jar
- struts2-tiles-plugin-2.1.8.1.jar


Tiles 2.1.4 버전에 포함된 라이브러리

- tiles-api-2.1.4.jar
- tiles-core-2.1.4.jar
- tiles-jsp-2.1.4.jar
- tiles-servlet-2.1.4.jar

Struts 2.1.8-blank.war에 포함된 라이브러리 외에 추가로 포함되어야 할 라이브러리 다운로드




위의 라이브러리가 모두 프로젝트에 포함되었다면, 다음과 같이 설정파일을 작성한다.

WebContent/WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Struts Blank</display-name>

    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
   
    <!-- Tiles Listener -->

 <listener>
     <listener-class>
       org.apache.struts2.tiles.StrutsTilesListener
     </listener-class>
 </listener>
    <context-param>
           <description>Tiles configuration file</description>
           <param-name>
                   org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG
           </param-name>
           <param-value>/WEB-INF/classes/tilesdef.xml</param-value>
   </context-param>

</web-app>





WebContent/WEB-INF/classes/tilesdef.xml

<?xml version="1.0" encoding="UTF-8" ?>

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

<tiles-definitions>
 <definition name="base_layout" template="/layout.jsp">
  <put-attribute name="title">레이아웃페이지</put-attribute>
  <put-attribute name="header" value="/header.jsp"/>
  <put-attribute name="menu" value="/menu.jsp"/>
  <put-attribute name="body" value="/body.jsp"/>
  <put-attribute name="footer" value="/footer.jsp"/>
 </definition>
 
 <definition name="hello" extends="base_layout">
  <put-attribute name="body" value="/hello.jsp"/>
 </definition>
 
 <definition name="logform" extends="base_layout">
  <put-attribute name="body" value="/logForm.jsp"/>
 </definition>
 
</tiles-definitions>



WebContent/WEB-INF/classes/struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="false" />

    <include file="example.xml"/>


    <package name="default" namespace="/" extends="struts-default">
        <default-action-ref name="index" />
        <action name="index">
            <result type="redirectAction">
                <param name="actionName">HelloWorld</param>
                <param name="namespace">/example</param>
            </result>
        </action>
    </package>

    <!-- Add packages here -->

   <package name="tiles" namespace="/tiles" extends="struts-default, tiles-default">
        <action name="index">
            <result type="tiles">base_layout</result>
        </action>
        <action name="menu1">
            <result type="tiles">hello</result>
        </action>
        <action name="menu2">
            <result type="tiles">logform</result>
        </action>
    </package>

</struts>



WebContent/layout.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>

<!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=EUC-KR">
<title><tiles:getAsString name="title"/></title>
</head>
<body>
 <center>
 <table>
 <tr><td colspan="2"><tiles:insertAttribute name="header"/></td></tr>
 <tr><td><tiles:insertAttribute name="menu"/></td>
 <td><tiles:insertAttribute name="body"/></td></tr>
 <tr><td colspan="2"><tiles:insertAttribute name="footer"/></td></tr>
 </table>
 </center>
</body>
</html>



WebContent/header.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<div style="border:2px double black;">
<center>
<h1>여기는 header.jsp의 내용</h1>
</center>
</div>



WebContent/menu.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<div style="border:1px solid blue;width:100px; height:300px;">
Menu
</div>



WebContent/body.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<div style="border:2px dashed red; height:300px;">
<center>
<h1>여기는 body.jsp의 내용</h1>
</center>
</div>



WebContent/footer.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<div style="border:2px solid green;">
<center>
<h1>여기는 footer.jsp의 내용</h1>
</center>
</div>



WebContent/hello.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<div style="border:2px solid green;">
<center>
<h1>여기는 hello.jsp의 내용</h1>
</center>
</div>


테스트
http://localhost/컨텍스트명/tiles/index.action
http://localhost/컨텍스트명/tiles/menu1.action
http://localhost/컨텍스트명/tiles/menu2.action

'Java' 카테고리의 다른 글

스프링 AOP  (0) 2010.07.16
Spring Framework의 기본 개념 Struts / Spring / Architecture  (0) 2010.07.15
스트러츠(struts)를 써보자.  (0) 2010.07.14
[read] JDOM SAX 파싱예제... (parser)  (0) 2010.06.29
JDOM(THE JAVA DOM)  (0) 2010.06.29

+ Recent posts