(转)mblog解读(二)
(二期)12、开源博客项目mblog解读(二)
常用指令和宏
freemarker宏 macro , nested , 函数
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
@Component
public class FreemarkerConfig {
@Autowired
private Configuration configuration;
@Autowired
private ApplicationContext applicationContext;
@PostConstruct
public void setSharedVariable() throws TemplateModelException {
configuration.setSharedVariable("author_contents", applicationContext.getBean(AuthorContentsDirective.class));
}
}
Configuration
是一个存放应用级别(application level)公共配置信息,以及模版(Template)可使用的全局共享变量的一个对象。同时它还负责模版(Template)实例的创建以及缓存。
import freemarker.template.Template;
import freemarker.template.TemplateException;
import java.io.*;
import java.util.Map;
/**
* 创建人: leon
* 创建时间: 2014年11月28日 上午10:07:51
* 类描述:Freemarker的工具类
*/
public class FreemarkerUtil {
/**
* 通过指定的文件目录和文件名生成相应的文件
* @param fileDir
* @param fileName
*/
public static Boolean printToFile(Template template,String fileDir,String fileName,Map<String,Object> root) {
boolean done = false;
Writer writer = null;
try {
//判断多级目录是否存在,不存在则一级级创建
String[] paths = fileDir.split("\\\\");//注意:此处“\\”是错误的,必须要“\\\\”才能分割字符串
String dir = paths[0];
for (int i = 1; i < paths.length; i++) {
dir = dir + File.separator + paths[i];
File file=new File(dir.toString());
if (!file.exists()) {
file.mkdir();
}
}
//创建输出流
File file = new File(fileDir +File.separator+ fileName);
//设置生成的文件编码为UTF-8
//服务器不支持UTF-8格式HTML时候使用ANSI格式HTML文件,即系统默认编码
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file),"UTF-8")); // 编码设置3
//writer = new FileWriter(fileDir +File.separator+ fileName);
//输出模板和数据模型都对应的文件
template.process(root, writer);
done = true;
} catch (IOException e) {
e.printStackTrace();
} catch (TemplateException e) {
e.printStackTrace();
} finally {
try {
if(writer!=null){
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return done;
}
}
@RestController
public class ToHtmlController extends BaseController {
@Autowired
private PostService postService;
@Autowired
private Configuration configuration;
@ResponseBody
@RequestMapping("/view/tohtml/{id}")
public boolean toHtml(@PathVariable Long id, HttpServletRequest req) throws IOException {
Template template = configuration.getTemplate("/default/channel/view.ftl");
String fileName = id + ".html";
String htmlDir = "D:/git-second/course-11-mblog/mblog/web/src/main/resources/static/html";
PostVO view = postService.get(id);
Map<String, Object> params = new HashMap<>();
params.put("view", view);
return FreemarkerUtil.printToFile(template, htmlDir, fileName, params);
}
}
需要注意的是,由于这个模板有些数据是通过模板标签来活得数据的,所以静态化过程中不能活得,需要重写模板,还有登录状态,阅读数等问题~~
重要的几个类:
- TemplateDirectiveModel:
- TemplateDirective :
- DirectiveHandler
- TemplateModelUtils
- ChannelDirective :
- FreemarkerConfig :
自定义函数
TemplateMethodModelEx
@Indexed
@Field
@NumericField
@Fields
@Analyzer
<!--hibernate search orm-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-search-orm</artifactId>
<version>5.6.3.Final</version>
</dependency>
<!--hibernate search dependency-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</dependency>
<!--处理中文的分析器-->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-analyzers-smartcn</artifactId>
<version>5.5.4</version>
</dependency>
<!--查询结果的高亮处理-->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-highlighter</artifactId>
<version>5.5.4</version>
</dependency>
<!-- /end -->
spring
jpa:
database: mysql
show-sql: false
hibernate:
ddl-auto: update
properties:
hibernate.format_sql: true
hibernate.naming.physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
hibernate.cache.use_second_level_cache: false
#把索引的文件保存在文件系统中
hibernate.search.default.directory_provider: filesystem
#指定索引保存的路径
hibernate.search.default.indexBase: ./indexes