Springboot整合Freemarker的實(shí)現(xiàn)詳細(xì)過(guò)程
基本配置、測(cè)試
1、導(dǎo)入依賴<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId></dependency>2、準(zhǔn)備一個(gè)Freemarker模板(.ftl)
@Controller@RequestMapping('/goodItem')public class GoodItemController { @Reference private IGoodsService goodsService; @Autowired private Configuration configuration; @RequestMapping('/createHtml') @ResponseBody public String createHtml(int gid, HttpServletRequest request){ //通過(guò)商品id獲取商品詳情信息 Goods goods = goodsService.queryById(gid); String [] images=goods.getGimage().split('|'); //通過(guò)模板生成商品靜態(tài)頁(yè)面 try { //獲取商品詳情的模板對(duì)象 Template template = configuration.getTemplate('goodsItem.ftl'); //準(zhǔn)備商品數(shù)據(jù) Map<String,Object> map=new HashMap<>(); map.put('goods',goods); map.put('context',request.getContextPath()); //freemarker頁(yè)面沒(méi)有分割功能,所以通過(guò)后臺(tái)將圖片分割后,將圖片數(shù)組傳到后臺(tái) map.put('images',images); //生成靜態(tài)頁(yè) //獲得classpath路徑 //靜態(tài)頁(yè)面的名稱必須和商品有所關(guān)聯(lián),最簡(jiǎn)單的方式就是用商品的id作為頁(yè)面的名字 String path = this.getClass().getResource('/static/page/').getPath()+goods.getId()+'.html';; template.process(map,new FileWriter(path)); } catch (Exception e) { e.printStackTrace(); } return ''; }}
注意:1、freemarker頁(yè)面不能通過(guò)<base th:href='http://www.4tl426be.cn/bcjs/${#request.getContextPath()+’/’}' rel='external nofollow' >獲得項(xiàng)目的根路徑。因此可從后臺(tái)將根路徑傳到前端,然后通過(guò)<base href='http://www.4tl426be.cn/bcjs/${context}/' rel='external nofollow' />獲取。2、當(dāng)page是一個(gè)空文件夾的時(shí)候,會(huì)報(bào)錯(cuò)。這是因?yàn)閙aven項(xiàng)目不會(huì)對(duì)空文件夾進(jìn)行打包編譯。
FreeMarker的基本語(yǔ)法
到此這篇關(guān)于Springboot整合Freemarker的實(shí)現(xiàn)詳細(xì)過(guò)程的文章就介紹到這了,更多相關(guān)Springboot整合Freemarker內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. SpringBoot使用Captcha生成驗(yàn)證碼2. android studio實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器(無(wú)bug)3. JavaEE SpringMyBatis是什么? 它和Hibernate的區(qū)別及如何配置MyBatis4. Python 忽略文件名編碼的方法5. android 控件同時(shí)監(jiān)聽(tīng)單擊和雙擊實(shí)例6. Java Media Framework 基礎(chǔ)教程7. springboot項(xiàng)目整合druid數(shù)據(jù)庫(kù)連接池的實(shí)現(xiàn)8. 解決vue頁(yè)面刷新,數(shù)據(jù)丟失的問(wèn)題9. python 讀txt文件,按‘,’分割每行數(shù)據(jù)操作10. python logging.info在終端沒(méi)輸出的解決
