本文整理匯總了Java中freemarker.template.Template.setOutputEncoding方法的典型用法代碼示例。如果您正苦於以下問題:Java Template.setOutputEncoding方法的具體用法?Java Template.setOutputEncoding怎麽用?Java Template.setOutputEncoding使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類freemarker.template.Template
的用法示例。
在下文中一共展示了Template.setOutputEncoding方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: 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();
}
示例2: 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);
}
}
}