Velocity is a Java template engine technology, an implementation of MVC architecture, but it focuses more on the bridge between Model and View as their bridge. For server-side rendering, the most we use is to render HTML. Let's take a look at his combination with spring boot.
As usual, let's look at the dependencies defined in the 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>
The jar required for the velocity template is defined in spring-boot-starter-velocity.
Check out the configuration in the configuration class
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 */@Configurationpublic class WebConfig { @Bean public EmbeddedVelocityViewResolver velocityViewResolver(VelocityProperties properties) { EmbeddedVelocityViewResolver resolver = new EmbeddedVelocityViewResolver(); properties.applyToViewResolver(resolver); resolver.setRedirectHttp10Compatible(false); return resolver; }}Students who are familiar with spring mvc should know ViewResolver, which tells spring mvc how to render this view. We use VelocityViewResolver, which tells spring mvc to use Velocity syntax to render the page. But this alone is not enough, we still have some configuration files
# SpringBoot static resources locationsspring.mvc.static-path-pattern=/**spring.resources.static-locations=classpath:/web/static/,classpath:/web/libs/,classpath:/web/views/# VELOCITY TEMPLATES (VelocityAutoConfiguration)spring.velocity.charset=UTF-8spring.velocity.properties.input.encoding=UTF-8spring.velocity.properties.output.encoding=UTF-8spring.velocity.resourceLoaderPath=classpath:/web/views/spring.velocity.suffix=.vm
The suffix of the velocity template is .vm, and the encoding is uniformly used by UTF-8, the view loading position, the static resource loading position, etc. To put it bluntly, it is to tell spring mvc where our resource file can be placed, and then it can be retrieved and rendered.
After the configuration is completed, let's look at the business code
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;@Controllerpublic 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); }}Set the value of name and age, and set the location and name of the file to be rendered. The meaning is: use the value in the map to render the index file. Let's take a last look at the contents of the index file
<html> <body> <h3>Name:${name}</h3> <h3>Age:${age}</h3> </body></html> A normal HTML, but with name and age attributes that need to be rendered. So what is the execution result? Start the project, enter http://localhost:8080/index to display the page
It can be seen that it is a normal HTML.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.