springboot整合freemarker生成静态HTML页面
2018-01-28 15:20
791 查看
这篇博客是基于上篇富文本图片异步保存之后,富文本内容生成静态页面,下面我就奉上自己的代码供大家参考,希望可以帮到有用的朋友,
首先是pom:
<!--引入freemarker--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> <version>1.4.1.RELEASE</version>
接下来是freemarker的一些基本设置,包括他的ftl文件路径,也就是他的模板路径,我在resources下templates下,后边是application.properties中的设置:
spring.freemarker.suffix=.ftl spring.freemarker.templateEncoding=UTF-8 spring.freemarker.templateLoaderPath=classpath:/templates/ spring.freemarker.content-type=text/html
接下来就是模本文件ftl,其中第一行的代码非常关键,我刚开始出现生成的页面浏览器打开是中文乱码,工具打开正常,后来查资料发现,文件默认保存是GBK
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style type='text/css'> *{ margin: 0; padding:0; } html{ font-size: 14px; } p{ width: 100%; margin-bottom: 5px; } p img{ width: 100%; } </style> <!DOCTYPE html> <html> <body> <h4>标题:${title}</h4> </br> ${content} </br> </body> </html>
接下来就是后台代码其中注意保存的路径问题
//获得freeMarker对象 @Autowired private Configuration configuration;
/** * 添加内容文件同时生成静态页面 * * c9ed @param weixinContent 内容实体对象 * @param request * @param * @return */ @ResponseBody @RequestMapping(value = "/service", method = RequestMethod.POST) public String ContentAdd(WeixinContent weixinContent, HttpServletRequest request,HttpServletResponse response) { try { //设置创建时间 weixinContent.setGmtCreate(new Date()); //设置修改时间 weixinContent.setGmtModified(new Date()); //设置是不否删除 weixinContent.setDelFlag(1); //设置状态 未测试状态 weixinContent.setState(1); //设置创建人 weixinContent.setFounder("张三"); //设置修改人 weixinContent.setModifier("李四"); //设置内容类型 weixinContent.setContentType("苹果树"); //设置菜单ID weixinContent.setMenuId(25L); //调用service代理对象进行添加操作 weixinContentService.insert(weixinContent); Long id=weixinContent.getId(); //获得保存静态资源路径 String htmlRealPath=request.getSession().getServletContext().getRealPath("/")+"\\freeMarker\\"; //实际部署保存路径 //String htmlRealPath=contentHtmlLocation; System.out.println("保存的绝对路径是:"+htmlRealPath+ "/" + id + ".html"); // 创建文件对象 File htmlFile = new File(htmlRealPath + "/" + id + ".html"); // 判断是否有静态页面文件 若没有 if (!htmlFile.exists()) { // 获得模板对象 Template template = configuration .getTemplate("content.ftl"); //先得到文件的上级目录,并创建上级目录,在创建文件 htmlFile.getParentFile().mkdir(); try { //创建文件 htmlFile.createNewFile(); } catch (IOException e) { e.printStackTrace(); } // 创建集合 //创建map集合 Map<String,Object> map=new HashMap<>(); map.put("title",weixinContent.getTitle()); map.put("content",weixinContent.getContent()); Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(htmlFile),"UTF-8")); // 合并输出 创建页面文件 template.process(map,out); } // 设置响应中文乱码问题 response.setContentType( "text/html;charset=UTF-8"); //获得工程的相对路径 String saveUrl ="http://127.0.0.1:8060/freeMarker/"+id+".html"; //实际部署访问路径 // String saveUrl =contentHtmlUrl + "/" + id + ".html"; System.out.println("保存的相对路径:"+saveUrl); weixinContent.setTemplatesUrl(saveUrl); //添加静态页面路径 weixinContentService.updateById(weixinContent); Map<String,Object> map=new HashMap<>(); map.put("path",saveUrl); return JSON.toJSONString(map); } catch (Exception e) { e.printStackTrace(); return null; } }
相关文章推荐
- Spring Boot使用freemarker并且生成静态html页面
- springboot-10-前端页面整合, thymeleaf, freemarker, jsp 模板使用
- JAVA程序通过freemarker生成静态HTML页面
- thymeleaf :springboot整合mybatis的web项目中使用html而不是jsp当页面渲染
- Freemarker生成HTML静态页面
- spring boot 使用velocity、freeMarker模板创建html页面返回给前端
- 利用freemarker、java生成html静态页面
- springmvc+freemarker生成静态html文件
- Java语言SpringMvc整合FreeMarker静态页面生成
- Spring-Boot整合freemarker引入静态资源css、js等(转)
- springboot 项目使用freemarker 配置之后,访问静态和动态页面的方法
- 利用Freemarker生成html静态页面,也可利用模版功能生成不同格式的文件
- java使用freemarker生成静态html页面,基于中文API(简单版)
- springboot thymeleaf 生成静态html
- spring boot 使用velocity、freeMarker模板创建html页面返回给前端
- springboot + freemarker 返回html页面基本操作(初学者)
- ASP网站生成静态html页面技术小结
- 使用HTTPCLIENT生成静态HTML页面
- JSP 生成静态HTML页面
- 如何用asp生成静态html页面??