您的位置:首页 > 编程语言 > Java开发

spring mvc +velocity

2016-05-19 14:21 316 查看
项目中引入jar包:



applicationcontext.xml 配置

<!-- 配置velocity引擎 -->
<bean id="velocityConfigurer"
class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath" value="/WEB-INF/templates/" /><!-- 模板存放的路径 -->
<property name="configLocation" value="classpath:velocity.properties" />
</bean>

<!-- 配置视图的显示 -->
<bean id="ViewResolver" class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver">
<property name="prefix" value="/" /><!-- 视图文件的前缀,即存放的路径 -->
<property name="suffix" value=".vm" /><!-- 视图文件的后缀名 -->
<property name="toolboxConfigLocation" value="/WEB-INF/tools.xml" /><!--toolbox配置文件路径-->
<property name="dateToolAttribute" value="date" /><!--日期函数名称-->
<property name="numberToolAttribute" value="number" /><!--数字函数名称-->
<property name="contentType" value="text/html;charset=UTF-8" />
<property name="exposeSpringMacroHelpers" value="true" /><!--是否使用spring对宏定义的支持-->
<property name="exposeRequestAttributes" value="true" /><!--是否开放request属性-->
<property name="requestContextAttribute" value="rc"/><!--request属性引用名称-->
<property name="layoutUrl" value="layout/default.vm"/><!--指定layout文件-->
</bean>
tool.xml和velocity.properties 两个文件,在jar包中都可以找到的

velocity.jar velocity.properties -->> org.apache.velocity.runtime.defaults.velocity.properties

velocity-tool.jar tools.xml -->> org\apache\velocity\tools\generic\tools.xml 记得改下标签。

velocity.properties要改一下
velocimacro.library = /macros.vm

input.encoding=UTF-8
output.encoding=UTF-8


macros.vm这个随你自己想不想要,反正默认也是会加载好多spring的宏的。编码是一定要设置的。

明明已经都配置好了,自己都觉得没问题了,action都进了,最后跳到模板引擎去加载模板的时候,告诉你这个模板找不到!!

经过仔细排查,是velocity.properties多了句配置,自己需要注释一下。
#file.resource.loader.path = .
将这句配置注释,这是说,路径为properties文件路径,但如果你的properties跟你的templates不在一个目录,这时候就会出问题了。我调进去看过,它默认就是templates目录,上面宏的地址,也是相对于templates的目录。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: