|
|
MVC框架集成指南 |
|
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。
Web配置查找顺序:
1.首先查找web.xml中的context-param配置:commontemplate-config参数所指的路径。如:
<context-param>
<param-name>commontemplate-config</param-name>
<param-value>/WEB-INF/commontemplate.properties</param-value>
</context-param>
(注:如果配置路径以 / 开头则表示在web应用目录下,否则在ClassPath下查找)
2.如果未配置,则查找默认WEB-INF路径:/WEB-INF/commontemplate.properties
3.如果WEB-INF中没有,则查找ClassPath根目录:commontemplate.properties
4.如果ClassPath根目录也没有,则使用标准配置:org/commontemplate/tools/web/commontemplate.properties
(注:如果自定义配置文件中未配置@extends项,则缺省为:@extends=org/commontemplate/tools/web/commontemplate.properties)
2. 与Struts集成 依赖...
Struts项目地址:http://struts.apache.org/1.x/
同上面配置好Servlet,在Struts中直接forward到相应.ctl页面
(注:与Struts的集成方案请参见下载列表中: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>
<!-- 不允许直接访问ctl模板 -->
<security-constraint>
<web-resource-collection>
<web-resource-name>CTL Templates</web-resource-name>
<url-pattern>*.ctl</url-pattern>
</web-resource-collection>
<auth-constraint>
<description>Forbidden</description>
</auth-constraint>
</security-constraint>
</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集成 依赖...
Spring项目地址:http://www.springframework.org/
(注:与SpringMVC的集成方案请参见下载列表中: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集成 依赖...
WebWork项目地址:http://webwork.opensymphony.com/
(注:与WebWork的集成方案请参见下载列表中:ct_webwork.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>
<!-- 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项目地址:http://struts.apache.org/2.x/
(注:与Struts2的集成方案请参见下载列表中: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>
|
|
|