1. 程式人生 > >關於spring boot使用velocity作為檢視層模板

關於spring boot使用velocity作為檢視層模板

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,嘗試啟動專案後,發現一切正常,問題已解決。