CommonTemplate与Struts2集成

现在用Struts2的人开始多起来,为CommonTemplate( http://www.commontemplate.org/)加了一个与Struts2的集成方案,

Struts2( http://struts.apache.org/2.x/)与Webwork一样,提供了模板输出hook,
所以集成是比较简单的,只需实现StrutsResultSupport的doExecute就行了。
下面是暂时的方案,有待调优。

package org.commontemplate.side.struts2;

import java.io.Writer;
import java.util.Locale;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.StrutsResultSupport;

import org.commontemplate.core.Context;
import org.commontemplate.core.Factory;
import org.commontemplate.core.TemplateException;
import org.commontemplate.engine.Engine;
import org.commontemplate.side.exception.HtmlTemplateExceptionHandler;
import org.commontemplate.side.map.ApplicationMap;
import org.commontemplate.side.map.ModelMap;
import org.commontemplate.side.map.RequestMap;
import org.commontemplate.side.map.SessionMap;
import org.commontemplate.standard.ConfigurationLoader;
import org.commontemplate.standard.ConfigurationSetting;
import org.commontemplate.standard.loader.WebappTemplateSourceLoader;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.LocaleProvider;

public class TemplateResult extends StrutsResultSupport {

	private static final long serialVersionUID = 1L;
	
	private Factory factory;

	@Override
	protected void doExecute(String finalLocation, ActionInvocation invocation)
			throws Exception {
		synchronized(this) {
			if (this.factory == null) {
				this.factory = initFactory();
			}
		}
		Writer output = ServletActionContext.getResponse().getWriter();
		Context context = null;
		try {
			context = factory.createContext(output, getLocale(invocation));
			context.pushLocalContext("application", new ApplicationMap(ServletActionContext.getRequest().getSession().getServletContext()));
			context.pushLocalContext("session", new SessionMap(ServletActionContext.getRequest()));
			context.pushLocalContext("request", new RequestMap(ServletActionContext.getRequest()));
			context.pushLocalContext(new ModelMap(invocation.getAction()));
			factory.getTemplate(finalLocation).render(context);
		} catch (TemplateException e) {
			new HtmlTemplateExceptionHandler(output).handleTemplateException(e);
		} finally {
			try {
				if (context != null) 
					context.clear();
				output.flush();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	protected Factory initFactory() {
		ConfigurationSetting config = ConfigurationLoader.loadStandardConfiguration();
		config.setTemplateSourceLoader(new WebappTemplateSourceLoader(ServletActionContext.getServletContext()));
		return new Engine(config);
	}

	protected Locale getLocale(ActionInvocation invocation) {
        if (invocation.getAction() instanceof LocaleProvider) {
            return ((LocaleProvider) invocation.getAction()).getLocale();
        } else {
            return Locale.getDefault();
        }
    }

}


测试:
配置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>

	<include file="struts-default.xml" />

	<package name="test" extends="struts-default">
		<result-types>
			<result-type name="commontemplate"
				class="org.commontemplate.side.struts2.TemplateResult" />
		</result-types>
		<action name="index" class="com.xxx.IndexAction" method="index">
			<result type="commontemplate">index.ctl</result>
		</action>
	</package>

</struts>

简单的index.ctl
<html>
	<head>
	</head>
	<body>
	$for{num : 1..9}
		${num}
	$end
	</body>
</html>

你可能感兴趣的:(apache,xml,struts,Webwork,commontemplate)