I've learned one template rendering technology before, Thymleaf. Today I'm going to talk about another template rendering technology, Velocity!
Knowledge Point Preview:
- Encapsulating various properties of Velocity with Properties
- Velocity Engine instantiates template engine objects based on Properties encapsulation
- Instantiate VelocityContext and encapsulate rendering data in it
- Velocity Engine renders contextual data to a specified template and writes rendered page data to StringWriter
- Response page data in StringWriter to the front end through the output stream of HttpServletResponse
Here is a case study.
Case presentation
1. Test preparation
Project engineering (webapp project in maven)
2. Code display
***index.jsp
<%-- Created by IntelliJ IDEA. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title></title> </head> <body> <% out.print("Hello,EveryBody!"); %> </body> </html>
***pom.xml
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>servlet-api</artifactId> <version>6.0.37</version> </dependency> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity</artifactId> <version>1.7</version> </dependency> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity-tools</artifactId> <version>2.0</version> </dependency> </dependencies>
***test.vm
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> </head> <body> ##String variables <fieldset> $article </fieldset> ##list set <fieldset> #foreach($area in $areas) $area <br/> #end </fieldset> ##array <fieldset> #foreach($name in $names) $name <br/> #end </fieldset> ##map set <fieldset> #foreach($person in $personMap.entrySet()) ${person.key} ::: ${person.value} <br/> #end </fieldset> </body> </html>
***VelocityServlet.java
package com.howie; import org.apache.velocity.VelocityContext; import org.apache.velocity.app.Velocity; import org.apache.velocity.app.VelocityEngine; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; import java.util.*; /** * @Author weihuanwen * @Date 2019/8/23 7:30 * @Version 1.0 */ @WebServlet("/vs") public class VelocityServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("UTF-8"); resp.setContentType("text/html;charset=UTF-8"); //Get the output stream PrintWriter out = resp.getWriter(); //Setting the velocity attribute Properties prop=new Properties(); //Set velocity resource loading mode to class prop.setProperty("resource.loader", "class"); //Processing class when setting velocity resource loading mode to class prop.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); //Setting default encoding format for templates prop.setProperty(Velocity.ENCODING_DEFAULT, "UTF-8"); //Input encoding format for template settings prop.setProperty(Velocity.INPUT_ENCODING, "UTF-8"); //Output encoding format for template settings prop.setProperty(Velocity.OUTPUT_ENCODING, "UTF-8"); //Instantiate a VelocityEngine object VelocityEngine velocityEngine = new VelocityEngine(prop); //Getting Velocity Context VelocityContext context=new VelocityContext(); String article = "how to learn velocity?"; context.put("article",article); //list set List<String> areas = new ArrayList<>(); areas.add("Xie"); areas.add("Xie"); context.put("areas",areas); //array String[] names = {"Jhon","Lily"}; context.put("names",names); //map set Map<String,Integer> personMap = new HashMap<>(); personMap.put("Jhon",20); personMap.put("Lily",26); context.put("personMap", personMap); //Collect the output character stream in the string buffer. It can be used to construct a string. Closing the stream is invalid. Calling other methods after closing will not report an exception. StringWriter sw = new StringWriter(); //Loading vm templates under specified paths velocityEngine.mergeTemplate("templates/test.vm", "utf-8", context, sw); out.println(sw.toString()); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req,resp); } }
3. test
Start the project, the page is shown as follows:
(2) Access request address: http://localhost:8080/vs The page is shown as follows: