在cms服务需要集成freemarker:
1、在CMS服务中加入freemarker的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
2、在application.yml配置freemarker
freemarker:
cache: false #关闭模板缓存,方便测试
settings:
template_update_delay: 0
创建页面预览控制器
package com.xuecheng.manage_cms.controller;
import com.xuecheng.framework.web.BaseController;
import com.xuecheng.manage_cms.service.PageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.servlet.ServletOutputStream;
import java.io.IOException;
/**
* @author Administrator
* @version 1.0
* @create 2018-09-15 16:20
**/
@Controller//注意:这里不是@RestController,因为我们这里最终要显示html内容而不是json数据,所以用@Controller
//BaseController是common包的一个基础类,提供了request和response
public class CmsPagePreviewController extends BaseController {
@Autowired
PageService pageService;
//页面预览
@RequestMapping(value="/cms/preview/{pageId}",method = RequestMethod.GET)
public void preview(@PathVariable("pageId") String pageId) throws IOException {
//执行静态化
String pageHtml = pageService.getPageHtml(pageId);
//通过response对象将内容输出
ServletOutputStream outputStream = response.getOutputStream();
outputStream.write(pageHtml.getBytes("utf-8"));
}
}
访问http://localhost:31001/cms/preview/5a795ac7dd573c04508f3a56/#/