主页 > 知识库 > JAVA velocity模板引擎使用实例

JAVA velocity模板引擎使用实例

热门标签:科大讯飞语音识别系统 Mysql连接数设置 Linux服务器 团购网站 电子围栏 银行业务 服务器配置 阿里云

velocity使用1.7版本。 在win7下使用intelliJ IDEA建立一基于tomcat的web app项目,命名为todo_web,设置path为/todo,导入velocity相关jar包。只导入velocity-1.7.jar这个包可能会报错,根据提示再导入velocity自带的其他包。 项目结构如下:



测试Tomcat

index.jsp内容如下:

复制代码 代码如下:

%-- Created by IntelliJ IDEA. --%>
%@ page contentType="text/html;charset=UTF-8" language="java" %>
html>
  head>
    title>/title>
  /head>
  body>
           %
               out.print("hi,todo");
           %>
  /body>
/html>

HelloWorld.java内容如下:
复制代码 代码如下:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;   
public class HelloWorld extends HttpServlet {
    /**
     *
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     */
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("html>");
        out.println("head>");
        out.println("title>Hi!/title>");
        out.println("/head>");
        out.println("body>");
        out.println("h1>Hello World!!!/h1>");
        out.println("/body>");
        out.println("/html>");
    }
}

在web.xml中加入以下内容:
复制代码 代码如下:

servlet>
    servlet-name>hi/servlet-name>
    servlet-class>HelloWorld/servlet-class>
/servlet>
servlet-mapping>
    servlet-name>hi/servlet-name>
    url-pattern>/hi/url-pattern>
/servlet-mapping>

运行项目,在http://localhost:8080/todo和http://localhost:8080/todo/hi中可以看到效果。


使用velocity

下面开始使用velocity模板引擎,在src下建立目录templates,在templates目录下建立文件test.vm,内容如下:

复制代码 代码如下:

html>
head>
meta http-equiv="Content-Type" content="text/html; charset=utf-8">
/head>
body>
#set( $this = "Velocity")
$this is great!  br/>
$name  br/>
hi  , i am letian
h1>你好/h1>
/body>
/html>

在src目录下新建java文件MyVelocity01.java:
复制代码 代码如下:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.VelocityContext;
import java.util.Properties;
public class MyVelocity01 extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        Properties properties=new Properties();
        properties.setProperty("resource.loader", "class");
        properties.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        //properties.setProperty("input.encoding", "UTF-8");
        //properties.setProperty("output.encoding", "UTF-8");
        properties.setProperty(Velocity.ENCODING_DEFAULT, "UTF-8");
        properties.setProperty(Velocity.INPUT_ENCODING, "UTF-8");
        properties.setProperty(Velocity.OUTPUT_ENCODING, "UTF-8");
        VelocityEngine velocityEngine = new VelocityEngine(properties);
        VelocityContext context=new VelocityContext();
        context.put("name", "test");
        StringWriter sw = new StringWriter();
        velocityEngine.mergeTemplate("templates/test.vm", "utf-8", context, sw);
        //velocityEngine.mergeTemplate("templates/test.vm", "utf-8", context, sw);      //这样就会出现两次
        out.println(sw.toString());
    }
}

配置web.xml:

复制代码 代码如下:

!--MyVelocity-->
servlet>
    servlet-name>ve/servlet-name>
    servlet-class>MyVelocity01/servlet-class>
/servlet>
servlet-mapping>
    servlet-name>ve/servlet-name>
    url-pattern>/ve/url-pattern>
/servlet-mapping>

重新部署,浏览器访问http://localhost:8080/todo/ve可以看到效果。

简单介绍velocity

velocity是一个基于java的模板引擎,有三种文件加载模板方式: 1、从文件路径加载 2、从类路径(MyVelocity01.java使用该方法) 3、从jar文件加载 开始接触velocity时可能会在加载模板上遇到问题。

如何向模板文件传递变量: 模板本身可以定义变量,例如在test.vm中定义了变量$this,java代码也可以给模板传递变量,例如test.vm中的变量$name便是VelocityContext实例传递过去的。同时velocity也支持迭代对象,例如: 我们在MyVelocity01.java中导入java.util.Vector,将代码:

复制代码 代码如下:

context.put("name", "test");

改为:
复制代码 代码如下:

Vector v = new Vector(); 
v.addElement("Harry"); 
v.addElement("John"); 
String[] names = {"Harry", "John"};
context.put("names1", v);
context.put("names2", names);

将test.vm内容改为:
复制代码 代码如下:

h1>hello/h1>
#foreach($name in $names1)
    $name   br/>
#end
#foreach($name in $names2)
    $name   br/>
#end

velocity还支持map容器,支持使用#include("")引入静态模板,#parse("模板名")引入动态模板。

如果想不开要用java MVC写网站的话,使用servlet + velocity是一个小巧灵活的选择。

您可能感兴趣的文章:
  • 详解使用Mybatis-plus + velocity模板生成自定义的代码
  • c#基于NVelocity实现代码生成
  • SiteMesh如何结合Freemarker及velocity使用
  • Vue中JS动画与Velocity.js的结合使用
  • 如何解决SpringBoot2.x版本对Velocity模板不支持的方案
  • SpringBoot与velocity的结合的示例代码
  • 聊聊JS动画库 Velocity.js的使用
  • springMVC+velocity实现仿Datatables局部刷新分页方法
  • 详解velocity模板使javaWeb的html+js实现模块化
  • Mybatis velocity脚本的使用教程详解(推荐)
  • html文件中jquery与velocity变量中的$冲突的解决方法
  • Java 如何使用Velocity引擎生成代码

标签:衢州 大理 衡水 广元 江苏 枣庄 萍乡 蚌埠

巨人网络通讯声明:本文标题《JAVA velocity模板引擎使用实例》,本文关键词  ;如发现本文内容存在版权问题,烦请提供相关信息告之我们,我们将及时沟通与处理。本站内容系统采集于网络,涉及言论、版权与本站无关。
  • 相关文章
  • 收缩
    • 微信客服
    • 微信二维码
    • 电话咨询

    • 400-1100-266