|
|
CommonTemplate 集成指南 |
|
一. 引擎使用集成方案 依赖包>>
(1) 与Servlet集成
配置web.xml:
<!-- 模板引擎初始化配置 -->
<listener>
<listener-class>org.commontemplate.tools.web.EngineInitializeListener</listener-class>
</listener>
<!-- 字符编码过滤器(也可以用其它工具提供的过滤器) -->
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.commontemplate.tools.web.EncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>*.ctl</url-pattern>
</filter-mapping>
<!-- 模板Servlet配置 -->
<servlet>
<servlet-name>commontemplate</servlet-name>
<servlet-class>org.commontemplate.tools.web.servlet.TemplateServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>commontemplate</servlet-name>
<url-pattern>*.ctl</url-pattern>
</servlet-mapping>
此Servlet只用于模板显现,使用时需要在其它Servlet中填充数据后forward到此Servlet。
通常用于与没有提供页面handler的MVC框架集成,如:Struts1, SpringMVC等,
在Web框架的Action/Controller中填充数据后forward到xxx.ctl。
(2) 与Struts集成
同上面配置好Servlet,在Struts中直接forward到相应.ctl页面
(注:与Struts的集成方案请参见下载列表中:commontemplate-example.zip 内的 ct_struts.war) 下载>>
配置web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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">
<!-- 模板引擎初始化配置 -->
<listener>
<listener-class>org.commontemplate.tools.web.EngineInitializeListener</listener-class>
</listener>
<!-- 字符编码过滤器(也可以用其它工具提供的过滤器) -->
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.commontemplate.tools.web.EncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>*.ctl</url-pattern>
</filter-mapping>
<!-- 模板Servlet配置 -->
<servlet>
<servlet-name>commontemplate</servlet-name>
<servlet-class>org.commontemplate.tools.web.servlet.TemplateServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>commontemplate</servlet-name>
<url-pattern>*.ctl</url-pattern>
</servlet-mapping>
<!-- Struts配置 -->
<servlet>
<servlet-name>struts</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>struts</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
配置struts.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<form-beans>
<form-bean name="userForm" type="com.xxx.UserForm" />
</form-beans>
<action-mappings>
<action path="/user/list" name="userForm" type="com.xxx.UserAction">
<forward name="success" path="/user/list.ctl" />
</action>
</action-mappings>
</struts-config>
CommonTemplate在MVC中的位置:
(3) 与SpringMVC集成
(注:与SpringMVC的集成方案请参见下载列表中:commontemplate-example.zip 内的 ct_springmvc.war) 下载>>
配置web.xml:
<!-- Spring容器初始化配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 模板引擎初始化配置 -->
<listener>
<listener-class>org.commontemplate.tools.web.EngineInitializeListener</listener-class>
</listener>
<!-- 字符编码过滤器(也可以用其它工具提供的过滤器) -->
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.commontemplate.tools.web.EncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<!-- SpringMVC配置 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
配置springmvc-servlet.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="viewResolver" class="org.commontemplate.tools.web.spring.CommonTemplateViewResolver">
<property name="suffix" value=".ctl"/>
<property name="contentType" value="text/html; charset=UTF-8" />
</bean>
<bean id="defaultHandlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<bean name="/list.do" class="com.xxx.web.spring.UserListController">
<property name="successView" value="list"/>
</bean>
</beans>
(4) 与WebWork集成
配置web.xml:
<!-- 模板引擎初始化配置 -->
<listener>
<listener-class>org.commontemplate.tools.web.EngineInitializeListener</listener-class>
</listener>
<!-- 字符编码过滤器(也可以用其它工具提供的过滤器) -->
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.commontemplate.tools.web.EncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<!-- WebWork配置 -->
<servlet>
<servlet-name>webwork</servlet-name>
<servlet-class>com.opensymphony.webwork.dispatcher.ServletDispatcher</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>webwork</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
配置xwork.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xwork PUBLIC
"-//OpenSymphony Group//XWork 1.1.1//EN"
"http://www.opensymphony.com/xwork/xwork-1.1.1.dtd">
<xwork>
<include file="webwork-default.xml" />
<package name="test" extends="webwork-default">
<result-types>
<result-type default="true" name="commontemplate" class="org.commontemplate.tools.web.webwork.TemplateResult" />
</result-types>
<action name="index" class="com.xxx.IndexAction" method="index">
<result name="success" type="commontemplate">/index.ctl</result>
</action>
</package>
</xwork>
(5) 与Struts2集成
(注:与Struts2的集成方案请参见下载列表中:commontemplate-example.zip 内的 ct_struts2.war) 下载>>
配置web.xml:
<!-- 模板引擎初始化配置 -->
<listener>
<listener-class>org.commontemplate.tools.web.EngineInitializeListener</listener-class>
</listener>
<!-- 字符编码过滤器(也可以用其它工具提供的过滤器) -->
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.commontemplate.tools.web.EncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<!-- Struts2配置 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
配置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="test" extends="commontemplate-default">
<action name="index" class="com.xxx.IndexAction" method="index">
<result name="success" type="commontemplate">/index.ctl</result>
</action>
</package>
</struts>
(6) 与JSP集成
test.jsp:
<%@taglib uri="commontemplate" prefix="ct" %>
<ct:template>
JSP or/and CTL...
</ct:template>
如果服务器不支持Servlet2.4或以上版本, 需在web.xml中配置tld的引用:
<taglib>
<taglib-uri>commontemplate</taglib-uri>
<taglib-location>/WEB-INF/commontemplate.tld</taglib-location>
</taglib>
二. 配置供给集成方案 依赖包>>
(1) 与SpringIoC容器集成
...
(2) 与CommonsLogging/Log4J集成
配置commontemplate.properties:
@extends=org/commontemplate/tools/web/commontemplate.properties
logger=org.commontemplate.ext.log.CommonsLogging()
配置commons-logging.properties:
logger=org.apache.commons.logging.impl.Log4JLogger
配置log4j.properties:
log4j.rootLogger=DEBUG,stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-5p [%d] %C - %m\n
log4j.logger.CommonTemplate=DEBUG
(3) 与OSCache集成
配置commontemplate.properties:
@extends=org/commontemplate/tools/web/commontemplate.properties
templateCache=org.commontemplate.ext.cache.OSCache()
oscache.properties:
cache.algorithm=com.opensymphony.oscache.base.algorithm.LRUCache
cache.capacity=1000
(4) 与EHCache集成
配置commontemplate.properties:
@extends=org/commontemplate/tools/web/commontemplate.properties
templateCache=org.commontemplate.ext.cache.EHCache()
配置ehcache.xml:
<ehcache>
<diskStore path="java.io.tmpdir" />
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
/>
</ehcache>
(5) 与TagLib集成
配置commontemplate.properties:
directive{textfield}=org.commontemplate.ext.directive.taglib.TagLineDirectiveAdapter()
directive{textfield}.tagClassName=org.apache.struts2.views.jsp.ui.TextFieldTag
|
|
|