Search in sources :

Example 6 with DefaultObjectWrapper

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);
    }
}
Also used : Configuration(freemarker.template.Configuration) TemplateException(freemarker.template.TemplateException) DefaultObjectWrapper(freemarker.template.DefaultObjectWrapper) IOException(java.io.IOException)

Example 7 with DefaultObjectWrapper

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));
}
Also used : Configuration(freemarker.template.Configuration) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TemplateException(freemarker.template.TemplateException) ClassTemplateLoader(freemarker.cache.ClassTemplateLoader) DefaultObjectWrapper(freemarker.template.DefaultObjectWrapper) IOException(java.io.IOException) Properties(java.util.Properties) Template(freemarker.template.Template) StringWriter(java.io.StringWriter) SimpleHash(freemarker.template.SimpleHash) TemplateVariable(io.leopard.web.freemarker.TemplateVariable)

Example 8 with DefaultObjectWrapper

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;
}
Also used : Configuration(freemarker.template.Configuration) DefaultObjectWrapper(freemarker.template.DefaultObjectWrapper)

Example 9 with DefaultObjectWrapper

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()");
}
Also used : BeanModel(freemarker.ext.beans.BeanModel) DocumentProcessor(com.openmeap.web.DocumentProcessor) HashMap(java.util.HashMap) AuthorizerImpl(com.openmeap.AuthorizerImpl) DefaultObjectWrapper(freemarker.template.DefaultObjectWrapper) GlobalSettings(com.openmeap.model.dto.GlobalSettings) IOException(java.io.IOException) ModelManager(com.openmeap.model.ModelManager) ModelServiceImpl(com.openmeap.model.ModelServiceImpl) AbstractApplicationContext(org.springframework.context.support.AbstractApplicationContext)

Example 10 with DefaultObjectWrapper

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;
}
Also used : Configuration(freemarker.template.Configuration) ClassTemplateLoader(freemarker.cache.ClassTemplateLoader) DefaultObjectWrapper(freemarker.template.DefaultObjectWrapper)

Aggregations

DefaultObjectWrapper (freemarker.template.DefaultObjectWrapper)12 Configuration (freemarker.template.Configuration)10 Template (freemarker.template.Template)6 IOException (java.io.IOException)4 Environment (freemarker.core.Environment)3 TemplateException (freemarker.template.TemplateException)3 HashMap (java.util.HashMap)3 ClassTemplateLoader (freemarker.cache.ClassTemplateLoader)2 File (java.io.File)2 StringWriter (java.io.StringWriter)2 Writer (java.io.Writer)2 VCDestination (cbit.vcell.message.VCDestination)1 VCMessage (cbit.vcell.message.VCMessage)1 VCMessagingDelegate (cbit.vcell.message.VCMessagingDelegate)1 VCMessagingService (cbit.vcell.message.VCMessagingService)1 VCRpcRequest (cbit.vcell.message.VCRpcRequest)1 AdminDBTopLevel (cbit.vcell.modeldb.AdminDBTopLevel)1 DatabaseServerImpl (cbit.vcell.modeldb.DatabaseServerImpl)1 LocalAdminDbServer (cbit.vcell.modeldb.LocalAdminDbServer)1 AuthorizerImpl (com.openmeap.AuthorizerImpl)1