Velocity JAVA引擎模板 (九)加载vm文件的三种方式

x33g5p2x  于2021-12-24 转载在 其他  
字(0.9k)|赞(0)|评价(0)|浏览(332)

方式一:加载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);
  • velocity.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);
上一篇: (八)宏

相关文章