本文整理匯總了Java中freemarker.template.Template類的典型用法代碼示例。如果您正苦於以下問題:Java Template類的具體用法?Java Template怎麽用?Java Template使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
Template類屬於freemarker.template包,在下文中一共展示了Template類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: main
import freemarker.template.Template; //導入依賴的package包/類
public static void main(String[] args) {
Map<String,Object> tmp = new HashMap<>();
tmp.put("user","邢天宇");
Configuration cfg = new Configuration(Configuration.VERSION_2_3_23);
StringTemplateLoader loader = new StringTemplateLoader();
loader.putTemplate(NAME,"hello ${user}");
cfg.setTemplateLoader(loader);
cfg.setDefaultEncoding("UTF-8");
try {
Template template = cfg.getTemplate(NAME);
StringWriter writer = new StringWriter();
template.process(tmp,writer);
System.out.println(writer.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
示例2: service
import freemarker.template.Template; //導入依賴的package包/類
@Override
public void service(ServletRequest servletRequest, ServletResponse response) throws ServletException, IOException {
ClientSettings settings = new ClientSettings(
options.getOption(SupportService.SUPPORT_EMAIL_ADDR),
options.getOption(SupportService.SUPPORT_EMAIL_SUBJECT),
options.getOption(SupportService.OUTSIDE_COMMUNICATION_DISABLED),
options.getOption(AccelerationOptions.ENABLE_SUBHOUR_POLICIES),
options.getOption(UIOptions.ALLOW_LOWER_PROVISIONING_SETTINGS),
options.getOption(UIOptions.TABLEAU_TDS_MIMETYPE));
String environment = config.allowTestApis ? "DEVELOPMENT" : "PRODUCTION";
final ServerData indexConfig = new ServerData(environment, serverHealthMonitor, config.getConfig(), settings, getVersionInfo(), supportService.getClusterId().getIdentity());
Template tmp = templateCfg.getTemplate("/index.html");
response.setContentType("text/html; charset=utf-8");
OutputStreamWriter outputWriter = new OutputStreamWriter(response.getOutputStream());
try {
tmp.process(ImmutableMap.of("dremio", indexConfig), outputWriter);
outputWriter.flush();
outputWriter.close();
} catch (TemplateException e) {
throw new IOException("Error rendering index.html template", e);
}
}
示例3: shouldBuildStatusReportView
import freemarker.template.Template; //導入依賴的package包/類
@Test
public void shouldBuildStatusReportView() throws Exception {
NodeOperationsImpl nodes = mock(NodeOperationsImpl.class);
PodOperationsImpl pods = mock(PodOperationsImpl.class);
when(nodes.list()).thenReturn(new NodeList());
when(kubernetesClient.nodes()).thenReturn(nodes);
when(pods.inNamespace(Constants.KUBERNETES_NAMESPACE_KEY)).thenReturn(pods);
when(pods.withLabel(Constants.CREATED_BY_LABEL_KEY, Constants.PLUGIN_ID)).thenReturn(pods);
when(pods.list()).thenReturn(new PodList());
when(kubernetesClient.pods()).thenReturn(pods);
final PluginStatusReportViewBuilder builder = mock(PluginStatusReportViewBuilder.class);
final Template template = mock(Template.class);
when(builder.getTemplate("status-report.template.ftlh")).thenReturn(template);
when(builder.build(eq(template), any(KubernetesCluster.class))).thenReturn("status-report");
final GoPluginApiResponse response = new StatusReportExecutor(pluginRequest, kubernetesClientFactory, builder).execute();
assertThat(response.responseCode(), is(200));
assertThat(response.responseBody(), is("{\"view\":\"status-report\"}"));
}
示例4: allSignatures
import freemarker.template.Template; //導入依賴的package包/類
public void allSignatures(String inputFile) throws IOException, TemplateException {
Configuration cfg = new Configuration();
Template template = cfg.getTemplate(inputFile);
Map<String, Object> data = new HashMap<String, Object>();
template.process(data, new OutputStreamWriter(System.out)); //TP
template.process(data, new OutputStreamWriter(System.out), null); //TP
template.process(data, new OutputStreamWriter(System.out), null, null); //TP
}
示例5: genHTML
import freemarker.template.Template; //導入依賴的package包/類
/**
* Processes the specified FreeMarker template with the specified request,
* data model.
*
* @param request
* the specified request
* @param dataModel
* the specified data model
* @param template
* the specified FreeMarker template
* @return generated HTML
* @throws Exception
* exception
*/
protected String genHTML(final HttpServletRequest request, final Map<String, Object> dataModel,
final Template template) throws Exception {
final StringWriter stringWriter = new StringWriter();
template.setOutputEncoding("UTF-8");
template.process(dataModel, stringWriter);
final StringBuilder pageContentBuilder = new StringBuilder(stringWriter.toString());
final long endimeMillis = System.currentTimeMillis();
final String dateString = DateFormatUtils.format(endimeMillis, "yyyy/MM/dd HH:mm:ss");
// final long startTimeMillis = (Long) request.getAttribute(Keys.HttpRequest.START_TIME_MILLIS);
final long startTimeMillis = System.currentTimeMillis();
final String msg = String.format("<!-- Generated by B3log Latke(%1$d ms), %2$s -->",
endimeMillis - startTimeMillis, dateString);
pageContentBuilder.append(msg);
return pageContentBuilder.toString();
}
示例6: getViewContent
import freemarker.template.Template; //導入依賴的package包/類
/**
* Gets view content of a plugin. The content is processed with the
* specified data model by template engine.
*
* @param dataModel
* the specified data model
* @return plugin view content
*/
private String getViewContent(final Map<String, Object> dataModel) {
if (null == configuration) {
initTemplateEngineCfg();
}
try {
final Template template = configuration.getTemplate("plugin.ftl");
final StringWriter sw = new StringWriter();
template.process(dataModel, sw);
return sw.toString();
} catch (final Exception e) {
// This plugin has no view
return "";
}
}
示例7: render
import freemarker.template.Template; //導入依賴的package包/類
@Override
public void render(final HttpServletRequest request, final HttpServletResponse response) {
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
try {
final Template template = ConsoleRenderer.TEMPLATE_CFG.getTemplate("kill-browser.ftl");
final PrintWriter writer = response.getWriter();
final StringWriter stringWriter = new StringWriter();
template.setOutputEncoding("UTF-8");
template.process(getDataModel(), stringWriter);
final String pageContent = stringWriter.toString();
writer.write(pageContent);
writer.flush();
writer.close();
} catch (final Exception e) {
try {
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
} catch (final IOException ex) {
logger.error("Can not sned error 500!", ex);
}
}
}
示例8: hasExpression
import freemarker.template.Template; //導入依賴的package包/類
/**
* Determines whether the specified expression exists in the specified
* element (includes its children) of the specified template.
*
* @param template
* the specified template
* @param expression
* the specified expression
* @param templateElement
* the specified element
* @return {@code true} if it exists, returns {@code false} otherwise
*/
private static boolean hasExpression(final Template template, final String expression,
final TemplateElement templateElement) {
final String canonicalForm = templateElement.getCanonicalForm();
if (canonicalForm.startsWith(expression)) {
logger.trace("Template has expression[nodeName={}, expression={}]",
new Object[] { templateElement.getNodeName(), expression });
return true;
}
@SuppressWarnings("unchecked")
final Enumeration<TemplateElement> children = templateElement.children();
while (children.hasMoreElements()) {
final TemplateElement nextElement = children.nextElement();
if (hasExpression(template, expression, nextElement)) {
return true;
}
}
return false;
}
示例9: process
import freemarker.template.Template; //導入依賴的package包/類
/**
* @param templateString
* 模板字符串
* @param args
* 參數
* @return
*/
public static String process(String templateString, Map<String, Object> args) {
try {
if (templateString == null) {
return null;
}
String key = "template_" + templateString.hashCode();
if (stringTemplateLoader.findTemplateSource(key) == null) {
stringTemplateLoader.putTemplate(key, templateString);
}
Template mytpl = configuration.getTemplate(key, chareSet);
return process(mytpl, args);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
示例10: generatorHtml
import freemarker.template.Template; //導入依賴的package包/類
public void generatorHtml(String dir, String htmlName, String templateFile, String attrName, Object attrValue) {
try {
if (StringUtils.isEmpty(htmlName)) htmlName = "index.html";
fileUtil.mkdirs(dir);
File staticFile = fileUtil.createFile(dir + "/" + htmlName);
FileOutputStream outStream = new FileOutputStream(staticFile);
OutputStreamWriter writer = new OutputStreamWriter(outStream, "UTF-8");
BufferedWriter sw = new BufferedWriter(writer);
Map rootMap = new HashMap();
if (!StringUtils.isEmpty(attrName)) rootMap.put(attrName, attrValue);
Template template = configuration.getTemplate(siteConfig.getTheme() + "/" + templateFile);
template.process(rootMap, sw);
sw.flush();
sw.close();
outStream.close();
} catch (IOException | TemplateException e) {
e.printStackTrace();
}
}
示例11: createDroolRule
import freemarker.template.Template; //導入依賴的package包/類
public String createDroolRule(Rule rule) throws TemplateException, IOException {
try {
Template temp = cfg.getTemplate(templateName);
Writer out = new StringWriter();
temp.process(createMap(rule), out);
return out.toString();
} catch (IOException iE) {
logger.error("Problem getting rule template file." + iE.getMessage());
throw iE;
} catch (TemplateException tE) {
logger.error("Problem writing Drool rule." + tE.getMessage());
throw tE;
} catch (Exception e) {
logger.error("Problem creating rule: " + e.getMessage());
throw e;
}
}
示例12: getRenderedContent
import freemarker.template.Template; //導入依賴的package包/類
public String getRenderedContent(final DetailPageCollection detailPageCollection, final Template template)
throws CluecumberPluginException {
ReportDetails reportDetails = new ReportDetails();
addChartJsonToReportDetails(detailPageCollection.getElement(), reportDetails);
addCurrentDateToReportDetails(reportDetails);
detailPageCollection.setReportDetails(reportDetails);
Writer stringWriter = new StringWriter();
try {
template.process(detailPageCollection, stringWriter);
} catch (TemplateException | IOException e) {
throw new CluecumberPluginException(e.getMessage());
}
return stringWriter.toString();
}
示例13: process
import freemarker.template.Template; //導入依賴的package包/類
/**
* 執行模板渲染
* @param temp 模板文件或者字符串
* @param data 數據
* @param writer 輸出方式
*/
public static void process(String temp, Map<String,Object> data, Writer writer){
try {
config.setDirectoryForTemplateLoading(new File(System .getProperty("user.dir")+File.separator+CoreConfig.getTemplatePath()));
Template template = config.getTemplate(temp,CoreConfig.getEncoding());
template.process(data, writer);
} catch (Exception e) {
e.printStackTrace();
} finally {
// 關閉write
try {
if (writer != null) {
writer.flush();
writer.close();
}
} catch (Exception ex) {
}
}
}
示例14: createRssPostContent
import freemarker.template.Template; //導入依賴的package包/類
@Override
public String createRssPostContent(Post post) {
String html = null;
Map<String, Object> model = new Hashtable<>();
model.put("post", post);
model.put("baseurl", applicationSettings.getBaseUrl());
try {
Template template = fm.getTemplate("posts/rss_post.ftl");
html = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
} catch (IOException | TemplateException e) {
logger.error("Problem merging post template : " + e.getMessage());
}
return html;
}
示例15: createPostHtml
import freemarker.template.Template; //導入依賴的package包/類
@Override
public String createPostHtml(Post post, String templateName) {
String html = null;
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMM dd, yyyy");
String postCreated = post.getPostDate().format(formatter);
Map<String, Object> model = new Hashtable<>();
model.put("post", post);
model.put("postCreated", postCreated);
model.put("shareSiteName",
StringUtils.deleteWhitespace(applicationSettings.getSiteName()));
model.put("shareUrl",
String.format("%s/post/%s", applicationSettings.getBaseUrl(), post.getPostName()));
String displayType = templateName == null ? post.getDisplayType().name().toLowerCase() : templateName;
String ftl = String.format("posts/%s.ftl", displayType);
try {
Template template = fm.getTemplate(ftl);
html = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
} catch (IOException | TemplateException e) {
logger.error("Problem merging post template : " + e.getMessage());
}
return html;
}