JAVA集成Freemarker生成静态html过程解析
程序员文章站
2023-01-30 09:09:38
springboot1.引入freemarker jar包 org.springframework.boot
springboot
1.引入freemarker jar包
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-freemarker</artifactid> </dependency>
2.配置application.properties
### freemarker spring.freemarker.templateloaderpath=classpath:/templates/ spring.freemarker.suffix=.ftl spring.freemarker.charset=utf-8 spring.freemarker.request-context-attribute=request spring.freemarker.settings.number_format=0.##########
3.创建ftl文件
在resource文件下新增文件夹templates,在templates文件夹下存放ftl文件,例如:
<!doctype html> <html> <head> <meta charset="utf-8"/> <title>freemarker</title> </head> <body> <h1>simple project</h1> <h1>${key}</h1> </body> </html>
4.新建controller调用方法
@suppresswarnings("unchecked") @requestmapping(value="/test",method=requestmethod.get) public string test(model model,httpservletrequest request) { model.addattribute("key","test project"); return "test"; }
5.生成html静态文件
使用工具类:
/** * @param modename 模板名称 * @param targetfilename 生成后的html名称 * @param params 传入模板的参数 * @author: zy * @date: 2020-6-4 09:39:47 * @description:生成静态页面 */ public void createhtmlbymode(string modename, string targetfilename, map<string, object> params) { writer out = null; // 找到服务器缓存目录,可以自己指定目录 string folder = propertisutil.getapplicationproperties("healthreport.logs.urls") + targetfilename; // 通过匹配路径格式拼接完整生成路径 string outfile = folder; try { file file = new file(outfile); // 生成空html文件 if (!file.exists()) { file.createnewfile(); } // 创建模版对象 template template = cfg.gettemplate(modename); // 设置输出流 out = new outputstreamwriter(new fileoutputstream(file), "utf-8");// 设置编码 utf-8 // 模版数据插入参数,通过输出流插入到html中 template.process(params, out); } catch (exception e) { e.printstacktrace(); } finally { if (null != out) { try { out.flush(); out.close(); } catch (ioexception e) { e.printstacktrace(); } } } }
修改controller中的方法:
@suppresswarnings("unchecked") @requestmapping(value="/test",method=requestmethod.get) public string test(model model,httpservletrequest request) { model.addattribute("key","test project"); //生成静态文件 map param=new hashmap(); param.put("key", "我是被生成的静态文件"); createhtmlbymode("test.ftl","test.html",param); return "test"; }
实现效果(我这里默认保存到d:/testlogs):
springmvc(和springboot大致相同,此处只留下配置)
1.引入freemarker jar包
<dependency> <groupid>org.springframework</groupid> <artifactid>spring-context-support</artifactid> <version>3.2.4.release</version> </dependency> <!-- https://mvnrepository.com/artifact/org.freemarker/freemarker --> <dependency> <groupid>org.freemarker</groupid> <artifactid>freemarker</artifactid> <version>2.3.25-incubating</version> </dependency>
2.springmvc配置
<!-- freemarker --> <bean id="freemarkerconfigurer" class="org.springframework.web.servlet.view.freemarker.freemarkerconfigurer"> <property name="templateloaderpath" value="/freemarker/ftl/"></property> <property name="defaultencoding" value="utf-8" /> <property name="freemarkersettings"> <props> <prop key="template_update_delay">1</prop> <prop key="locale">zh_cn</prop> <prop key="datetime_format">yyyy-mm-dd</prop><!-- 时间格式化 --> <prop key="date_format">yyyy-mm-dd</prop> <prop key="number_format">#.##</prop> </props> </property> </bean> <bean id="freemarkerviewresolver" class="org.springframework.web.servlet.view.freemarker.freemarkerviewresolver"> <property name="cache" value="true" /> <property name="prefix" value="" /><!-- 上面已经配了,这里就不用配啦 --> <property name="suffix" value=".ftl" /> <property name="contenttype" value="text/html;charset=utf-8" /> <property name="allowsessionoverride" value="true" /> <property name="allowrequestoverride" value="true" /> <property name="exposespringmacrohelpers" value="true" /> <property name="exposerequestattributes" value="true" /> <property name="exposesessionattributes" value="true" /> <property name="requestcontextattribute" value="request" /> </bean>
3.调用方式
/**返回模板信息*/ @suppresswarnings("unchecked") @requestmapping(value="/test",method={requestmethod.get}) public modelandview test(httpservletrequest request) { modelandview mv = new modelandview(); //设置参数 mv.addobject("key", "测试freemarker"); //配置模板 mv.setviewname("test"); return mv; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。