freemarker

FileNotFoundException when loading freemarker template in java

倖福魔咒の 提交于 2019-11-30 02:45:45
问题 I get a file not found exception while loading a freemarker template even though the template is actually present in the path. Update: This is running as a webservice. It will return an xml to the client based on a search query. The template loads successfully when i call it from another java program(from static main). But the when the client requests for the xml, FileNotFoundException occurs. OS: Windows 7 Absolute path of file: C:/Users/Jay/workspace/WebService/templates/ Here is my code:

How can I introspect a freemarker template to find out what variables it uses?

♀尐吖头ヾ 提交于 2019-11-30 01:21:48
问题 I'm not at all sure that this is even a solvable problem, but supposing that I have a freemarker template, I'd like to be able to ask the template what variables it uses. For my purposes, we can assume that the freemarker template is very simple - just "root level" entries (the model for such a template could be a simple Map). In other words, I don't need to handle templates that call for nested structures, etc. 回答1: I had the same task to get the list of variables from template on java side

Freemarker 常用内置函数和用法

人盡茶涼 提交于 2019-11-30 01:00:36
2010-07-06 10:46 一、 Sequence的内置函数 1. sequence?first 返回sequence的第一个值。 2. sequence?last 返回sequence的最后一个值。 3. sequence?reverse 将sequence的现有顺序反转,即倒序排序 4. sequence?size 返回sequence的大小 5. sequence?sort 将sequence中的对象转化为字符串后顺序排序 6. sequence?sort_by(value) 按sequence中对象的属性value进行排序 注意:Sequence不能为null。 二、 Hash的内置函数 1. hash?keys 返回hash里的所有key,返回结果为sequence 2. hash?values 返回hash里的所有value,返回结果为sequence 例如: <#assign user={“name”:“hailang”, “sex”:“man”}> <#assign keys=user?keys> <#list keys as key> ${key}=${user[key]} </#list> 三、 操作字符串函数 1. substring(start,end)从一个字符串中截取子串 start:截取子串开始的索引,start必须大于等于0,小于等于end

Freemarker 常用内置函数和用法

╄→尐↘猪︶ㄣ 提交于 2019-11-30 01:00:35
2010-07-06 10:46 一、 Sequence的内置函数 1. sequence?first 返回sequence的第一个值。 2. sequence?last 返回sequence的最后一个值。 3. sequence?reverse 将sequence的现有顺序反转,即倒序排序 4. sequence?size 返回sequence的大小 5. sequence?sort 将sequence中的对象转化为字符串后顺序排序 6. sequence?sort_by(value) 按sequence中对象的属性value进行排序 注意:Sequence不能为null。 二、 Hash的内置函数 1. hash?keys 返回hash里的所有key,返回结果为sequence 2. hash?values 返回hash里的所有value,返回结果为sequence 例如: <#assign user={“name”:“hailang”, “sex”:“man”}> <#assign keys=user?keys> <#list keys as key> ${key}=${user[key]} </#list> 三、 操作字符串函数 1. substring(start,end)从一个字符串中截取子串 start:截取子串开始的索引,start必须大于等于0,小于等于end

SpringBoot整合freemarker

孤人 提交于 2019-11-29 23:26:53
增加freemarker依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> 增加freemarker相关配置 spring.freemarker.allow-request-override=false spring.freemarker.allow-session-override=false spring.freemarker.cache=true spring.freemarker.charset=UTF-8 spring.freemarker.check-template-location=true spring.freemarker.content-type=text/html spring.freemarker.enabled=true spring.freemarker.expose-request-attributes=false spring.freemarker.expose-session-attributes=false spring.freemarker.expose-spring-macro-helpers=true spring

freemarker

时光怂恿深爱的人放手 提交于 2019-11-29 21:24:14
FreeMarker是一款模板引擎: 即一种基于模板和要改变的数据, 并用来生成输出文本(HTML网页、电子邮件、配置文件、源代码等)的通用工具。 它不是面向最终用户的,而是一个Java类库,是一款程序员可以嵌入他们所开发产品的组件。 springboot yml文件配置 freemarker: allow-request-override: false cache: false ##是否生成缓存,生成环境建议开启(默认为true) check-template-location: true charset: UTF-8 ##编码 content-type: text/html; charset=utf-8 ##content-type类型(默认为test/html) expose-request-attributes: false ## 设定所有request的属性在merge到模板的时候,是否要都添加到model中(默认为false) expose-session-attributes: false ##设定所有HttpSession的属性在merge到模板的时候,是否要都添加到model中.(默认为false) expose-context-attributes: request ##RequestContext属性的名称(默认为-) expose-spring-macro

Handling null values in Freemarker

五迷三道 提交于 2019-11-29 21:17:30
How to handle null values in Freemarker? I get some exceptions in the template when null values are present in data. You can use the ?? test operator: This checks if the attribute of the object is not null: <#if object.attribute??></#if> This checks if object or attribute is not null: <#if (object.attribute)??></#if> Source: FreeMarker Manual Starting from freemarker 2.3.7, you can use this syntax : ${(object.attribute)!} or, if you want display a default text when the attribute is null : ${(object.attribute)!"default text"} Senthil Kumar Sekar I think it works the other way <#if object

superword中的模板抽取实践

倖福魔咒の 提交于 2019-11-29 20:48:16
superword这个项目,全使用JAVA8新特性: https://github.com/ysc/superword ,一开始只是我的一个英语单词分析工具,用于生成HTML片段然后发到博客中,后来功能越来越强于是我就做成一个项目了,再后来有人跟我说自己不是计算机专业的不会用这个软件,于是我就改造成了一个WEB项目,这个项目现在有点需要改进的地方,就是 把JAVA代码生成HTML的这个逻辑改成使用FREEMARKER的方式 。 我们首先来看在org.apdplat.superword.system.AntiRobotFilter类中的原来的 JAVA代码生成HTML的逻辑: StringBuilder html = new StringBuilder(); html.append("<h1>The meaning of red color font is your answer, but the right answer is the meaning of blue color font for the word <font color=\"red\">") .append(quizItem.getWord().getWord()) .append(":</font></h1>"); html.append("<h2><ul>"); for(String option :

Load FreeMarker templates from database

南笙酒味 提交于 2019-11-29 20:43:29
I would like to store my FreeMarker templates in a database table that looks something like: template_name | template_content --------------------------------- hello |Hello ${user} goodbye |So long ${user} When a request is received for a template with a particular name, this should cause a query to be executed, which loads the relevant template content. This template content, together with the data model (the value of the 'user' variable in the examples above), should then be passed to FreeMarker. However, the FreeMarker API seems to assume that each template name corresponds to a file of the

springboot系列十四 web模板 freemarker thymeleaf

扶醉桌前 提交于 2019-11-29 20:39:31
Freemarker https://freemarker.apache.org/ http://freemarker.foofun.cn/ springboot中freemarker的默认配置 package org.springframework.boot.autoconfigure.freemarker; import java.util.HashMap; import java.util.Map; import org.springframework.boot.autoconfigure.template.AbstractTemplateViewResolverProperties; import org.springframework.boot.context.properties.ConfigurationProperties; @ConfigurationProperties( prefix = "spring.freemarker"//配置文件默认前缀 ) public class FreeMarkerProperties extends AbstractTemplateViewResolverProperties { public static final String DEFAULT_TEMPLATE_LOADER_PATH = "classpath: