实际上我们在Eclipse + Freemarker插件(来自JBoss)下有freemarker文件(ftl).
你知道格式化ftl文件的方法吗?
谢谢
我有一个使用spring和freemarker的国际化应用程序.我正在使用本地化属性文件获取内容.
${rc.getMessage("help.headings.frequently_asked_questions")}
Run Code Online (Sandbox Code Playgroud)
对于某些内容,属性值中有回车符.因为我在网页上显示我想用这些替换它们
.
做这个的最好方式是什么?
编辑:看得更近似乎我在属性文件中实际上没有回车.这些属性将作为单行字符串返回.
是否有更好的方法来声明属性,以便他们知道它们是多行的?
help.faq.answer.new_users=If you have not yet set a PIN, please enter your username and passcode (from your token) in the boxes provided and leave the PIN field blank.\
You will be taken through the steps to create a PIN the first time you log in.
Run Code Online (Sandbox Code Playgroud)
干杯,皮特
即使模板实际存在于路径中,我在加载freemarker模板时也会收到文件未找到异常.
更新:这是作为Web服务运行的.它将根据搜索查询将xml返回给客户端.当我从另一个java程序(来自静态main)调用它时,模板加载成功.但是当客户端请求xml时,会发生FileNotFoundException.
操作系统:Windows 7文件的绝对路径:C:/ Users/Jay/workspace/WebService/templates /
这是我的代码:
private String templatizeQuestion(QuestionResponse qr) throws Exception
{
SimpleHash context = new SimpleHash();
Configuration config = new Configuration();
StringWriter out = new StringWriter();
Template _template = null;
if(condition1)
{
_template = config.getTemplate("/templates/fibplain.xml");
}
else if(condition2)
{
_template = config.getTemplate("/templates/mcq.xml");
}
context.put("questionResponse", qr);
_template.process(context, out);
return out.toString();
}
Run Code Online (Sandbox Code Playgroud)
完整错误堆栈:
java.io.FileNotFoundException: Template /templates/fibplain.xml not found.
at freemarker.template.Configuration.getTemplate(Configuration.java:495)
at freemarker.template.Configuration.getTemplate(Configuration.java:458)
at com.hm.newAge.services.Curriculum.templatizeQuestion(Curriculum.java:251)
at com.hm.newAge.services.Curriculum.processQuestion(Curriculum.java:228)
at com.hm.newAge.services.Curriculum.processQuestionList(Curriculum.java:210)
at com.hm.newAge.services.Curriculum.getTest(Curriculum.java:122)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) …Run Code Online (Sandbox Code Playgroud) 最近我写了一些工具,帮助我生成Java代码,否则这将是漫长而乏味的任务.我使用freemarker来编写模板.但是,模板中的所有空格都保留在输出中,导致代码非常混乱.我可以从我的模板中删除缩进来修复此问题,但这会使我的模板无法维护.
考虑这个简单的示例模板:
public class MyTestClass
{
<#list properties as p>
private String ${p.name};
</#list>
}
Run Code Online (Sandbox Code Playgroud)
我的模板格式很好,但我的代码是这样的:
public class MyTestClass
{
private String prop1;
private String prop2;
private String prop3;
}
Run Code Online (Sandbox Code Playgroud)
缩进有点太多了,它应该是:
public class MyTestClass
{
private String prop1;
private String prop2;
private String prop3;
}
Run Code Online (Sandbox Code Playgroud)
要做到这一点,我必须从我的模板中删除缩进,如下所示:
public class MyTestClass
{
<#list properties as p>
private String ${p.name};
</#list>
}
Run Code Online (Sandbox Code Playgroud)
对于这个简单的情况,从模板中删除缩进并不是一个真正的问题,但是您可以想象复杂的模板变得非常难以理解.
一方面我真的希望我的代码格式很好,但另一方面我希望我的模板格式也很好.我使用Eclipse作为IDE,内置格式化器完全根据我(和我的团队)的愿望进行定制.如果我能以某种方式从freemarker模板生成代码并作为后处理步骤格式化其输出与Eclipse的格式化器,那将是很好的.
我当然可以在生成代码后手动运行格式化程序,但我真的想自动执行此过程.
所以,长话短说,有谁知道如何在我自己的Java代码中使用Eclipse的代码格式化程序?
是否可以在FreeMarker模板中替换多个值?例如,如果我想用"c"替换"a"和"b",我必须这样做:
${event.EventTitle?replace('a','c')?replace('b','c')}
Run Code Online (Sandbox Code Playgroud)
但我宁愿这样做:
${event.EventTitle?replace("'a','b'",'c')}
Run Code Online (Sandbox Code Playgroud)
FreeMarker有这种能力吗?
我最终试图用破折号替换所有特殊字符($,.,@,&等),所以请随意提出一种更简单的方法.
我想在freemarker数据模型中看到所有变量,就像struts2 debug标签一样显示值栈.
freemarker有办法做到这一点吗?
是否有人知道在IntelliJ 14.0.2下为Spring Boot应用程序进行资源和模板热部署的机制.
我知道完整的Spring Boot支持计划在14.1但我有一个项目,我从标准的WAR项目转换为Spring Boot项目,我真的很想念热门部署.
目前,我必须手动构建资源所在的项目以进行热部署,有时甚至会有点不稳定.我宁愿只保存一个模板或一个javascript/css文件,并像我通过IntelliJ使用本地tomcat服务器运行我的应用程序时一样.
我可以切换回Eclipse来实现这个功能,但是我的项目是基于Scala的,IntelliJ Scala的支持要优越得多.
有没有办法在freemarker模板中按索引获取列表项,可能是这样的:
<#assign i = 1>
${fields}[i]
Run Code Online (Sandbox Code Playgroud)
我是freemarker的新手.
我正在尝试使用免费标记一起创建一个包含域和数据模块的Android Studio自定义项目模板.但是我无法在新项目创建时创建新模块.请帮帮我
编辑:我正在使用此博客创建自定义模板 https://robusttechhouse.com/tutorial-how-to-create-custom-android-code-templates/ 但它只会生成一个应用模块
freemarker ×10
eclipse ×3
java ×3
templates ×2
android ×1
intellij-14 ×1
replace ×1
spring ×1
spring-boot ×1
spring-mvc ×1