使用freemarker进行备用行着色的好方法是什么?
这真的是最好的方式吗?
<#assign row=0>
<#list items as item>
<#if (row % 2) == 0>
<#assign bgcolor="green">
<#else>
<#assign bgcolor="red">
</#if>
<tr style='background-color: ${bgcolor}'><td>${item}</td></tr>
<#assign row = row + 1>
</#list>
Run Code Online (Sandbox Code Playgroud)
我试过这样做:
<#assign row=0>
<#list items as item>
<tr style='background-color: ${(row % 2) == 0 ? "green" : "blue"}'><td>${item}</td></tr>
<#assign row = row + 1>
</#list>
Run Code Online (Sandbox Code Playgroud)
但显然你不能在那里使用三元运算符.
注意:我想我之前应该提到它,但我不能使用css类或javascript,因为这个HTML会进入电子邮件消息.
当我使用FreeMarker读取UTF-8编码模板时,在浏览器中正确呈现特殊字符,但freeMarkerConfig.getDefaultEncoding()返回"Cp1252".如果我设置freeMarkerConfig.setDefaultEncoding("UTF-8"),我只在浏览器中看到问号,尽管"UTF-8"是模板文件的实际编码.在每种情况下,都会发送http标头"Content-Type:text/html; charset = UTF-8".
知道什么是错的吗?
我在Freemarker中创建了函数:
<#function formatDate anyDate>
<#assign dateFormat = read_from_configuration() />
<#if anyDate??>
<#return anyDate?date(dateFormat) />
<#else >
<#return '' />
</#if>
</#function>
我称之为:${formatDate(object.someDate)}.
一切正常,直到someDate为空.在那种情况下,我得到例外:
Error executing macro: formatDate required parameter: anyDate is not specified.
我怎样才能做到这一点?如果参数值为null,我希望函数能够工作.
我在java中有这个HashMap:
HashMap<String, String> map = new HashMap<String, String>();
map.put("k1", "3");
map.put("k2", "4");
map.put("k3", "2");
map.put("k4", "6");
map.put("k5", "1");
map.put("k6", "5");
Run Code Online (Sandbox Code Playgroud)
我在这种模式下使用freemarker模板打印:
<#list map?values as v>
${v} -
</#list>
Run Code Online (Sandbox Code Playgroud)
但它按此顺序打印:
2 - 6 - 1 - 5 - 3 - 4
Run Code Online (Sandbox Code Playgroud)
我想按此顺序打印:
1 - 2 - 3 - 4 - 5 -6
Run Code Online (Sandbox Code Playgroud)
如何使用freemarker模板对值进行排序?
我们一直遇到模板错误偶尔潜入我们的生产站点的问题,所以如果有一个工具来捕获这些,我很乐意将它添加到我们的部署过程中.
我有一个模板,我在其中:
<#if result.numFound > 10>
(some data)
</#if>
Run Code Online (Sandbox Code Playgroud)
这给了我解析错误:
For "#if" condition: Expected a boolean, but this evaluated to a number
Run Code Online (Sandbox Code Playgroud)
result.numFound是Integer.我已经阅读了文档,也许我错过了一些东西......
isOffline我的websetting对象中的字段是布尔类型.在我的Freemarker模板中,我需要检查它是否为假.
所以我做了以下,但它不起作用
<#if !websetting.isOffline> false </#if>
Run Code Online (Sandbox Code Playgroud)
看起来Freemarker不支持Not !.我也试过了<#if websetting.isOffline == false> false </#if>,但它不起作用.
我想迭代嵌套在Map中的List,数据结构如下:
Map<Integer, List<Integer>> groups = new TreeMap<>()
// Some code else to put values into groups ...
Run Code Online (Sandbox Code Playgroud)
Freemarker模板:
<#list groups?keys as groupKey>
${groupKey} // It's OK here.
<#list groups[groupKey] as item> // Exception threw here, detail message is pasted below
${item}
</#list>
</#list>
Run Code Online (Sandbox Code Playgroud)
详细异常消息:
FreeMarker模板错误:对于"... [...]"左手操作数:预期序列或字符串或其他东西可自动转换为字符串(数字,日期或布尔值),但这被评估为extended_hash(包装器:ftSimpleHash) :==>组
那么,问题是什么?
PS
我试过groups.get(groupKey)而不是groups[groupKey],它会抛出一个新的Exception堆栈:
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
java.lang.String.compareTo(String.java:108)
java.util.TreeMap.getEntry(TreeMap.java:346)
java.util.TreeMap.get(TreeMap.java:273)
freemarker.template.SimpleHash.get(SimpleHash.java:160)
freemarker.core.Dot._eval(Dot.java:40)
freemarker.core.Expression.eval(Expression.java:76)
Run Code Online (Sandbox Code Playgroud) 我正在尝试一个简单的Spring Boot和FreeMarker集成示例(基于我在网上找到的教程).出于某种原因,我的观点没有解决FreeMarker模板(我认为这是问题).
在浏览器中启动时的结果只是返回TFL视图文件的名称,即"索引".因此调用控制器并返回字符串"index",但似乎没有触发器来拉入FTL文件本身.任何帮助,将不胜感激...
我有以下配置类,我定义了视图解析器和Free Maker配置.
@Configuration
public class MvcConfigurer extends WebMvcConfigurerAdapter {
@Bean
public ViewResolver viewResolver() {
FreeMarkerViewResolver resolver = new FreeMarkerViewResolver();
resolver.setCache(true);
resolver.setPrefix("");
resolver.setSuffix(".ftl");
resolver.setContentType("text/html; charset=UTF-8");
return resolver;
}
@Bean
public FreeMarkerConfigurer freemarkerConfig() throws IOException, TemplateException {
FreeMarkerConfigurationFactory factory = new FreeMarkerConfigurationFactory();
factory.setTemplateLoaderPaths("classpath:templates", "src/main/resource/templates");
factory.setDefaultEncoding("UTF-8");
FreeMarkerConfigurer result = new FreeMarkerConfigurer();
result.setConfiguration(factory.createConfiguration());
return result;
}
}
Run Code Online (Sandbox Code Playgroud)
然后我有以下控制器:
@RestController
public class HelloController {
/**
* Static list of users to simulate Database
*/
private static List<User> userList = new ArrayList<User>();
//Initialize …Run Code Online (Sandbox Code Playgroud) 我正在阅读https://jersey.github.io/documentation/latest/filters-and-interceptors.html和http://www.dropwizard.io/1.1.4/docs/manual/core.html#jersey-过滤器尝试使这个:
@CookieParam("User-Data") userData: String,
@HeaderParam("User-Agent") userAgent: String,
Run Code Online (Sandbox Code Playgroud)
我的网络应用程序的每个资源GET方法都不需要.userData是来自cookie的json数据,其中包含"name"和"id"等字段,并且userAgent是标题中的完整User-Agent字符串.对于我传入的每个视图:
AppUser.getName(userData), AppUser.isMobile(userAgent)
Run Code Online (Sandbox Code Playgroud)
该getName函数解析json并仅返回name字段,isMobile如果找到字符串"mobile" ,函数将返回true布尔值.
我在FreeMarker中的应用程序的每个视图中使用它来显示用户的名字,并在移动设备为真时更改一些布局内容.
有没有办法减少重复次数?我宁愿每次都使用BeforeFilter来自动设置它.
freemarker ×10
java ×7
dropwizard ×1
if-statement ×1
java-ee ×1
jersey ×1
lint ×1
map ×1
spring ×1
templates ×1