SpringBoot與velocity的結(jié)合的示例代碼
Velocity是一種Java模版引擎技術(shù),MVC架構(gòu)的一種實(shí)現(xiàn),但它更多的是關(guān)注在Model和View之間,作為它們的橋梁。服務(wù)端渲染,我們使用最多的就是用他來(lái)渲染HTML。下面我們看看他與spring boot的結(jié)合。
老樣子,我們看下pom中定義的依賴
<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。
看下配置類中的配置
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 的同學(xué)都應(yīng)該知道ViewResolver,是告訴spring mvc 怎樣渲染這個(gè)視圖,我們這邊使用了VelocityViewResolver就是告訴spring mvc 使用Velocity的語(yǔ)法來(lái)渲染頁(yè)面。但是僅有這個(gè)還不行,我們還有些配置文件的配置
# 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,編碼統(tǒng)一使用UTF-8,視圖的加載位置,靜態(tài)資源的加載位置等。說(shuō)白了,就是告訴spring mvc,我們的資源文件放到什么位置,然后才能取到,才能渲染。
配置搞定后,我們看下業(yè)務(wù)代碼
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); } }
設(shè)置了name與age的值,設(shè)置了需要渲染文件的位置及名稱。含義就是:用map中的值,渲染index這個(gè)文件。我們最后看一眼,index這個(gè)文件的內(nèi)容
<html> <body> <h3>姓名:${name}</h3> <h3>年齡:${age}</h3> </body> </html>
一段普通的HTML,只不過(guò)有name和age屬性需要渲染。那么執(zhí)行結(jié)果是什么?啟動(dòng)項(xiàng)目,輸入http://localhost:8080/index,展示頁(yè)面
可以看到是一個(gè)正常的HTML。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 詳解使用Mybatis-plus + velocity模板生成自定義的代碼
- c#基于NVelocity實(shí)現(xiàn)代碼生成
- SiteMesh如何結(jié)合Freemarker及velocity使用
- Vue中JS動(dòng)畫(huà)與Velocity.js的結(jié)合使用
- 如何解決SpringBoot2.x版本對(duì)Velocity模板不支持的方案
- 聊聊JS動(dòng)畫(huà)庫(kù) Velocity.js的使用
- springMVC+velocity實(shí)現(xiàn)仿Datatables局部刷新分頁(yè)方法
- 詳解velocity模板使javaWeb的html+js實(shí)現(xiàn)模塊化
- Mybatis velocity腳本的使用教程詳解(推薦)
- JAVA velocity模板引擎使用實(shí)例
- html文件中jquery與velocity變量中的$沖突的解決方法
- Java 如何使用Velocity引擎生成代碼
相關(guān)文章
SpringBoot @Validated注解實(shí)現(xiàn)參數(shù)分組校驗(yàn)的方法實(shí)例
這篇文章主要給大家介紹了關(guān)于SpringBoot @Validated注解實(shí)現(xiàn)參數(shù)分組校驗(yàn)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09java實(shí)現(xiàn)掃雷游戲控制臺(tái)版
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)掃雷游戲控制臺(tái)版,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04SpringMVC深入講解文件的上傳下載實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了springMVC實(shí)現(xiàn)文件上傳和下載的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06spring boot補(bǔ)習(xí)系列之幾種scope詳解
這篇文章主要給大家介紹了關(guān)于spring boot補(bǔ)習(xí)系列之幾種scope的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用spring boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07Spring boot動(dòng)態(tài)修改日志級(jí)別的方法
我們經(jīng)常會(huì)遇到業(yè)務(wù)想看debug日志的問(wèn)題,但是debug日志頻繁打印會(huì)對(duì)日志查看有影響,且日志多對(duì)系統(tǒng)也會(huì)有一定的壓力,因此,如果可以在需要的時(shí)候動(dòng)態(tài)臨時(shí)調(diào)整下日志的級(jí)別則是比較完美的,spring boot已經(jīng)支持這種功能,需要的朋友可以參考下2022-12-12Spring整合Quartz Job以及Spring Task的實(shí)現(xiàn)方法
下面小編就為大家分享一篇Spring整合Quartz Job以及Spring Task的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12