SpringBoot與velocity的結(jié)合的示例代碼

Velocity是一種Java模版引擎技術(shù),MVC架構(gòu)的一種實現(xiàn),但它更多的是關(guān)注在Model和View之間,作為它們的橋梁。服務端渲染,我們使用最多的就是用他來渲染HTML。下面我們看看他與spring boot的結(jié)合。

陽新網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)公司,陽新網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為陽新近1000家提供企業(yè)網(wǎng)站建設(shè)服務。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)要多少錢,請找那個售后服務好的陽新做網(wǎng)站的公司定做!

老樣子,我們看下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 的同學都應該知道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è)務代碼

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>
   <h4>姓名:${name}</h4>
   <h4>年齡:${age}</h4>
  </body>
</html>

一段普通的HTML,只不過有name和age屬性需要渲染。那么執(zhí)行結(jié)果是什么?啟動項目,輸入http://localhost:8080/index,展示頁面

SpringBoot與velocity的結(jié)合的示例代碼

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

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

本文標題:SpringBoot與velocity的結(jié)合的示例代碼
當前URL:http://bm7419.com/article34/gijipe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)、網(wǎng)站導航、營銷型網(wǎng)站建設(shè)網(wǎng)站維護、微信公眾號、外貿(mào)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)