SpringBoot與velocity的結(jié)合的示例代碼
Velocity是一種Java模版引擎技術(shù),MVC架構(gòu)的一種實現(xiàn),但它更多的是關(guān)注在Model和View之間,作為它們的橋梁。服務(wù)端渲染,我們使用最多的就是用他來渲染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 的同學都應(yīng)該知道ViewResolver,是告訴spring mvc 怎樣渲染這個視圖,我們這邊使用了VelocityViewResolver就是告訴spring mvc 使用Velocity的語法來渲染頁面。但是僅有這個還不行,我們還有些配置文件的配置
# 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)資源的加載位置等。說白了,就是告訴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這個文件。我們最后看一眼,index這個文件的內(nèi)容
<html> <body> <h3>姓名:${name}</h3> <h3>年齡:${age}</h3> </body> </html>
一段普通的HTML,只不過有name和age屬性需要渲染。那么執(zhí)行結(jié)果是什么?啟動項目,輸入http://localhost:8080/index,展示頁面
可以看到是一個正常的HTML。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- 詳解使用Mybatis-plus + velocity模板生成自定義的代碼
- c#基于NVelocity實現(xiàn)代碼生成
- SiteMesh如何結(jié)合Freemarker及velocity使用
- Vue中JS動畫與Velocity.js的結(jié)合使用
- 如何解決SpringBoot2.x版本對Velocity模板不支持的方案
- 聊聊JS動畫庫 Velocity.js的使用
- springMVC+velocity實現(xiàn)仿Datatables局部刷新分頁方法
- 詳解velocity模板使javaWeb的html+js實現(xiàn)模塊化
- Mybatis velocity腳本的使用教程詳解(推薦)
- JAVA velocity模板引擎使用實例
- html文件中jquery與velocity變量中的$沖突的解決方法
- Java 如何使用Velocity引擎生成代碼
相關(guān)文章
SpringBoot @Validated注解實現(xiàn)參數(shù)分組校驗的方法實例
這篇文章主要給大家介紹了關(guān)于SpringBoot @Validated注解實現(xiàn)參數(shù)分組校驗的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-09-09Spring整合Quartz Job以及Spring Task的實現(xiàn)方法
下面小編就為大家分享一篇Spring整合Quartz Job以及Spring Task的實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12