JAVA集成Freemarker生成靜態(tài)html過程解析
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.創(chuàng)建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調(diào)用方法
@SuppressWarnings("unchecked") @RequestMapping(value="/test",method=RequestMethod.GET) public String test(Model model,HttpServletRequest request) { model.addAttribute("key","test project"); return "test"; }
5.生成html靜態(tài)文件
使用工具類:
/** * @param modeName 模板名稱 * @param targetFileName 生成后的HTML名稱 * @param params 傳入模板的參數(shù) * @Author: zy * @Date: 2020-6-4 09:39:47 * @Description:生成靜態(tài)頁(yè)面 */ public void createHtmlByMode(String modeName, String targetFileName, Map<String, Object> params) { Writer out = null; // 找到服務(wù)器緩存目錄,可以自己指定目錄 String folder = PropertisUtil.getApplicationProperties("healthReport.logs.urls") + targetFileName; // 通過匹配路徑格式拼接完整生成路徑 String outFile = folder; try { File file = new File(outFile); // 生成空HTML文件 if (!file.exists()) { file.createNewFile(); } // 創(chuàng)建模版對(duì)象 Template template = cfg.getTemplate(modeName); // 設(shè)置輸出流 out = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");// 設(shè)置編碼 UTF-8 // 模版數(shù)據(jù)插入?yún)?shù),通過輸出流插入到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"); //生成靜態(tài)文件 Map param=new HashMap(); param.put("key", "我是被生成的靜態(tài)文件"); createHtmlByMode("test.ftl","test.html",param); return "test"; }
實(shí)現(xiàn)效果(我這里默認(rèn)保存到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><!-- 時(shí)間格式化 --> <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="" /><!-- 上面已經(jīng)配了,這里就不用配啦 --> <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.調(diào)用方式
/**返回模板信息*/ @SuppressWarnings("unchecked") @RequestMapping(value="/test",method={RequestMethod.GET}) public ModelAndView test(HttpServletRequest request) { ModelAndView mv = new ModelAndView(); //設(shè)置參數(shù) mv.addObject("key", "測(cè)試freemarker"); //配置模板 mv.setViewName("test"); return mv; }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
IDEA 設(shè)置顯示內(nèi)存的使用情況和內(nèi)存回收的方法
這篇文章主要介紹了IDEA 設(shè)置顯示內(nèi)存的使用情況和內(nèi)存回收的方法,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04SpringBoot?2.7.18?集成?Mybatis?Plus?+?Druid的實(shí)例詳解
Mybatis和MybatisPlus都是流行的持久層框架,MybatisPlus在Mybatis基礎(chǔ)上增加了更多便捷的功能,如自動(dòng)CRUD、分頁(yè)插件等,文章還提到了Entity、Mapper、Service、Controller等組件的基本使用方法,為開發(fā)者提供了一套完整的集成方案2024-10-10Jersey實(shí)現(xiàn)Restful服務(wù)(實(shí)例講解)
下面小編就為大家?guī)硪黄狫ersey實(shí)現(xiàn)Restful服務(wù)(實(shí)例講解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08springboot使用log4j2異步日志提升性能的實(shí)現(xiàn)方式
這篇文章主要介紹了springboot使用log4j2異步日志提升性能,異步日志實(shí)現(xiàn)方式:將日志存入一個(gè)單獨(dú)的隊(duì)列中,有一個(gè)單獨(dú)的線程從隊(duì)列中獲取日志并寫入磁盤文件,需要的朋友可以參考下2022-05-05JavaScript中HTML元素操作的實(shí)現(xiàn)
本文主要介紹了JavaScript中HTML元素操作的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06ElasticSearch 動(dòng)態(tài)映射實(shí)戰(zhàn)詳解
這篇文章主要為大家介紹了ElasticSearch 動(dòng)態(tài)映射實(shí)戰(zhàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01IntelliJ IDEA 2020.2 配置大全詳細(xì)圖文教程(更新中)
這篇文章主要介紹了IntelliJ IDEA 2020.2 配置大全(更新中),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08