freemarker

Displaying Spring MVC validation errors in Freemarker templates

谁说胖子不能爱 提交于 2019-11-30 23:23:51
I'm trying to display a list of global validation errors in my freemarker template if a controller returns binding errors. I can display errors that are associated with a field, but I want to detect when an error has occurred within a specific bean and display a message at the top of the page. I've tried using the example below which produces no output: <@spring.bind "webPage" /> .... <#if spring.status.error> There were problems with the data you entered: <ul> <#list spring.status.errorMessages as error> <li>${error?html}</li> </#list> </ul> </#if> The line below always returns 0, despite

freemarker templates in several jars

本小妞迷上赌 提交于 2019-11-30 19:11:56
How can I configure freemarker to search templates in several jars? With spring. <!-- freemarker config --> <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <property name="templateLoaderPaths" value="classpath:/freemarker/" /> </bean> One war file (to deploy), and jar file (in dependencies). One.war /freemarker/simple.ftl Two.jar /freemarker/test.ftl Work. ModelAndView mav = new ModelAndView(); mav.setViewName("simple"); Do not work. Can't find test.ftl ModelAndView mav = new ModelAndView(); mav.setViewName("test"); If settings is:

Displaying Spring MVC validation errors in Freemarker templates

安稳与你 提交于 2019-11-30 18:57:07
问题 I'm trying to display a list of global validation errors in my freemarker template if a controller returns binding errors. I can display errors that are associated with a field, but I want to detect when an error has occurred within a specific bean and display a message at the top of the page. I've tried using the example below which produces no output: <@spring.bind "webPage" /> .... <#if spring.status.error> There were problems with the data you entered: <ul> <#list spring.status

Spring Boot 整合视图层技术

巧了我就是萌 提交于 2019-11-30 17:58:46
这一节我们主要学习如何整合视图层技术: Jsp Freemarker Thymeleaf 在之前的案例中,我们都是通过 @RestController 来处理请求,所以返回的内容为json对象。那么如果需要渲染html页面的时候,要如何实现呢?   Spring Boot推荐使用模板引擎   模板引擎实现伪html 达到seo优化 使动态页面静态化   在动态html上实现Spring Boot依然可以完美胜任,并且提供了多种模板引擎的默认配置支持,所以在推荐的模板引擎下,我们可以很快的上手开发动态网站。   Spring Boot提供了默认配置的模板引擎主要有以下几种:     Thymeleaf     FreeMarker     Velocity     Groovy     Mustache   Spring Boot建议使用这些模板引擎,避免使用jsp。 Jsp 创建项目   创建 war 项目,编写pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven

freemarker常见语法大全

江枫思渺然 提交于 2019-11-30 17:49:39
@freemarker语法 FreeMarker的模板文件并不比HTML页面复杂多少,FreeMarker模板文件主要由如下4个部分组成: 1. 文本:直接输出的部分 2. 注释:<#– … –>格式部分,不会输出 3. 插值:即${…}或#{…}格式的部分,将使用数据模型中的部分替代输出 4. FTL指令:FreeMarker指定,和HTML标记类似,名字前加#予以区分,不会输出 1. FTL指令规则 在FreeMarker中,使用FTL标签来使用指令,FreeMarker有3种FTL标签,这和HTML标签是完全类似的 1. 开始标签:<#directivename parameter> 2. 结束标签:</#directivename> 3. 空标签:<#directivename parameter/> FreeMarker指令使用注意 1. 实际上,使用标签时前面的符号#也可能变成@,如果该指令是一个用户指令而不是系统内建指令时,应将#符号改成@符号. 2. 使用FTL标签时, 应该有正确的嵌套,而不是交叉使用,这和XML标签的用法完全一样. 3. 如果全用不存在的指令,FreeMarker不会使用模板输出,而是产生一个错误消息. 4. FreeMarker会忽略FTL标签中的空白字符.值得注意的是< , /> 和指令之间不允许有空白字符. 2.

Freemarker escaping freemarker

假如想象 提交于 2019-11-30 17:00:24
问题 I'm using freemarker to generate a freemarker template. But I need some way to escape freemarker tags. How would I escape a <#list> tag or a ${expression} ? 回答1: You could also use: ${"$"}{expression} if you find the {} nesting confusing. 回答2: I'm using the alternative syntax feature. I start the template with [#ftl] and use this syntax. For the expressions I use the string literal feature: ${r"${expression}"} 回答3: In the case when you want to use non-raw strings so that you can escape double

Simulate null parameters in Freemarker macros

匆匆过客 提交于 2019-11-30 14:50:21
问题 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

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

大城市里の小女人 提交于 2019-11-30 13:38:33
问题 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

FreeMarker实现网页静态化

自古美人都是妖i 提交于 2019-11-30 13:32:45
1、FreeMarker实现网页静态化。   FreeMarker是一个用Java语言编写的模板引擎,它基于模板来生成文本输出。FreeMarker与Web容器无关,即在Web运行时,它并不知道Servlet或HTTP。它不仅可以用作表现层的实现技术,而且还可以用于生成XML,JSP或Java 等。目前企业中:主要用Freemarker做静态页面或是页面展示。 2、使用freemarker需要的jar。   a)、把下载到的jar包(freemarker-2.3.23.jar)放到\webapp\WEB-INF\lib目录下。官方网站: http://freemarker.org/   b)、如果使用的是Maven结构,可在pom.xml中引入以下坐标。 1 <dependency> 2 <groupId>org.freemarker</groupId> 3 <artifactId>freemarker</artifactId> 4 <version>2.3.23</version> 5 </dependency> 3、Freemarker原理图。模板 + 数据模型 = 输出 4、 freemarker 的测试案例如下所示: 1 package com.taotao.freemarker; 2 3 import java.io.File; 4 import java.io

Default escaping in Freemarker

橙三吉。 提交于 2019-11-30 12:43:25
In Freemarker templates we can use the escape directive to automatically apply an escaping to all interpolations inside the included block: <#escape x as x?html> <#-- name is escaped as html --> Hallo, ${name} </#escape> Is there a way to programmatically achieve a similar effect, defining a default escape applied to all interpolations in the template, including those outside escape directives? Thanks. To elaborate on Attila's answer: you can use a class like this one and then wrap your template loader like this: final TemplateLoader templateLoader = new ClassTemplateLoader(this.getClass(),