关于spring boot使用velocity作为视图层模板

时间:2022-11-24 17:57:18

1、maven配置

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
</parent>


<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- spring boot 对velocity的支持只到1.4.x -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-velocity</artifactId>
</dependency>
</dependencies>


2、application.properties配置

spring.velocity.cache= true
spring.velocity.charset=UTF-8
spring.velocity.check-template-location=true
spring.velocity.content-type=text/html
spring.velocity.enabled=true
spring.velocity.resource-loader-path=/WEB-INF/views
spring.velocity.prefix=/
spring.velocity.suffix=.vm
spring.velocity.toolbox-config-location=/WEB-INF/toolbox.xml


3、controller相关代码

@Controller
@RequestMapping
public class IndexController {


@RequestMapping("/index")
public String index() {
return "index";
}
}


注意:在我使用1.4.7版本的spring boot时,始终无法将controller的index方法返回的“index”作为视图名称去解析,而是将其用作url地址跳转,最终抛出异常:

javax.servlet.ServletException: Circular view path [index]: would dispatch back to the current handler URL [/demo/index] again. Check your ViewResolver setup! (Hint:This may be the result of an unspecified view, due to default view name generation.)

网上查阅了一些资料后,我怀疑是spring boot的版本问题导致该问题的发生。于是将spring boot版本降级至1.4.2,尝试启动项目后,发现一切正常,问题已解决。