freemarker

Setting freemarker template from classpath

萝らか妹 提交于 2019-11-27 01:34:39
问题 I have a web application that I need to manually obtain a Freemarker template - the template is obtained via a class in a library project, but the actual tpl file is contained in the web application classpath. So, there are 2 projects, one 'taac-backend-api' and another 'taac-web'; taac-backend-api has the code to grab the template, and process it, but taac-web is where the template is stores (specifically in: WEB-INF/classes/email/vendor.tpl) - I have tried everything from using springs

FreeMarker 语法 null 的处理

五迷三道 提交于 2019-11-27 00:48:08
一、java 代码 @Test public void testFreeMarker() throws Exception { //1、创建一个模板文件 //2、创建一个Configuration对象 Configuration configuration = new Configuration(); //3、设置模板文件保存的目录 configuration.setDirectoryForTemplateLoading(new File("E:/workspaces/fw-item-web/src/main/webapp/WEB-INF/ftl")); //4、模板文件的编码格式,一般就是utf-8 configuration.setDefaultEncoding("utf-8"); //5、加载一个模板文件,创建一个模板对象。 Template template = configuration.getTemplate("testNull.ftl"); //6、创建一个数据集。可以是pojo也可以是map。推荐使用map Map data = new HashMap<>(); //添加 null 值 data.put("val", null); //7、创建一个Writer对象,指定输出文件的路径及文件名。 Writer out = new FileWriter(new File

freemarker中对null值问题的处理

老子叫甜甜 提交于 2019-11-27 00:47:10
1. freemarker不支持null。 如果值为null会报错。 2.当值为null的处理 1)过滤不显示 Hello ${name!} 在属性后面加感叹号即可过滤null和空字符串 if和”??“ <#if age??> 无年龄值 <#/if> $和! ${age!'0'} 如果age为null,默认给'0' 2)设置默认值 如${student1.user!"null"} 3)判断是否存在值 $stduent.user?if_exists} 这样显示就没有问题了 if判断null处理 ========================================================================= freemarker对null的处理 无非就这几种 1 提供默认值 <#if mouse?> Mouse found <#else> 也可以直接${mouse?if_exists} <#if user.age??> //TO DO </#if> 2.忽略null值 假设前提:userName为null ${userName} error ${userName!} 空白 ${userName!'tivon'} tivon 假设前提:user.name为null ${user.name},异常 ${(user.name)!},显示空白 ${user

FreeMarker 对null值的处理技巧

纵饮孤独 提交于 2019-11-27 00:46:57
以下引用官方描述: ? The FreeMarker template language doesn't know the Java language null at all. It doesn't have null keyword, and it can't test if something is null or not.    1.判断是否存在,通过exists关键字或者"??"运算符。都将返回一个布尔值 user.name?exists user.name?? ? <# if user.name?exists> //TO DO </# if > <# if user.age??> //TO DO </# if >    2.忽略null值 假设前提:user.name为null ${user.name},异常 ${user.name!},显示空白 ${user.name!'vakin'},若user.name不为空则显示本身的值,否则显示vakin ${user.name?default('vakin')},同上 ${user.name???string(user.name,'vakin')},同上 以下引用官方描述: ? The FreeMarker template language doesn't know the Java language null at all

How to check if a variable exists in a FreeMarker template?

╄→尐↘猪︶ㄣ 提交于 2019-11-26 23:56:19
问题 I have a Freemarker template which contains a bunch of placeholders for which values are supplied when the template is processed. I want to conditionally include part of the template if the userName variable is supplied, something like: [#if_exists userName] Hi ${userName}, How are you? [/#if_exists] However, the FreeMarker manual seems to indicate that if_exists is deprecated, but I can't find another way to achieve this. Of course, I could simple providing an additional boolean variable

A url resource that is a dot (%2E)

喜夏-厌秋 提交于 2019-11-26 22:50:37
I have a resource that is a . This means my url looks like this: http://myapp/index/ . And i need to add query parameters so that it looks like this: http://myapp/index/.?type=xml I use Freemarker for the presentation of my resources and made a percent-encoding hack for this case: <#if key?matches("\\.")> <li><a href="${contextPath}/index/%2E">${key}</a></li> </#if> This works fine for Firefox. But all other Browsers like IE, Safari, Chrom, Opera just ignore my url encoded dot ( http://myapp/index/%2E ). Any suggestions? It's actually not really clearly stated in the standard ( RFC 3986 )

freemarker快速上手+空值+三元运算的多种处理方法

大憨熊 提交于 2019-11-26 21:49:41
前言: 为什么要用模板?有了JSTL,还需要freemarker吗? 模板技术与容器无关,同样可以应用于非Web应用程序环境。ftl文件改动之后是不需要编译的,这点不同于Jsp 。JSTL只能用在jsp中,修改了jsp,需要重新编译,从而使用模板更有效率。 一、上手实例 1. 在WEB-INF/lib中放置freemarker.jar,新建一个Servlet,Hello.java Java代码 package com.fbysss.test.servlet; import java.io.IOException; import java.io.Writer; import java.util.HashMap; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import freemarker.core.Environment; import freemarker.template.Configuration; import freemarker.template.Template; import freemarker

Freemarker iterating over hashmap keys

人盡茶涼 提交于 2019-11-26 20:10:40
Freemarker has two collection datatypes, lists and hashmaps Is there a way to iterate over hashmap keys just as we do with lists? So if I have a var with data lets say: user : { name : "user" email : "looser@everything.com" homepage : "http://nosuchpage.org" } I would like to print all the user's properties with their value. This is invalid, but the goal is clear: <#list user.props() as prop> ${prop} = ${user.get(prop)} </#list> skaffman Edit: Don't use this solution with FreeMarker 2.3.25 and up, especially not .get(prop) . See other answers. You use the built-in keys function, e.g. this

How to handle exceptions thrown while rendering a view in Spring MVC?

北慕城南 提交于 2019-11-26 19:43:32
问题 I have a Spring MVC application which uses FreeMarker as View technology (But maybe the view technology doesn't really matter for my question). I need to intercept all exceptions which may get thrown during a request. I have implemented a HandlerExceptionResolver but this resolver is only executed when the exception occurs within a controller. But when a controller returns a ModelAndView and the exception occurs while rendering the view (Because a variable was not found or something like this

IntelliJ not recognizing a particular file correctly, instead its stuck as a text file

别说谁变了你拦得住时间么 提交于 2019-11-26 18:55:04
问题 There is a freemarker file (ftl) in my IntelliJ project that is incorrectly recognized as a text file. There are many of the same type that are correct. I am aware of the "Mark as text" option. This may be the original reason this file was marked as text but I am not provided with a "mark as ftl file" option to turn it back, if indeed this is the problem. If I mark other ftl files as txt, I am able to turn them back into ftl files as expected. 回答1: Please ensure that this file (or a pattern