|
|
@@ -7,10 +7,10 @@ import cn.hutool.extra.template.TemplateEngine;
|
|
|
import org.apache.velocity.app.Velocity;
|
|
|
|
|
|
/**
|
|
|
- * Velocity模板引擎
|
|
|
- *
|
|
|
- * @author looly
|
|
|
+ * Velocity模板引擎<br>
|
|
|
+ * 见:http://velocity.apache.org/
|
|
|
*
|
|
|
+ * @author looly
|
|
|
*/
|
|
|
public class VelocityEngine implements TemplateEngine {
|
|
|
|
|
|
@@ -18,14 +18,16 @@ public class VelocityEngine implements TemplateEngine {
|
|
|
private TemplateConfig config;
|
|
|
|
|
|
// --------------------------------------------------------------------------------- Constructor start
|
|
|
+
|
|
|
/**
|
|
|
* 默认构造
|
|
|
*/
|
|
|
- public VelocityEngine() {}
|
|
|
+ public VelocityEngine() {
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* 构造
|
|
|
- *
|
|
|
+ *
|
|
|
* @param config 模板配置
|
|
|
*/
|
|
|
public VelocityEngine(TemplateConfig config) {
|
|
|
@@ -34,7 +36,7 @@ public class VelocityEngine implements TemplateEngine {
|
|
|
|
|
|
/**
|
|
|
* 构造
|
|
|
- *
|
|
|
+ *
|
|
|
* @param engine {@link org.apache.velocity.app.VelocityEngine}
|
|
|
*/
|
|
|
public VelocityEngine(org.apache.velocity.app.VelocityEngine engine) {
|
|
|
@@ -44,7 +46,7 @@ public class VelocityEngine implements TemplateEngine {
|
|
|
|
|
|
@Override
|
|
|
public TemplateEngine init(TemplateConfig config) {
|
|
|
- if(null == config){
|
|
|
+ if (null == config) {
|
|
|
config = TemplateConfig.DEFAULT;
|
|
|
}
|
|
|
this.config = config;
|
|
|
@@ -54,15 +56,16 @@ public class VelocityEngine implements TemplateEngine {
|
|
|
|
|
|
/**
|
|
|
* 初始化引擎
|
|
|
+ *
|
|
|
* @param engine 引擎
|
|
|
*/
|
|
|
- private void init(org.apache.velocity.app.VelocityEngine engine){
|
|
|
+ private void init(org.apache.velocity.app.VelocityEngine engine) {
|
|
|
this.engine = engine;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取原始的引擎对象
|
|
|
- *
|
|
|
+ *
|
|
|
* @return 原始引擎对象
|
|
|
* @since 4.3.0
|
|
|
*/
|
|
|
@@ -72,7 +75,7 @@ public class VelocityEngine implements TemplateEngine {
|
|
|
|
|
|
@Override
|
|
|
public Template getTemplate(String resource) {
|
|
|
- if(null == this.engine){
|
|
|
+ if (null == this.engine) {
|
|
|
init(TemplateConfig.DEFAULT);
|
|
|
}
|
|
|
|
|
|
@@ -80,15 +83,15 @@ public class VelocityEngine implements TemplateEngine {
|
|
|
String root;
|
|
|
// 自定义编码
|
|
|
String charsetStr = null;
|
|
|
- if(null != this.config){
|
|
|
+ if (null != this.config) {
|
|
|
root = this.config.getPath();
|
|
|
charsetStr = this.config.getCharsetStr();
|
|
|
|
|
|
// 修正template目录,在classpath或者web_root模式下,按照配置添加默认前缀
|
|
|
// 如果用户已经自行添加了前缀,则忽略之
|
|
|
final TemplateConfig.ResourceMode resourceMode = this.config.getResourceMode();
|
|
|
- if(TemplateConfig.ResourceMode.CLASSPATH == resourceMode
|
|
|
- || TemplateConfig.ResourceMode.WEB_ROOT == resourceMode){
|
|
|
+ if (TemplateConfig.ResourceMode.CLASSPATH == resourceMode
|
|
|
+ || TemplateConfig.ResourceMode.WEB_ROOT == resourceMode) {
|
|
|
resource = StrUtil.addPrefixIfNot(resource, StrUtil.addSuffixIfNot(root, "/"));
|
|
|
}
|
|
|
}
|
|
|
@@ -98,7 +101,7 @@ public class VelocityEngine implements TemplateEngine {
|
|
|
|
|
|
/**
|
|
|
* 创建引擎
|
|
|
- *
|
|
|
+ *
|
|
|
* @param config 模板配置
|
|
|
* @return {@link org.apache.velocity.app.VelocityEngine}
|
|
|
*/
|
|
|
@@ -116,29 +119,29 @@ public class VelocityEngine implements TemplateEngine {
|
|
|
|
|
|
// loader
|
|
|
switch (config.getResourceMode()) {
|
|
|
- case CLASSPATH:
|
|
|
- // 新版Velocity弃用
|
|
|
+ case CLASSPATH:
|
|
|
+ // 新版Velocity弃用
|
|
|
// ve.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
|
|
|
- ve.setProperty("resource.loader.file.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
|
|
|
- break;
|
|
|
- case FILE:
|
|
|
- // path
|
|
|
- final String path = config.getPath();
|
|
|
- if (null != path) {
|
|
|
- ve.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, path);
|
|
|
- }
|
|
|
- break;
|
|
|
- case WEB_ROOT:
|
|
|
- ve.setProperty(Velocity.RESOURCE_LOADER, "webapp");
|
|
|
- ve.setProperty("webapp.resource.loader.class", "org.apache.velocity.tools.view.servlet.WebappLoader");
|
|
|
- ve.setProperty("webapp.resource.loader.path", StrUtil.nullToDefault(config.getPath(), StrUtil.SLASH));
|
|
|
- break;
|
|
|
- case STRING:
|
|
|
- ve.setProperty(Velocity.RESOURCE_LOADER, "str");
|
|
|
- ve.setProperty("str.resource.loader.class", SimpleStringResourceLoader.class.getName());
|
|
|
- break;
|
|
|
- default:
|
|
|
- break;
|
|
|
+ ve.setProperty("resource.loader.file.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
|
|
|
+ break;
|
|
|
+ case FILE:
|
|
|
+ // path
|
|
|
+ final String path = config.getPath();
|
|
|
+ if (null != path) {
|
|
|
+ ve.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, path);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case WEB_ROOT:
|
|
|
+ ve.setProperty(Velocity.RESOURCE_LOADER, "webapp");
|
|
|
+ ve.setProperty("webapp.resource.loader.class", "org.apache.velocity.tools.view.servlet.WebappLoader");
|
|
|
+ ve.setProperty("webapp.resource.loader.path", StrUtil.nullToDefault(config.getPath(), StrUtil.SLASH));
|
|
|
+ break;
|
|
|
+ case STRING:
|
|
|
+ ve.setProperty(Velocity.RESOURCE_LOADER, "str");
|
|
|
+ ve.setProperty("str.resource.loader.class", SimpleStringResourceLoader.class.getName());
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ break;
|
|
|
}
|
|
|
|
|
|
ve.init();
|