velocity是一種java模版引擎技術,mvc架構的一種實現,但它更多的是關注在model和view之間,作為它們的橋梁。服務端渲染,我們使用最多的就是用他來渲染html。下面我們看看他與spring boot的結合。
老樣子,我們看下pom中定義的依賴
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-velocity</artifactid> </dependency> |
spring-boot-starter-velocity 中定義了velocity模板需要的jar。
看下配置類中的配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
package com.shuqi; import org.springframework.boot.autoconfigure.velocity.velocityproperties; import org.springframework.boot.web.servlet.view.velocity.embeddedvelocityviewresolver; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.configuration; /** * * @author linyang * @date 2017/5/9 */ @configuration public class webconfig { @bean public embeddedvelocityviewresolver velocityviewresolver(velocityproperties properties) { embeddedvelocityviewresolver resolver = new embeddedvelocityviewresolver(); properties.applytoviewresolver(resolver); resolver.setredirecthttp10compatible( false ); return resolver; } } |
熟悉spring mvc 的同學都應該知道viewresolver,是告訴spring mvc 怎樣渲染這個視圖,我們這邊使用了velocityviewresolver就是告訴spring mvc 使用velocity的語法來渲染頁面。但是僅有這個還不行,我們還有些配置文件的配置
1
2
3
4
5
6
7
8
9
10
|
# springboot static resources locations spring.mvc. static -path-pattern=/** spring.resources. static -locations=classpath:/web/ static /,classpath:/web/libs/,classpath:/web/views/ # velocity templates (velocityautoconfiguration) spring.velocity.charset=utf- 8 spring.velocity.properties.input.encoding=utf- 8 spring.velocity.properties.output.encoding=utf- 8 spring.velocity.resourceloaderpath=classpath:/web/views/ spring.velocity.suffix=.vm |
里面配置了velocity模板的后綴是.vm,編碼統一使用utf-8,視圖的加載位置,靜態資源的加載位置等。說白了,就是告訴spring mvc,我們的資源文件放到什么位置,然后才能取到,才能渲染。
配置搞定后,我們看下業務代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
package com.shuqi.controller; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; import org.springframework.web.servlet.modelandview; import java.util.hashmap; import java.util.map; @controller public class hellocontroller { @requestmapping (value = "/index" , method = requestmethod.get) public modelandview index() { map<string, string> map = new hashmap<>(); map.put( "name" , "shuqi" ); map.put( "age" , "26" ); return new modelandview( "index" , map); } } |
設置了name與age的值,設置了需要渲染文件的位置及名稱。含義就是:用map中的值,渲染index這個文件。我們最后看一眼,index這個文件的內容
1
2
3
4
5
6
|
<html> <body> <h3>姓名:${name}</h3> <h3>年齡:${age}</h3> </body> </html> |
一段普通的html,只不過有name和age屬性需要渲染。那么執行結果是什么?啟動項目,輸入http://localhost:8080/index,展示頁面
可以看到是一個正常的html。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://www.jianshu.com/p/d0ae1a9e078e