use of freemarker.template.DefaultObjectWrapper in project stanbol by apache.
the class ViewableWriter method renderPojo.
/**
* Old school classical freemarker rendering, no LD here
*/
public void renderPojo(Object pojo, final String templatePath, Writer out) {
Configuration freemarker = new Configuration();
freemarker.setDefaultEncoding("utf-8");
freemarker.setOutputEncoding("utf-8");
freemarker.setLocalizedLookup(false);
freemarker.setObjectWrapper(new DefaultObjectWrapper());
freemarker.setTemplateLoader(templateLoader);
try {
//should root be a map instead?
freemarker.getTemplate(templatePath).process(pojo, out);
out.flush();
} catch (IOException e) {
throw new RuntimeException("IOException while processing Template '" + templatePath + "' with Object '" + pojo + "' (class: " + pojo != null ? pojo.getClass().getName() : null + ")!", e);
} catch (TemplateException e) {
throw new RuntimeException(e);
}
}
use of freemarker.template.DefaultObjectWrapper in project leopard by tanhaichao.
the class AbstractTemplateView method render.
public void render(HttpServletRequest request, HttpServletResponse response) throws IOException {
RequestHolder.setRequest(request);
Configuration config = new Configuration();
ConfigurationHolder.setConfiguration(config);
// 设置要解析的模板所在的目录,并加载模板文件
// config.setDirectoryForTemplateLoading(templateFile);
config.setTemplateLoader(new ClassTemplateLoader(this.getClass(), folder));
// 设置包装器,并将对象包装为数据模型
config.setObjectWrapper(new DefaultObjectWrapper());
config.setDefaultEncoding("UTF-8");
Map<String, Object> freemarkerVariables = new HashMap<String, Object>();
freemarkerVariables.put("xml_escape", "fmXmlEscape");
List<TemplateVariable> variables = this.getVariables();
if (variables != null) {
for (TemplateVariable variable : variables) {
freemarkerVariables.put(variable.getKey(), variable);
}
}
// freemarkerVariables.put("time", new TimeTemplateMethod());
// freemarkerVariables.put("url", new UrlTemplateMethod());
// freemarkerVariables.put("underline", new UnderlineTemplateMethod());
Properties freemarkerSettings = new Properties();
freemarkerSettings.put("template_update_delay", "1");
freemarkerSettings.put("defaultEncoding", "UTF-8");
try {
config.setSettings(freemarkerSettings);
config.setAllSharedVariables(new SimpleHash(freemarkerVariables, config.getObjectWrapper()));
} catch (TemplateException e) {
throw new IOException(e);
}
// 获取模板,并设置编码方式,这个编码必须要与页面中的编码格式一致
// 否则会出现乱码
Template template = config.getTemplate(templateName + ".ftl", Locale.CHINA, "UTF-8");
template.setCustomAttribute("request", request);
model.put("request", request);
StringWriter writer = new StringWriter();
// Writer out = response.getWriter();
try {
template.process(model, writer);
} catch (TemplateException e) {
throw new IOException(e);
}
output(writer, response);
// Template tmp = (getEncoding() != null ? conf.getTemplate(name,
// locale, getEncoding()) : conf.getTemplate(name, locale));
}
use of freemarker.template.DefaultObjectWrapper in project head by mifos.
the class TallyXMLGenerator method buildFreemarkerConfiguration.
private static Configuration buildFreemarkerConfiguration() {
if (freemarkerConfiguration == null) {
freemarkerConfiguration = new Configuration();
freemarkerConfiguration.setClassForTemplateLoading(TallyXMLGenerator.class, "");
freemarkerConfiguration.setObjectWrapper(new DefaultObjectWrapper());
}
return freemarkerConfiguration;
}
use of freemarker.template.DefaultObjectWrapper in project OpenMEAP by OpenMEAP.
the class AdminServlet method service.
@SuppressWarnings("unchecked")
@Override
public void service(HttpServletRequest request, HttpServletResponse response) {
logger.trace("Entering service()");
try {
DocumentProcessor documentProcessor = null;
logger.debug("Request uri: {}", request.getRequestURI());
logger.debug("Request url: {}", request.getRequestURL());
logger.debug("Query string: {}", request.getQueryString());
if (logger.isDebugEnabled()) {
logger.debug("Parameter map: {}", ParameterMapUtils.toString(request.getParameterMap()));
}
if (request.getParameter("logout") != null) {
logger.trace("Executing logout");
request.getSession().invalidate();
response.sendRedirect(request.getContextPath() + "/interface/");
}
if (request.getParameter("refreshContext") != null && context instanceof AbstractApplicationContext) {
logger.trace("Refreshing context");
((AbstractApplicationContext) context).refresh();
}
// support for clearing the persistence context
if (request.getParameter("clearPersistenceContext") != null && context instanceof AbstractApplicationContext) {
logger.trace("Clearing the persistence context");
ModelServiceImpl ms = (ModelServiceImpl) ((AbstractApplicationContext) context).getBean("modelService");
ms.clearPersistenceContext();
}
// default to the mainOptionPage, unless otherwise specified
String pageBean = null;
if (request.getParameter(FormConstants.PAGE_BEAN) != null)
pageBean = request.getParameter(FormConstants.PAGE_BEAN);
else
pageBean = FormConstants.PAGE_BEAN_MAIN;
logger.debug("Using page bean: {}", pageBean);
documentProcessor = (DocumentProcessor) context.getBean(pageBean);
ModelManager mgr = getModelManager();
Map<Object, Object> map = new HashMap<Object, Object>();
// TODO: I'm not really happy with this hacky work-around for the login form not being in actual request scope
if (documentProcessor.getProcessesFormData()) {
GlobalSettings settings = mgr.getGlobalSettings();
map = ServletUtils.cloneParameterMap(settings.getMaxFileUploadSize(), settings.getTemporaryStoragePath(), request);
map.put("userPrincipalName", new String[] { request.getUserPrincipal().getName() });
AuthorizerImpl authorizer = (AuthorizerImpl) context.getBean("authorizer");
authorizer.setRequest(request);
}
response.setContentType(FormConstants.CONT_TYPE_HTML);
Map<Object, Object> defaultTemplateVars = new HashMap<Object, Object>();
defaultTemplateVars.put("request", new BeanModel(request, new DefaultObjectWrapper()));
documentProcessor.setTemplateVariables(defaultTemplateVars);
documentProcessor.handleProcessAndRender(map, response.getWriter());
response.getWriter().flush();
response.getWriter().close();
} catch (IOException te) {
throw new RuntimeException(te);
}
logger.trace("Leaving service()");
}
use of freemarker.template.DefaultObjectWrapper in project OpenMEAP by OpenMEAP.
the class FreeMarkerTestUtils method createConfiguration.
public static Configuration createConfiguration(Class<?> clazz, String path) {
Configuration cfg = new Configuration();
cfg.setTemplateLoader(new ClassTemplateLoader(clazz, path));
cfg.setObjectWrapper(new DefaultObjectWrapper());
return cfg;
}
Aggregations