freemarker

Freemarker + xml 实现Java导出word

橙三吉。 提交于 2019-12-05 10:58:04
前言 最近做了一个调查问卷导出的功能,需求是将维护的题目,答案,导出成word,参考了几种方案之后,选择功能强大的freemarker+固定格式之后的wordxml实现导出功能。导出word的代码是可以直接复用的,于是在此贴出,并进行总结,方便大家拿走。 实现过程概览 先在word上,调整好自己想要的样子。然后存为xml文件。保存为freemarker模板,以ftl后缀结尾。将需要替换的变量使用freemarker的语法进行替换。最终将数据准备好,和模板进行渲染,生成文件并返回给浏览器流。 详细的实现过程 准备好word的样式 我们新建一个word,我们应该使用Microsoft office,如果使用wps可能会造成样式有些不兼容。在新建的office中,设置好我们的表格样式。我们的调查问卷涉及到四种类型,单选,多选,填空,简答。我们做出四种类型的示例。 样式没有问题后,我们选择另存为word xml 2003版本。将会生成一个xml文件。 格式化xml,并用freemarker语法替换xml 我们可以先下载一个工具 firstobject xml editor,这个可以帮助我们查看xml,同时方便我们定位我们需要改的位置。 复制过去之后,按f8可以将其进行格式化,左侧是标签,右侧是内容,我们只需要关注w:body即可。 像右侧的调查问卷这个就是个标题

Freemarker removeIntrospectionInfo does not work with DCEVM after model hotswap

邮差的信 提交于 2019-12-05 08:40:24
I am using Freemarker and DCEVM+HotSwapManager agent. This basically allows me to hotswap classes even when adding/removing methods. Everything works like charm until Freemarker uses hotswapped class as model. It's throwing freemarker.ext.beans.InvalidPropertyException: No such bean property on me even though reflection shows that the method is there (checked during debug session). I am using final Method clearInfoMethod = beanWrapper.getClass().getDeclaredMethod("removeIntrospectionInfo", Class.class); clearInfoMethod.setAccessible(true); clearInfoMethod.invoke(clazz); to clear the cache, but

In freemarker is it possible to check to see if a file exists before including it?

泪湿孤枕 提交于 2019-12-05 04:25:48
We are trying to build a system in freemarker where extension files can be optionally added to replace blocks of the standard template. We have gotten to this point <#attempt> <#include "extension.ftl"> <#recover> Standard output </#attempt> So - if the extension.ftl file exists it will be used otherwise the part inside of the recover block is output. The problem with this is that freemarker always logs the error that caused the recover block to trigger. So we need one of two things: Don't call the include if the file doesn't exist - thus the need to check for file existence. -OR- A way to

How can I get the first item in a list using free marker?

一曲冷凌霜 提交于 2019-12-05 04:21:48
I have the following code which contains about 12 items but I only need to retrieve the first item. How can I display the first item in my list? My code is: <#list analysttest.rss.channel.item as item> <div> <h3 class="bstitle">${item.title}</h3> <span class="bsauthor">${item.author}</span> <span>${item.pubDate}</span> <p>${item.description}</p> </div> </#list> analysttest.rss.channel.item[0] gives the fist item, which you can #assign to a shorther name for convenience. Note that at least 1 item must exist, or else you get an error. (Or, you can do something like <#assign item = analysttest

Freemarker utf-8 encoding problems on t.page

荒凉一梦 提交于 2019-12-05 02:00:05
问题 I'm having problems with inside pages. It simply are recognizing pages as iso, but I want utf-8, I'm declaring it as default charset. I tried some modifications on freemarker configuration, but they are not having effect. spring-servlet.xml <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <property name="templateLoaderPath" value="/WEB-INF/pages/"/> </bean> template.html <#macro page> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0

Checking Spring security roles and logged username in Freemarker template

不羁的心 提交于 2019-12-05 00:28:06
问题 Does anyone know the freemarker tags to check spring security roles and username in freemarker file? I found from couple of resources on the web that the following code would print the logged in username. But it is not printing the username, instead it is printing just "logged in as" <security:authorize access="isAuthenticated()"> logged in as <security:authentication property="principal.username" /> </security:authorize> Also checking the roles in Freemarker file is also not working. Has

深入了解Java模板引擎Freemarker

泪湿孤枕 提交于 2019-12-05 00:19:37
前言 常用的Java模板引擎包括:JSP、Freemarker、Thymeleaf、Velocity,从Github上查阅到这几款主流的模板引擎的性能的对比,总体上看,JSP、Freemarker、Thymeleaf、Velocity在性能上差别不是很大,而Thymeleaf与前三者相比,在性能上存在着较大的差距,因此在选择Thymeleaf作为模板需要慎重,本片文章主要介绍Freemarker模板引擎的只要标签和使用。 模板引擎性能对比 参考: https://github.com/jreijn/spring-comparing-template-engines/issues/19 本地系统配置: 架构:x86_64 CPU操作模式:32位,64位 字节顺序:小端字节 CPU:4 个在线CPU列表:0-3个 线程每个内核: 每个插槽2个内核:2个 插槽:1个 NUMA节点:1 供应商ID:真正的 英特尔 CPU系列:6 型号:37 型号名称:英特尔(R)Core(TM)i5 CPU M 430 @ 2.27GHz 步进:2 CPU MHz:1199.000 CPU max MHz:2267.0000 CPU min MHz:1199.0000 BogoMIPS:4522.04 虚拟化:VT-x L1d缓存:32K L1i缓存:32K L2缓存:256K L3缓存:3072K

freemarker template for loop statement

和自甴很熟 提交于 2019-12-05 00:09:34
I want to create for statement in freemarker template. I am reading howto http://freemarker.sourceforge.net/ but there is just list. How can i create for statement or foreach. parameter.put("size", size); I want to create in freemarker template for statement like for (int number = 1; number <= size; number++) { Laabidi Raissi From the Freemarker manual you can do: <#assign x=3> <#list 1..x as i> ${i} </#list> Edit: Beware, if x is 0 (or less), it will count backwards. So you mostly want 1 ..< x , which excludes x (this requires FreeMarker 2.3.22). longhua You can use list directive: <ul> <

在FTL文件中使用自定义的Java方法

社会主义新天地 提交于 2019-12-04 23:47:10
在项目中使用FreeMarker做为Spring MVC中的视图文件,在展示List的时候,展示的对象中带有时间字段,但是此时间字段存的是整型的毫秒值,为了更好的展示给用户,必须要进行格式化。 但是FreeMarker中,没有这样的功能方法,只是自己去实现,还好它提供了一个接口,只需要在Java代码中,实现TemplateMethodModel,则可以在FTL中使用了。下面是我实现的Java代码: public class LongTimeToDate implements TemplateMethodModel { /* * (non-Javadoc) * @see freemarker.template.TemplateMethodModel#exec(java.util.List) */ @SuppressWarnings("rawtypes") @Override public Object exec(List args) throws TemplateModelException { if (null != args && 0 < args.size()) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd H:mm:ss"); return format.format(new Date(Long

springboot(9)——整合freemarker

余生长醉 提交于 2019-12-04 20:06:35
目录 概述 springboot整合freemarker 概述 关于freemarker基础语法使用详见 https://www.cnblogs.com/javayihao/p/11881053.html springboot整合freemarker 第一步: 加入freemarker启动器 spring-boot-starter-freemarker ​ 第二步:再templates文件下创建模板 ​ 名称习惯已ftl结尾,也可以是html,自己要配置 修改以上默认可以采用在application配置文件中选择配置 ​ 关于freemarker的语法使用,详见 https://www.cnblogs.com/javayihao/p/11881053.html ​ 公众号 java一号 更多java实战项目资料、技术干活。更重要的是小猿愿成为你编程路上的一个朋友! 文章首发地址: www.javayihao.top 首发公众号: java一号 HTTPS://呜呜呜.cn blog上.com/Java一号/怕/11881053.HTML [HTTPS://Wū wū wū.Cn blog shàng.Com/Java yī hào/pà/11881053.HTML] HTTPS: // on ohh .cn blog .com / Java One / fear /11881053