Package freemarker.template

Examples of freemarker.template.Configuration


   * 初始化Freemarker的配置
   */
  private void initFreemarker() {
    if (cfg == null) {
      try {
        cfg = new Configuration();
        cfg.setDirectoryForTemplateLoading(new File(this.session
            .getServletContext().getRealPath("/")));
        cfg.setObjectWrapper(new DefaultObjectWrapper());
      } catch (IOException e) {
        throw new RuntimeException("初始化Freemarker引擎失败", e);
View Full Code Here


        throws Exception
    {
        StringWriter writer = new StringWriter();

        // - create the template
        Template template = new Template("strTemplate", new StringReader("${test1}${test2}"), new Configuration());

        HashMap templateObjects = new HashMap();

        templateObjects.put(
            "test1",
View Full Code Here

        ExceptionUtils.checkEmpty("templateFile", templateFile);
        ExceptionUtils.checkNull("output", output);

        if (this.configuration == null)
        {
            this.configuration = new Configuration();

            // tell FreeMarker it should use the ContextClassLoader when
            // searching for templates
            this.configuration.setTemplateLoader(new ContextResourceLoader());
View Full Code Here

    public String getEvaluatedExpression(final String expression, Map templateObjects)
    {
        try
        {
            // - create the template
            final Template template = new Template(STRING_TEMPLATE, new StringReader(expression), new Configuration());

            if (templateObjects == null)
            {
                templateObjects = new HashMap();
            }
View Full Code Here

 
  public void test_FreemarkerHelper() {
    Map model = new HashMap();
    model.put("package", "com/company");
    model.put("module", "app");
    assertEquals("src/com/company",FreemarkerHelper.processTemplateString("src/${package}", model, new Configuration()));
    assertEquals("src/com/company/app",FreemarkerHelper.processTemplateString("src/${package}/${module}", model, new Configuration()));
   
    model.put("module", "");
    assertEquals("src/com/company//username.java",FreemarkerHelper.processTemplateString("src/${package}/${module}/username.java", model, new Configuration()));
  }
View Full Code Here

     * to custom-configure the configuration object in a subclass.
     * The default implementation returns a new {@link Configuration}
     * instance.
     */
    protected Configuration createConfiguration() {
        return new Configuration();
    }
View Full Code Here

    public void renderView(FreeMarkerView view, ViewContext viewContext) throws Exception{
        if (view == null){
            return;
        }
        ServletContext servletContext = viewContext.getServletContext();
        Configuration cfg = (Configuration)servletContext.getAttribute(FreeMarkerViewRenderer.FREEMARKER_CONFIG);
        if (cfg == null){
            cfg = new Configuration();
            cfg.setDefaultEncoding("UTF-8");
            initConfiguration(cfg, viewContext);
            servletContext.setAttribute(FreeMarkerViewRenderer.FREEMARKER_CONFIG, cfg);
        }
       
        HttpServletRequest request = viewContext.getRequest();
        HttpServletResponse response = viewContext.getResponse();
        String controllerPath = viewContext.getControllerPath();
       
        Enumeration<?> attrNames = request.getAttributeNames();
        while(attrNames.hasMoreElements()){
            String attr = (String)attrNames.nextElement();
            Object value = request.getAttribute(attr);
            view.setAttribute(attr, value);
        }
       
        String path = view.getPath();
        if (path == null){
            path = controllerPath.toLowerCase() + "/" + viewContext.getActionName() + ".ftl";
        }
        else{
            if (path.indexOf("/") == -1){
                path = controllerPath.toLowerCase() + "/" + path;
            }
            else if (!path.startsWith("/")){
                path = "/" + path;
            }
            if (!path.endsWith(".ftl")){
                path += ".ftl";
            }
        }
       
        if (view.getContentType() != null){
            response.setContentType(view.getContentType());
        }
       
        Template t = cfg.getTemplate(path, request.getLocale());
        t.process(view.getAttributes(), response.getWriter());
    }
View Full Code Here

            // good
        }
    }
   
    public void testSettingParser() throws TemplateException {
        Configuration cfg = new Configuration();
       
        cfg.setSetting("new_builtin_class_resolver",
                "trusted_templates: foo.ftl, \"lib/*\"");
        TemplateClassResolver res = cfg.getNewBuiltinClassResolver();
        assertEquals(String.class, res.resolve("java.lang.String", null,
                Template.getPlainTextTemplate("foo.ftl", "", cfg)));
        assertEquals(String.class, res.resolve("java.lang.String", null,
                Template.getPlainTextTemplate("lib/bar.ftl", "", cfg)));
        try {
            res.resolve("java.lang.String", null,
                    Template.getPlainTextTemplate("bar.ftl", "", cfg));
            fail();
        } catch (TemplateException e) {
            // good
        }

        cfg.setSetting("new_builtin_class_resolver",
                "allowed_classes: java.lang.String, java.lang.Integer");
        res = cfg.getNewBuiltinClassResolver();
        assertEquals(String.class, res.resolve("java.lang.String", null,
                Template.getPlainTextTemplate("foo.ftl", "", cfg)));
        assertEquals(Integer.class, res.resolve("java.lang.Integer", null,
                Template.getPlainTextTemplate("foo.ftl", "", cfg)));
        try {
            res.resolve("java.lang.Long", null,
                    Template.getPlainTextTemplate("foo.ftl", "", cfg));
            fail();
        } catch (TemplateException e) {
            // good
        }

        cfg.setSetting("new_builtin_class_resolver",
                "trusted_templates: foo.ftl, 'lib/*', " +
                "allowed_classes: 'java.lang.String', java.lang.Integer");
        res = cfg.getNewBuiltinClassResolver();
        assertEquals(String.class, res.resolve("java.lang.String", null,
                Template.getPlainTextTemplate("x.ftl", "", cfg)));
        assertEquals(Integer.class, res.resolve("java.lang.Integer", null,
                Template.getPlainTextTemplate("x.ftl", "", cfg)));
        try {
            res.resolve("java.lang.Long", null,
                    Template.getPlainTextTemplate("x.ftl", "", cfg));
            fail();
        } catch (TemplateException e) {
            // good
        }
        assertEquals(Long.class, res.resolve("java.lang.Long", null,
                Template.getPlainTextTemplate("foo.ftl", "", cfg)));
        assertEquals(Long.class, res.resolve("java.lang.Long", null,
                Template.getPlainTextTemplate("lib/bar.ftl", "", cfg)));
        try {
            res.resolve("java.lang.Long", null,
                    Template.getPlainTextTemplate("x.ftl", "", cfg));
            fail();
        } catch (TemplateException e) {
            // good
        }
       
        try {
            cfg.setSetting("new_builtin_class_resolver", "wrong: foo");
            fail();
        } catch (TemplateException e) {
            // good
        }
       
        cfg.setSetting("new_builtin_class_resolver",
                "\"allowed_classes\"  :  java.lang.String  ,  " +
                "'trusted_templates' :\"lib:*\"");
        res = cfg.getNewBuiltinClassResolver();
        assertEquals(String.class, res.resolve("java.lang.String", null,
                Template.getPlainTextTemplate("x.ftl", "", cfg)));
        try {
            res.resolve("java.lang.Long", null,
                    Template.getPlainTextTemplate("x.ftl", "", cfg));
View Full Code Here

{
    public static void main(String[] args)
    throws
        Exception
    {
        Configuration config = new Configuration();
        // config.setDebugMode(false);
        config.setClassicCompatible(false);
        config.setClassForTemplateLoading(PerformanceTest.class, "/freemarker/testcase");
        Template template = config.getTemplate("PerformanceTest.fm");
        boolean toFile = args.length > 0 && args[0].equals("file");
        File f = File.createTempFile("fmPerfTest", ".txt");
        f.deleteOnExit();
        OutputStream nullStream = new NullStream();
        SimpleHash h = new SimpleHash();
View Full Code Here

          }
        } catch (IOException e) {
          throw new RuntimeException(e);
        }
      }
      config = new Configuration();
      config.setCacheStorage(new MruCacheStorage(32, 32));
      MultiTemplateLoader loader = new MultiTemplateLoader(loaders.toArray(new TemplateLoader[loaders.size()]));
      config.setTemplateLoader(loader);
      config.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
    }
View Full Code Here

TOP

Related Classes of freemarker.template.Configuration

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.