本文整理匯總了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);
}