JWeb之Velocity模板渲染

之前学习过一种模板渲染技术是Thymleaf,今天讲解下另一种模板渲染技术Velocity!

知识点预习:

  • 使用Properties封装Velocity的各种属性
  • VelocityEngine根据Properties封装属性实例化模板引擎对象
  • 实例化VelocityContext并在其中封装渲染数据
  • VelocityEngine将上下文中的数据渲染至指定的模板中并将渲染后的页面数据写入StringWriter
  • 通过HttpServletResponse的输出流将StringWriter中页面数据响应至前端

下面通过案例来展示!

一.案例展示

1.测试准备

项目工程(maven中选择webapp工程)

JWeb之Velocity模板渲染_第1张图片

2.代码展示

***index.jsp

<%-- Created by IntelliJ IDEA. --%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    


<%
    out.print("Hello,EveryBody!");
%>

***pom.xml

 
    
      junit
      junit
      4.12
      test
    
    
      org.apache.tomcat
      servlet-api
      6.0.37
    
    
      org.apache.velocity
      velocity
      1.7
    
    
      org.apache.velocity
      velocity-tools
      2.0
    
  

***test.vm



    


    ##字符串变量
    
$article
##list集合
#foreach($area in $areas) $area
#end
##数组
#foreach($name in $names) $name
#end
##map集合
#foreach($person in $personMap.entrySet()) ${person.key} ::: ${person.value}
#end

***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");
        //获取输出流
        PrintWriter out = resp.getWriter();
        //设定velocity属性
        Properties prop=new Properties();
        //设置velocity资源加载方式为class
        prop.setProperty("resource.loader", "class");
        //设置velocity资源加载方式为class时的处理类
        prop.setProperty("class.resource.loader.class",
                "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        //设置模板的默认编码格式
        prop.setProperty(Velocity.ENCODING_DEFAULT, "UTF-8");
        //设置模板的输入编码格式
        prop.setProperty(Velocity.INPUT_ENCODING, "UTF-8");
        //设置模板的输出编码格式
        prop.setProperty(Velocity.OUTPUT_ENCODING, "UTF-8");
        //实例化一个VelocityEngine对象
        VelocityEngine velocityEngine = new VelocityEngine(prop);
        //获取Velocity上下文
        VelocityContext context=new VelocityContext();
        String article = "how to learn velocity?";
        context.put("article",article);
        //list集合
        List areas = new ArrayList<>();
        areas.add("北京");
        areas.add("上海");
        context.put("areas",areas);
        //数组
        String[] names = {"Jhon","Lily"};
        context.put("names",names);
        //map集合
        Map personMap = new HashMap<>();
        personMap.put("Jhon",20);
        personMap.put("Lily",26);
        context.put("personMap", personMap);
        //在字符串缓冲区中收集输出的字符流,可用于构造字符串, 关闭流无效,关闭后调用其他方法不会报异常
        StringWriter sw = new StringWriter();
        //加载指定路径下的vm模板
        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.测试

①启动项目,页面展示如下:

JWeb之Velocity模板渲染_第2张图片

②访问请求地址:http://localhost:8080/vs,页面展示如下:

JWeb之Velocity模板渲染_第3张图片

 

你可能感兴趣的:(Java)