本文整理汇总了Java中freemarker.template.Configuration.setServletContextForTemplateLoading方法的典型用法代码示例。如果您正苦于以下问题:Java Configuration.setServletContextForTemplateLoading方法的具体用法?Java Configuration.setServletContextForTemplateLoading怎么用?Java Configuration.setServletContextForTemplateLoading使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类freemarker.template.Configuration
的用法示例。
在下文中一共展示了Configuration.setServletContextForTemplateLoading方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: initTemplateEngineCfg
import freemarker.template.Configuration; //导入方法依赖的package包/类
/**
* Initializes template engine configuration.
*/
private void initTemplateEngineCfg() {
configuration = new Configuration();
configuration.setDefaultEncoding("UTF-8");
final ServletContext servletContext = ContextLoader.getCurrentWebApplicationContext().getServletContext();
configuration.setServletContextForTemplateLoading(servletContext, "/plugins/" + dirName);
logger.debug("Initialized template configuration:{}", dirName);
readLangs();
}
示例2: crateHTML
import freemarker.template.Configuration; //导入方法依赖的package包/类
/**
* 生成静态页面主方法
*
* @param context
* ServletContext
* @param data
* 一个Map的数据结果集
* @param templatePath
* ftl模版路径
* @param targetHtmlPath
* 生成静态页面的路径
*/
public static void crateHTML(ServletContext context, Object data, String templatePath, String targetHtmlPath) {
Configuration freemarkerCfg = new Configuration();
// 加载模版
freemarkerCfg.setServletContextForTemplateLoading(context, "/");
freemarkerCfg.setEncoding(Locale.getDefault(), "UTF-8");
try {
// 指定模版路径
Template template = freemarkerCfg.getTemplate(templatePath, "UTF-8");
template.setEncoding("UTF-8");
// 静态页面路径
String htmlPathString = context.getRealPath("/") + "/" + targetHtmlPath;
File htmlFile = new File(htmlPathString);
if (!htmlFile.getParentFile().exists()) {
htmlFile.getParentFile().mkdirs();
}
htmlFile.createNewFile();
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(htmlFile), "UTF-8"));
// 处理模版
template.process(data, out);
out.flush();
out.close();
} catch (Exception e) {
logger.error("error occured where generate Html file.", e);
}
}
示例3: DocumentTemplate
import freemarker.template.Configuration; //导入方法依赖的package包/类
public DocumentTemplate(javax.servlet.ServletContext servletContext,
String pathPrefix) {
cf = new Configuration();
try {
cf.setServletContextForTemplateLoading(servletContext, pathPrefix);
} catch (Exception e) {
throw new AppRuntimeException(e);
}
}
示例4: FreemarkerTemplate
import freemarker.template.Configuration; //导入方法依赖的package包/类
public FreemarkerTemplate()
{
cfg = new Configuration();
try
{
//File cfgPath = new File(config.getConfigurationPath());
//cfg.setDirectoryForTemplateLoading( new File(cfgPath.getParent()) );
cfg.setServletContextForTemplateLoading(TernContext.getServletContext(),"/WEB-INF/views");
//cfg.setTemplateLoader(loader)
//cfg.setLocale(Locale.CHINA);
cfg.setSetting("date_format", "yyyy-MM-dd");
cfg.setSetting("time_format", "HH:mm:ss");
cfg.setSetting("datetime_format", "yyyy-MM-dd HH:mm:ss");
}
catch (Exception e)
{
cfg = null;
Trace.write(Trace.Error, e,"init freemarker failed!");
return;
}
cfg.setDefaultEncoding(config.getEncoding());
cfg.setOutputEncoding(config.getEncoding());
wrapper = new ActionDataWrapper();//ObjectWrapper.BEANS_WRAPPER;//.DEFAULT_WRAPPER;
/*wrapper = new DefaultObjectWrapper(){
public TemplateModel wrap(Object obj) throws TemplateModelException {
if(obj instanceof com.tern.dao.RecordSet){
return new freemarker.template.SimpleCollection(((com.tern.dao.RecordSet)obj).iterator(), this);
}
return super.wrap(obj);
}
};*/
cfg.setObjectWrapper(wrapper);
cfg.setTemplateExceptionHandler(new TemplateExceptionHandler(){
@Override
public void handleTemplateException(TemplateException te,
Environment env, Writer writer) throws TemplateException {
if(config.isDebug())
{
if(te.getCause() instanceof RedirectRequest)
{
throw (RedirectRequest)te.getCause();
}
TemplateExceptionHandler.HTML_DEBUG_HANDLER.handleTemplateException(te, env, writer);
}
throw te;
}
});
/*if(config.isDebug())
{
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.HTML_DEBUG_HANDLER);
cfg.setTemplateUpdateDelay(0);
}
else
{
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
cfg.setTemplateUpdateDelay(config.getInt("template.delay", 3600));
}*/
if(!config.isDebug())
{
cfg.setTemplateUpdateDelay(config.getInt("template.delay", 3600));
}
}
示例5: execute
import freemarker.template.Configuration; //导入方法依赖的package包/类
public void execute(PageContext pageContext) throws Exception {
Configuration cfg = new Configuration();
cfg.setServletContextForTemplateLoading(pageContext.getServletContext(), "WEB-INF/templates");
Map root = new HashMap();
root.put("name", "Tom");
Template t = cfg.getTemplate("test.ftl");
Writer out = pageContext.getResponse().getWriter();
t.process(root, out);
}