一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - SpringBoot與velocity的結合的示例代碼

SpringBoot與velocity的結合的示例代碼

2021-04-15 11:28數齊 Java教程

本篇文章主要介紹了SpringBoot與velocity的結合的示例代碼,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

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,展示頁面

SpringBoot與velocity的結合的示例代碼

可以看到是一個正常的html。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://www.jianshu.com/p/d0ae1a9e078e

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 爱情岛论坛亚洲自拍 | 久久机热视频 这里只有精品首页 | 日本一区二区免费在线 | 我要看靠逼片 | 欧美亚洲国产综合在线 | 天天爽视频 | 亚洲欧美日韩国产一区二区精品 | 逼逼狗影院 | 色中色破解版 | videos护士有奶水 | 射18p| 午夜国产理论 | 好大用力深一点女公交车 | 97久久精品午夜一区二区 | 99只有精品 | 高清视频在线播放 | 公妇乱淫在线播放免费观看 | 国产专区视频在线观看 | 国产免费美女视频 | 黄 在线播放| 国产aaa伦理片 | 波多野结衣在线观看中文字幕 | 日本加勒比一区 | 骚虎网站在线观看 | 亚洲国产精品久久久久久网站 | 色综合欧美色综合七久久 | 四虎影院在线免费播放 | 亚欧有色在线观看免费版高清 | 青青成人福利国产在线视频 | 欧美精品久久久亚洲 | 60岁了天天要小伙子 | 91精品综合久久久久久五月天 | 狠狠色狠狠色综合婷婷tag | 日本黄a三级三级三级 | 操bb| 99精品国产高清自在线看超 | 关晓彤被调教出奶水 | 无耻之徒第十一季在线观看 | 国产色综合久久五月色婷婷中文 | 精品国产一区二区三区国产馆 | 91制片厂制作传媒网站破解 |