1.模板技术
1.1.简介
velocity-1.6.3.jar 默认模板的后缀vm
freemarker-2.2.19.jar 默认模板的后缀ftl
1.2.Velocity模板技术可以实现的功能
动态页面静态化:xxx.html
在后台准备数据,在前台准备模板,通过IO把数据与模板合并,真正的生成一个html页面出来,用作发送邮件、短信模板。
1.3.pom.xml:添加jar文件
<!-- 代码生成器模版技术 -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.6</version>
</dependency>
1.4.Hello Velocity
1.4.1.VelocityTest
package cn.itsource.pss;
import java.io.StringWriter;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.junit.Test;
/**
* 测试Velocity功能
*/
public class VelocityTest {
@Test
public void testVelocity01() throws Exception {
//创建模板应用上下文
VelocityContext context = new VelocityContext();
context.put("msg", "小张是个好同志");
//拿到相应的模板(需要设置好编码)
Template template = Velocity.getTemplate("temptest/hello.html","UTF-8");
//准备输出流
StringWriter writer = new StringWriter();
template.merge(context, writer);
System.out.println(writer);
}
@Test
public void testVelocity02() throws Exception {
//创建模板应用上下文
VelocityContext context = new VelocityContext();
context.put("msg", "小张是个好同志");
//拿到相应的模板(需要设置好编码)
Template template = Velocity.getTemplate("temptest/hello.html","UTF-8");
//准备输出流
File file = new File("temptest/helloNew.html");
FileWriter writer = new FileWriter(file);
template.merge(context, writer);
writer.close();
}
}
1.4.2.temptest /hello.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
大伙都说:${msg}
</body>
</html>
2.代码生成器
咱们在写代码的时候,应该已经发现了,如果你要做另外一个模块,那么它们的基础代码都差不多,虽然我们直接拷备过来修改使用也是可以的。但还是会觉得有点浪费时间,所以我们可以做一个自己生成基础代码的功能,然后我们直接生成,再添加其它的功能即可!
没有代码生成器的时候,如果添加部门模型domain,复制IDepartmentService,修改类名,ctrl+f,进行人工替换,而使用了代码生成器就是先定义好模板,然后使用代码自动修改类名,有代码参照模板进行指定内容自动替换
2.1.开发步骤
- 准备模板(把每个模块需要改的地方确定好)
- 确定所有模板的生成顺序
- 确定所有模板的生成位置
- 确定要生成的基本功能的domain(Dept)
- 根据Domain名称与模板结合,在相应位置生成文件
- 解决如果父文件夹不存在的问题
- 解决如果文件已经存在的问题
- 排错(有错先改模板)可以将本地的图片直接拖拽到编辑区域直接展示;
2.2.准备模板
我们需要生成多少个文件,我就得准备多少个模板。把所有Employee对应的文件都拷备一份到相应的位置(并改名为Domain):(注意:大写与小写的替换)
把模板中的文件进行修改,操作的时候一定要小心,注意大小写的转换。
留下一个最基础的name字段,其它的多余功能全部去掉。
注意(Employee中关于部门的操作都需要去掉),替换的位置$不要值不要出现前后空格
2.3.确定顺序
确定修改的顺序(这个顺序你根据自己的习惯决定即可)
- src/main/java/cn/itsource/pss/query/DomainQuery.java
- src/main/java/cn/itsource/pss/repository/DomainRepository.java
- src/main/java/cn/itsource/pss/service/IDomainService.java
- src/main/java/cn/itsource/pss/service/impl/DomainServiceImpl.java
- src/main/java/cn/itsource/pss/web/controller/DomainController.java
- src/main/test/cn/itsource/pss/service/DomainServiceTest.java
- src/main/webapp/js/model/domain.js
- src/main/webapp/WEB-INF/views/domain/domain.jsp
2.4.准备路径
//准备拼接路径的常量,如果有更多文件有利于重复利用
private static final String SRC = "src/main/java/";
private static final String PACKAGE = "cn/itsource/pss/";
private static final String TEST = "src/test/java /";
private static final String WEBAPP = "src/main/webapp/";
//准备路径的拼接
private String[] paths ={
SRC+PACKAGE+"query/", SRC+PACKAGE+"repository/", SRC+PACKAGE+"service/",
SRC+PACKAGE+"service/impl/", SRC+PACKAGE+"web/controller/", TEST+PACKAGE+"service/",
WEBAPP+"js/model/",WEBAPP+"WEB-INF/views/domain/"
};
//准备所有模板名称(和路径顺序一致)
private String[] tempNames = {
"DomainQuery.java","DomainRepository.java","IDomainService.java",
"DomainServiceImpl.java","DomainController.java","DomainServiceTest.java",
"domain.js","domain.jsp"
};
//准备要生成的Domain(有可能同时生成多个)
private String[] domains = {
"Dept"};
2.5.完成创建代码
//生成相应的文件
@Test
public void testCreate() throws Exception{
//创建模板应用上下文
VelocityContext context = new VelocityContext();
//一.遍历所有的Domain
for (int i = 0; i < domains.length; i++) {
//1.1拿到大写的domain
String domainBig = domains[i];
//1.2拿到小写的domain
String domainSmall = domainBig.substring(0,1).toLowerCase() + domainBig.substring(1);
//System.out.println(domainBig);
//System.out.println(domainSmall);
//1.3设置上下文的替换名称
context.put("Domain",domainBig);
context.put("domain",domainSmall);
//二.遍历所有的路径
for (int j = 0; j < paths.length; j++) {
//2.1拿到相应的路径
String path =paths[j];
//2.2拿到相应的模板名称
String tempName = tempNames[j];
//2.3拼接回我们需要的位置文件
String realPath = (path + tempName).replaceAll("Domain",domainBig).replaceAll("domain",domainSmall);
//三.准备相应文件与模板进行组合
//3.1准备相应的文件(要生成的文件)
File file = new File(realPath);
// 如果父文件不存在,我们创建一个
File parentFile = file.getParentFile();
if(!parentFile.exists()){
parentFile.mkdirs();
}
//3.2拿到相应的模板(需要设置好编码)
Template template = Velocity.getTemplate("template/"+tempName,"UTF-8");
FileWriter writer = new FileWriter(file);
template.merge(context, writer);
writer.close();
}
}
}
2.6.细节处理
先运行看效果,如果有问题,请先进行模板文件的修改,再重新生成。直到最后功能可以成功!
完整代码如下(还解决了如果文件存在我们就不再覆盖的问题):
package cn.itsource.pss.common;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;