Velocity JAVA引擎模板 (九)加载vm文件的三种方式
方式一:加载classpath目录下的vm文件
Properties prop = new Properties();
prop.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
prop.setProperty(Velocity.ENCODING_DEFAULT, "UTF-8");
prop.setProperty(Velocity.INPUT_ENCODING, "UTF-8");
prop.setProperty(Velocity.OUTPUT_ENCODING, "UTF-8");
Velocity.init(prop);
方式二:读取配置文件
Properties properties = new Properties();
properties.load(Test.class.getClassLoader().getResourceAsStream("velocity.properties"));
Velocity.init(properties);
#指定输入编码格式
input.encoding=UTF-8
#自定velocity的servlet向浏览器输出内容的编码
default.contentType=text/html;charset=UTF-8
#指定输出编码格式
output.encoding=UTF-8
方式三:根据绝对路径加载
Properties properties = new Properties();
properties.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, "d://");
Velocity.init(properties);