update:velocity代码重构

This commit is contained in:
JackQ 2017-12-21 17:46:51 +08:00
parent 39a1e2fda8
commit f9b9dfc136
3 changed files with 30 additions and 25 deletions

View File

@ -1,13 +1,7 @@
jetty.port=8080
jetty.host=0.0.0.0
#\u8D44\u6E90classpath\u5730\u5740
velocity.path=template
#\u7F16\u7801
velocity.encoding=UTF-8
#
velocity.velocimacro.library.autoreload = false
#\u6253\u5F00cache\u5F00\u5173
velocity.file.resource.loader.cache = true
#\u6307\u4EE4\u7A7A\u503C
velocity.directive.set.null.allowed = true
#\u8D44\u6E90classpath\u5730\u5740,\u4E0D\u914D\u7F6E\u9ED8\u8BA4\u4E3Atemplate
#velocity.path=template
#\u7F16\u7801\uFF0C\u4E0D\u914D\u7F6E\u9ED8\u8BA4\u4E3AUTF-8
#velocity.encoding=UTF-8

View File

@ -19,23 +19,26 @@ public class VelocityView extends AbstractPathView {
private VelocityEngine engine;
private String templateClasspath;
private String charsetEncoding;
public VelocityView(String dest, VelocityEngine engine) {
public VelocityView(String dest, VelocityEngine engine, String templateClasspath, String charsetEncoding) {
super(dest);
this.engine = engine;
this.templateClasspath = templateClasspath;
this.charsetEncoding = charsetEncoding;
}
@Override
public void render(HttpServletRequest req, HttpServletResponse resp, Object obj) throws Throwable {
String charsetEncoding = getProperty("encoding","UTF-8");
resp.setCharacterEncoding(charsetEncoding);
if (resp.getContentType() == null) {
resp.setContentType("text/html; charset=" + charsetEncoding);
}
try {
String templateUrl = getProperty("path","template")+evalPath(req,obj);
Template template = engine.getTemplate(templateUrl,charsetEncoding);
String templateUrl = templateClasspath + evalPath(req, obj);
Template template = engine.getTemplate(templateUrl, charsetEncoding);
VelocityWebContext webContext = new VelocityWebContext(req, resp);
VelocityContext context = new VelocityContext(webContext);
PrintWriter writer = resp.getWriter();
@ -47,8 +50,8 @@ public class VelocityView extends AbstractPathView {
}
private String getProperty(String key,String defaultValue){
private String getProperty(String key, String defaultValue) {
String v = (String) engine.getProperty(key);
return !Lang.isEmpty(v)?v:defaultValue;
return !Lang.isEmpty(v) ? v : defaultValue;
}
}

View File

@ -25,7 +25,9 @@ public class VelocityViewMakerStarter implements ViewMaker {
@Inject
protected AppContext appContext;
private VelocityEngine engine ;
private VelocityEngine engine;
private String templateClasspath = "template";
private String charsetEncoding = "UTF-8";
public void init() {
if (conf == null) {
@ -33,23 +35,29 @@ public class VelocityViewMakerStarter implements ViewMaker {
}
log.debug("velocity init ....");
engine = new VelocityEngine();
engine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
if (conf.has("velocity.path")) {
templateClasspath = conf.get("velocity.path");
}
if (conf.has("velocity.encoding")) {
charsetEncoding = conf.get("velocity.encoding");
}
engine.setProperty("input.encoding", charsetEncoding);
engine.setProperty("output.encoding", charsetEncoding);
engine.setProperty("resource.loader", "classpath");
engine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
engine.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS,"org.apache.velocity.runtime.log.Log4JLogChute");
engine.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.Log4JLogChute");
engine.setProperty("runtime.log.logsystem.log4j.category", "velocity");
engine.setProperty("runtime.log.logsystem.log4j.logger", "velocity");
for (String key : conf.keySet()) {
if (key.startsWith("velocity.")) {
engine.setProperty(key.substring("velocity.".length()), conf.get(key));
}
}
engine.setProperty("velocimacro.library.autoreload", false);
engine.setProperty("file.resource.loader.cache", true);
engine.setProperty("directive.set.null.allowed", true);
engine.init();
log.debug("velocity init complete");
}
public View make(Ioc ioc, String type, final String value) {
if ("vm".equals(type)) {
return new VelocityView(value,engine);
return new VelocityView(value, engine, templateClasspath, charsetEncoding);
}
return null;
}