当前位置: 移动技术网 > IT编程>开发语言>Java > SpringBoot与velocity的结合的示例代码

SpringBoot与velocity的结合的示例代码

2019年07月19日  | 移动技术网IT编程  | 我要评论

velocity是一种java模版引擎技术,mvc架构的一种实现,但它更多的是关注在model和view之间,作为它们的桥梁。服务端渲染,我们使用最多的就是用他来渲染html。下面我们看看他与spring boot的结合。

老样子,我们看下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,编码统一使用utf-8,视图的加载位置,静态资源的加载位置等。说白了,就是告诉spring mvc,我们的资源文件放到什么位置,然后才能取到,才能渲染。

配置搞定后,我们看下业务代码

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这个文件的内容

<html>
  <body>
   <h3>姓名:${name}</h3>
   <h3>年龄:${age}</h3>
  </body>
</html>

一段普通的html,只不过有name和age属性需要渲染。那么执行结果是什么?启动项目,输入http://www.lhsxpumps.com/_localhost:8080/index,展示页面


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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网