freemarker

Simulate null parameters in Freemarker macros

那年仲夏 提交于 2019-11-30 12:24:15
I'm building a site using Freemarker and have started heavily using macros. I know in Freemarker 2.3 that passing a null value into a macro as a parameter is equivalent to not passing a parameter at all so I've created a global variable called "null" to simulate null checking in my macros: <#assign null="NUL" /> Now in my macros I can do this: <#maco doSomething param1=null> <#if param1 != null> <div>WIN!</div> </#if> </#macro> The problem comes if I want to pass a parameter that isn't a scalar. For instance, passing a List (which in Freemarker is a SimpleSequence) to a macro and checking

freemarker 的xml模板

蓝咒 提交于 2019-11-30 12:04:58
public String xmlFormwork(String xmlPath, String xmlName, String format, Map<String, Object> dataMap) { String xmlString = ""; // 得FreeMarker配置对象 // 创建Configuration对象 Configuration cfg = new Configuration(new Version("2.3.28")); // 设置模板编码格式 cfg.setEncoding(Locale.getDefault(), format); // 得FreeMarker的关键对象---------模板 // 创建Template对象 Template template = null; try { // 设置FreeMarker的模版文件位置 cfg.setDirectoryForTemplateLoading(new File(xmlPath)); cfg.setDefaultEncoding("UTF-8"); template = cfg.getTemplate(xmlName); } catch (IOException e1) { e1.printStackTrace(); } // String path =

How do I call java methods on an object from a FreeMarker template?

走远了吗. 提交于 2019-11-30 10:43:33
Is it possible to call a method that takes parameters from a Freemarker template? I have an object model that I'm trying to render with Freemarker into a web page. One of the objects has a method to get a sublist of it's contents - taking a parameter that is used to filter the list: public List getunits(final String type); I know in JSP you can't do this directly, but you can write custom functions that will allow you to achieve the result you want. How do you solve this in Freemarker? Is it the same with writing custom functions? Or is there some way of actually calling this kind of function?

How to get the request context in a freemaker template in spring

。_饼干妹妹 提交于 2019-11-30 08:09:35
How to get the request context path in freemarker template when using with spring ? My view resolver is like this <bean id="freeMarkerViewResolver" class="learn.common.web.view.FreemarkerViewResolver"> <property name="order" value="1" /> <property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView" /> <property name="suffix" value=".ftl" /> <property name="cache" value="false" /> </bean> My view resolver learn.common.web.view.FreemarkerViewResolver extends org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver In your view resolver you can

Will not closing a stringwriter cause a leak?

巧了我就是萌 提交于 2019-11-30 07:18:55
问题 I realize that in java the GC will eventually cleanup objects, but I'm asking if it is bad practice to not close your string writer, currently I am doing this: private static String processTemplate(final Template template, final Map root) { StringWriter writer = new StringWriter(); try { template.process(root, writer); } catch (TemplateException e) { logger.error(e.getMessage()); } catch (IOException e) { logger.error(e.getMessage()); } finally { } return writer.toString(); } Should I be

SpringMVC-Freemarker异常配置

心已入冬 提交于 2019-11-30 06:26:03
1、spring mvc的异常配置 spring mvc 提供了SimpleMappingExceptionResolver来处理异常,这里的只是由web 请求,经由controller引发的异常,无法处理freemarker的异常 2、freemarker自定义异常 /** * freemarker页面上的异常控制 * 在webmvc-config.xml里面的freemarkerSettings里头配置 * @author scipio * @created 2014-02-01 */ public class FreemarkerExceptionHandler implements TemplateExceptionHandler { private static final Logger log = LoggerFactory .getLogger(FreemarkerExceptionHandler.class); public void handleTemplateException(TemplateException te, Environment env, Writer out) throws TemplateException { log.warn("[Freemarker Error: " + te.getMessage() + "]"); throw new

accept-charset=“UTF-8” parameter doesnt do anything, when used in form

北城余情 提交于 2019-11-30 03:32:38
问题 I am using accept-charset="utf-8" attribute in form and found that the when do a form post with non-ascii, the headers have different accept charset option in the request header. Is there anything i am missing ? My form looks like this <form method="post" action="controller" accept-charset="UTF-8"> ..input text box .. submit button </form> Thanks in advance 回答1: The question, as asked, is self-contradictory: the heading says that the accept-charset parameter does not do anything, whereas the

Freemarker网页静态化

江枫思渺然 提交于 2019-11-30 03:18:41
什么是网页静态化?    动态页面静态化是通过动态网站静态化将动态网页以静态的形式进行展现。 为什么要用网页静态化 网页静态化技术和缓存技术的共同点都是为了减轻数据库的访问压力。 网页静态化有利于搜索引擎收录。 网页静态化有利于网站的稳定性。 网页静态化有利于提高速度。  Freemarker是什么?    FreeMarker是一个用Java语言编写的模板引擎,它基于模板来生成文本输出。   FreeMarker与Web容器无关,即在Web运行时,它并不知道Servlet或HTTP。   它不仅可以用作表现层的实现技术,而且还可以用于生成XML,JSP或Java 等。    案例实现 Test.html <html> <head> <meta charset="utf-8"> <title>Freemarker入门小DEMO </title> </head> <body> 小李飞刀 欢迎来到神奇的博客网站:http://www.javaxl.com! <h3>assigne指令</h3> 联系人:周先生 电话:13301231212 地址:北京市昌平区王府街 <h3>if指令</h3> 你已通过实名认证 <h3>list指令</h3> ----商品价格表----<br> 1 商品名称: 苹果 价格:5.8<br> 2 商品名称: 香蕉 价格:2.5<br> 3 商品名称: 橘子

Getting template text from FreeMarker in Spring app

烂漫一生 提交于 2019-11-30 03:16:24
In my Spring app, I'd like to use FreeMarker to generate the text of emails that will be sent by my application. The generated text will never be returned to the view so I don't need to configure a FreeMarker view resolver. The documentation seems to indicate that I should configure a FreeMarkerConfigurationFactoryBean like this <bean id="freemarkerConfiguration" class="org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean"> <property name="templateLoaderPath" value="/WEB-INF/freemarker/"/> </bean> Once I have this bean configured how do I actually get the text that is generated

Intellij Idea 使用日常记录

假装没事ソ 提交于 2019-11-30 03:15:45
作者使用的Idea版本: 1.去除Mybatis Mapper Xml文件中Sql语句块绿色背景 问题描述:idea中使用mybatis开发,mapper的xml中SQL语句快背景是绿色的,看起来很不舒服,如下图所示: 解决办法:按照File—>Settings—>Editor—Color Scheme—>general打开xml背景色设置窗口,点开Code项目,选择第三个Injected Language Fragment ,然后去掉Background的颜色的钩 2.关闭单词拼写检查 问题描述:在idea中我们写的单词或者一些我们自定义的缩写词,idea默认开启了拼写检查,idea不认识就会报出一个小提醒(下划绿色波浪线),如下图所示: 解决办法:按照File—>Settings—> Editor—> Inspections 打开设置窗口,在搜索框输入Spelling,然后取消typo选项的勾应用保存即可 3.解决Freemarker标签在Html页面不解析的问题 问题描述:在springboot项目中引入Freemarker,然后设置Freemarker解析.html,但是在html页面中使用Freemarker的标签不能被识别,但是能正常使用,问题如下图: 解决办法:按照File—>Settings—> Editor—> File Type 打开设置窗口