Struts2011.09.14 13:18

버전

struts-2.2.3.1

tiles-2.2.2

freemarker-2.3.18


최소 필요 라이브러리

commons-beanutils-1.7.0.jar

commons-digester-2.0.jar

commons-fileupload-1.2.2.jar

commons-io-2.0.1.jar

commons-lang-2.5.jar

commons-logging-1.1.1.jar

freemarker.jar

javassist-3.11.0.GA.jar

log4j-1.2.16.jar

ognl-3.0.1.jar

slf4j-api-1.6.1.jar

slf4j-log4j12-1.6.1.jar

struts2-core-2.2.3.1.jar

struts2-tiles-plugin-2.2.3.1.jar

tiles-api-2.2.2.jar

tiles-core-2.2.2.jar

tiles-freemarker-2.2.2.jar

tiles-jsp-2.2.2.jar

tiles-servlet-2.2.2.jar

tiles-template-2.2.2.jar

xwork-core-2.2.3.1.jar


/WebContent/META-INF/tld/tiles-jsp.tld


web.xml

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

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

id="WebApp_ID" version="2.5">


<display-name>struts2-tiles2</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>


<listener>

<listener-class>local.common.listener.TestTilesListener</listener-class>

</listener>


<servlet>

<servlet-name>freemarker</servlet-name>

<servlet-class>org.apache.tiles.freemarker.servlet.TilesFreemarkerServlet</servlet-class>


<!-- FreemarkerServlet settings: -->

<init-param>

<param-name>TemplatePath</param-name>

<param-value>/</param-value>

</init-param>

<init-param>

<param-name>NoCache</param-name>

<param-value>true</param-value>

</init-param>

<init-param>

<param-name>ContentType</param-name>

<param-value>text/html</param-value>

</init-param>


<!-- FreeMarker settings: -->

<init-param>

<param-name>template_update_delay</param-name>

<param-value>0</param-value> <!-- 0 is for development only! Use higher value otherwise. -->

</init-param>

<init-param>

<param-name>default_encoding</param-name>

<param-value>UTF-8</param-value>

</init-param>

<init-param>

<param-name>number_format</param-name>

<param-value>0.##########</param-value>

</init-param>


<load-on-startup>5</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>freemarker</servlet-name>

<url-pattern>*.ftl</url-pattern>

</servlet-mapping>


<welcome-file-list>

<welcome-file>index.html</welcome-file>

</welcome-file-list>


</web-app>


TestTilesContainerFactory, TestTilesListener

public class TestTilesContainerFactory extends BasicTilesContainerFactory {


    @Override

    protected List<URL> getSourceURLs(TilesApplicationContext applicationContext,

            TilesRequestContextFactory contextFactory) {

        List<URL> urls = new ArrayList<URL>();

        try {

            urls.add(applicationContext.getResource("/WEB-INF/tiles.xml"));

        } catch (IOException e) {

            throw new DefinitionsFactoryException("Cannot load definition URLs", e);

        }

        return urls;

    }

    

    @Override

    protected void registerAttributeRenderers(

    BasicRendererFactory rendererFactory,

    TilesApplicationContext applicationContext,

    TilesRequestContextFactory contextFactory,

    TilesContainer container,

    AttributeEvaluatorFactory attributeEvaluatorFactory) {

    super.registerAttributeRenderers(rendererFactory, applicationContext,

    contextFactory, container, attributeEvaluatorFactory);

   

FreeMarkerAttributeRenderer freemarkerRenderer = new FreeMarkerAttributeRenderer();

   freemarkerRenderer.setApplicationContext(applicationContext);

   freemarkerRenderer.setAttributeEvaluatorFactory(attributeEvaluatorFactory);

   freemarkerRenderer.setRequestContextFactory(contextFactory);

   freemarkerRenderer.setParameter("TemplatePath", "/");

   freemarkerRenderer.setParameter("NoCache", "true");

   freemarkerRenderer.setParameter("ContentType", "text/html");

   freemarkerRenderer.setParameter("template_update_delay", "0");

   freemarkerRenderer.setParameter("default_encoding", "UTF-8");

   freemarkerRenderer.setParameter("number_format", "0.##########");

   freemarkerRenderer.commit();

   rendererFactory.registerRenderer("freemarker", freemarkerRenderer);

    }

    

}


public class TestTilesListener extends AbstractTilesListener {


    @Override

    protected TilesInitializer createTilesInitializer() {

        return new TestTilesListenerInitializer();

    }


    private static class TestTilesListenerInitializer extends AbstractTilesInitializer {


        @Override

        protected AbstractTilesContainerFactory createContainerFactory(

                TilesApplicationContext context) {

            return new TestTilesContainerFactory();

        }

    }

}


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>


<package name="common-default" extends="struts-default">

<result-types>

<result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult" />

</result-types>

<action name="index">

<result type="tiles" name="success">index</result>

</action>

</package>


</struts>


struts.properties

struts.action.extension=action,, -> struts.action.extension=action 변경


log4j.properties

log4j.rootLogger=INFO, stdout


log4j.appender.stdout=org.apache.log4j.ConsoleAppender

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern=%d{yyyy/MM/dd HH:mm} %-5p (%F:%L) - %m%n


tiles.xml

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

<!DOCTYPE tiles-definitions PUBLIC

       "-//Apache Software Foundation//DTD Tiles Configuration 2.1//EN"

       "http://tiles.apache.org/dtds/tiles-config_2_1.dtd">


<tiles-definitions>


<definition name="layout" template="/template/layout/layout.ftl">

<put-attribute name="title" value="web" />

<put-attribute name="header" value="/template/layout/header.ftl" type="freemarker" />

<put-attribute name="left" value="/template/layout/left.ftl" type="freemarker" />

<put-attribute name="footer" value="/template/layout/footer.ftl" type="freemarker" />

</definition>

<definition name="*" extends="layout">

<put-attribute name="body" value="{1}.ftl" type="freemarker" />

</definition>

</tiles-definitions>


/WebContent/template/layout/layout.ftl

<#assign tiles = JspTaglibs["/META-INF/tld/tiles-jsp.tld"] />

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="kr" xml:lang="kr" >

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<title><@tiles.getAsString name="title" /></title>

</head>

<body>

<@tiles.insertAttribute name="header" />


<@tiles.insertAttribute name="left" />


<@tiles.insertAttribute name="body" />


<@tiles.insertAttribute name="footer" />

</body>

</html>


마지막

/WebContent/template/layout/header.ftl

/WebContent/template/layout/left.ftl

/WebContent/template/layout/footer.ftl

작성 후

body 부분은 url 호출되는 내용을 보여주면 된다.

예) 첫 화면 페이지 index.ftl


끝.

신고
Posted by 수앙

댓글을 달아 주세요


티스토리 툴바