我有一个资源是一个.
这意味着我的网址看起来像这样:
http://myapp/index/.
我需要添加查询参数,使它看起来像这样:
http://myapp/index/.?type=xml
我使用Freemarker来表示我的资源,并为这种情况做了一个百分比编码黑客:
<#if key?matches("\\.")>
<li><a href="${contextPath}/index/%2E">${key}</a></li>
</#if>
Run Code Online (Sandbox Code Playgroud)
这适用于Firefox.但所有其他浏览器,如IE,Safari,Chrom,Opera只是忽略了我的网址编码点(http://myapp/index/%2E).
有什么建议?
我想将我的FreeMarker模板存储在一个类似于以下内容的数据库表中:
template_name | template_content
---------------------------------
hello |Hello ${user}
goodbye |So long ${user}
Run Code Online (Sandbox Code Playgroud)
当收到具有特定名称的模板的请求时,这应该导致执行查询,该查询加载相关的模板内容.然后,应将此模板内容与数据模型(上述示例中的'user'变量的值)一起传递给FreeMarker.
但是,FreeMarker API似乎假设每个模板名称对应于文件系统特定目录中的同名文件.有什么方法可以轻松地从数据库而不是文件系统加载我的模板?
编辑:我应该提到我希望能够在应用程序运行时向数据库添加模板,因此我不能简单地在启动时将所有模板加载到新的StringTemplateLoader中(如下所示).
干杯,唐
我试图在Freemarker和Velocity之间作为模板引擎来决定你看到的每个优点/缺点?
我应该考虑的任何替代方案?
如何在eclipse中安装Freemarker Plug-In?
我用Google搜索并发现了这个网站
但它对我不起作用.#3中提到的URL" http://www.freemarker.org/eclipse/update "不存在.然后我尝试了URL" http://www.freemarker.org/eclipse/ "然后它工作但是在安装之后,我在Windows中添加了*.ftl作为文件类型 - >首选项 - >常规 - >编辑器 - > FIle关联但是我在"Associated editor"中找不到Freemarker编辑器.
我在Windows 7中使用Eclipse of Version:Indigo Service Release 1.
有什么建议吗???
我正在开发一个freemarker模板,这是一个示例.
<Grantor>
<UniqueID>${(currentGrantorIndex)!?string}</UniqueID> // want to comment this line
<Entity>${(grantor.entityTypeName)!?string}</Entity>
</Grantor>
Run Code Online (Sandbox Code Playgroud)
我想知道如何在freemarker模板中写评论或注释掉几行.任何的想法?
以下freemarker代码会导致异常
<#assign i= it.getList().size()>
<#list it.getList() as elem>
<#if i==1>
<li>${elem.name}</li>
<#else>
<li class="marked">${elem.name}</li>
</#if>
<#assign i = i-1>
</#list>
Run Code Online (Sandbox Code Playgroud)
抛出以下异常:
期待哈希.it.getList()被评估为freemarker.template.SimpleSequence
谁知道为什么?如何将列表的长度分配给我的变量i?
我们正在使用Spring MVC和freemarker作为模板语言构建一个restful api.我们选择在freemarker中构建json响应
示例freemarker.ftl:
{
"field1" : "${response.value1}",
"field2" : "${response.value2}"
}
Run Code Online (Sandbox Code Playgroud)
当值中的字符串包含引号(或JSON语法中的任何其他字符)时,我们会遇到问题.
问题:如何使用freemarker来逃避这些字符串?
我们已经查看过?xml或者?html没有涵盖所有相关字符(例如\).
编辑: ?js_string将转义字符串以符合JavaScript.由于JSON基于JavaScript(JavaScript Object Notation),因此它可以工作.
编辑2:如果单引号弹出,?js_script将逃脱它再次导致无效的JSON.它的修补程序是:
${variable?js_string?replace("\\'", "\'")}
Run Code Online (Sandbox Code Playgroud)
如果你真的想挑剔:
${variable?js_string?replace("\\'", "\'")?replace("\\>",">")}
Run Code Online (Sandbox Code Playgroud)
或者,如果您使用Spring:http://www.springsurf.org/sites/1.0.0.M3/spring-webscripts/spring-webscripts-documentation/reference/html-single/index.html#js-api-index- org.springframework.extensions.webscripts.json.jsonutils
我有一个哈希映射如下
HashMap<String, String> map = new HashMap<String, String>();
map.put("one", "1");
map.put("two", "2");
map.put("three", "3");
Map root = new HashMap();
root.put("hello", map);
Run Code Online (Sandbox Code Playgroud)
我的Freemarker模板是:
<html><body>
<#list hello?keys as key>
${key} = ${hello[key]}
</#list>
</body></html>
Run Code Online (Sandbox Code Playgroud)
目标是在我生成的HTML中显示键值对.请帮我做.谢谢!
我想检查freemarker模板中的序列是否为空.
此代码段用于检查序列是否包含值:
<#if node.attachments?seq_contains("blue")>
<pre>hello</pre>
</#if>
Run Code Online (Sandbox Code Playgroud)
但是,如果node.attachments是空的我想做别的事情.
那是这个的语法?
在.jsp中我会使用:
<fmt:message key="welcome.title"/>
Run Code Online (Sandbox Code Playgroud)
从我的messages.properties文件中显示一条消息.
我如何用freemarker做到这一点?