NutzCN Logo
问答 关于集成velocity提示 模板引擎错误
发布于 3053天前 作者 backbp 3468 次浏览 复制 上一个帖子 下一个帖子
标签: velocity

当我把模板testItem.vm放在根目录(WebRoot)下时,返回写的是:@Ok("vm:/testItem")
会提示一下错误

DEBUG - Path::/testItem
ERROR - 模板引擎错误
Unable to find resource '/testItem'

然后,我把 testItem.vm 移动到:/WEB-INF/templates/velocity/testItem.vm,返回写的是:@Ok("vm:templates.velocity.testItem")
还是会提示错误:

DEBUG - Path::templates.velocity.testItem
ERROR - 模板引擎错误
org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource 'templates.velocity.testItem'
8 回复

@wendal 对,官网的文档里面是使用maven来下载的,因为项目不是用maven,所以我就自己下载下来了jar包,是1.b.52的版本

我试试, 老久没用过这个插件了

首先是配置文件 velocity.properties


input.encoding=UTF-8 output.encoding=UTF-8 resource.loader=webapp webapp.resource.loader.class=org.apache.velocity.tools.view.WebappResourceLoader webapp.resource.loader.path=/WEB-INF/templates/velocity/

然后是入口方法

	@Ok("vm:index.vm")
	@At
	public Context index() {
		return Lang.context().set("name", "wendal");
	}

引擎初始化代码

try {
			InputStream ins = getClass().getClassLoader().getResourceAsStream("velocity.properties");
			Properties props = new Properties();
			props.load(ins);
			ins.close();
			Velocity.setApplicationAttribute("javax.servlet.ServletContext", nc.getServletContext());
			Velocity.init(props);
		} catch (IOException e) {
			e.printStackTrace();
		}

模板位于 /WEB-INF/templates/velocity/index.vm 测试可用

看着有点繁琐, 我找个时间整理一下

@wendal 搞定,谢谢啦,是引擎初始化的问题,非常感谢

添加回复
请先登陆
回到顶部