1.什么是Velocity

一种J2EE的前端模版技术,和JSP,Freemarker差不多,都是用来展示网页内容的。和JSP不同的是velocity只能显示Action中的数据,不能处理数据。不能写java代码,但是可以使用Velocity标记。也就是说把显示代码和后端的JAVA代码分离开来,降低程序的耦合性

2.需要引入哪些Jar包

velocity-1.5.jar,velocity-1.6.2.jar,velocity-tools-2.0.jar,velocity-tools-generic-2.0.jar,velocity-tools-view-2.0.jar

3.编写模板.vm文件

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<!-- 页面对变量赋值 -->
#set( $current = "Velocity22")
<!-- 输出 -->
$current is great! <br/>
<!-- 输出后台context设置的参数 -->
$name <br/> <!--String循环-->
#foreach( $elem in $arrList)
$elem</br>
#end <!--对象循环-->
#foreach( $elem in $userList)
名字:$elem.name 性别:$elem.sex 地址:$elem.address</br>
#end
</body>
</html>

4.编写servelet文件

package com.babybus.sdteam.servelet;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Properties; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException; import com.babybus.sdteam.vo.User; public class VelocityTemplateServelt extends HttpServlet { private static final long serialVersionUID = 1L; @Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException { // 设置request 和 response 编码,放置乱码
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8"); // 获取页面输出流
PrintWriter out = response.getWriter(); // 创建Properties文件,也可以直接在目录下创建
Properties properties=new Properties();
properties.setProperty("resource.loader", "class");
properties.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
properties.setProperty(Velocity.ENCODING_DEFAULT, "UTF-8");
properties.setProperty(Velocity.INPUT_ENCODING, "UTF-8");
properties.setProperty(Velocity.OUTPUT_ENCODING, "UTF-8"); // 创建模板引擎
VelocityEngine velocityEngine = null;
try {
velocityEngine = new VelocityEngine(properties);
} catch (Exception e) {
e.printStackTrace();
} // 创建上下文, 用于存放变量
VelocityContext context=new VelocityContext();
context.put("name", "test"); // List(String)
ArrayList<String> arrList = new ArrayList<String>();
arrList.add("test01");
arrList.add("test02");
arrList.add("test03");
context.put("arrList", arrList); // UserList(存放对象List)
ArrayList<User> userList = new ArrayList<User>();
userList.add(new User("蔡大三", "男", "南安一中五条巷子"));
userList.add(new User("马大哈", "男", "红灯区"));
userList.add(new User("林超", "女", "下三路"));
context.put("userList", userList); // 读取模板文件流
StringWriter sw = new StringWriter();
try {
velocityEngine.mergeTemplate("templates/example.vm", "utf-8", context, sw);
} catch (ResourceNotFoundException e) {
e.printStackTrace();
} catch (ParseErrorException e) {
e.printStackTrace();
} catch (MethodInvocationException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} // 输出到页面
out.println(sw.toString());
}
}

5.配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name> <!--MyVelocity-->
<servlet>
<servlet-name>ve</servlet-name>
<servlet-class>com.babybus.sdteam.servelet.VelocityTemplateServelt</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ve</servlet-name>
<url-pattern>/ve</url-pattern>
</servlet-mapping> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

6.输入地址:http://localhost:8080/velocitydemo/ve启动项目

本站文章为宝宝巴士 SD.Team原创,转载务必在明显处注明:(作者官方网站:宝宝巴士)

转载自【宝宝巴士SuperDo团队】 原文链接: http://www.cnblogs.com/superdo/p/4827097.html

[JavaWeb基础] 020.Velocity 模板引擎简单示例的更多相关文章

  1. 转 如何使用velocity模板引擎开发网站

    基于 Java 的网站开发,很多人都采用 JSP 作为前端网页制作的技术,尤其在是国内.这种技术通常有一些问题,我试想一下我们是怎样开发网站的,通常有几种方法: 1:功能确定后,由美工设计网页的UI( ...

  2. Velocity模板引擎介绍

    整理下Velocity使用方法,整理比较详细用例 1 Velocity基础语法 1.1 用户和开发人员参考文档 http://velocity.apache.org/engine/releases/v ...

  3. velocity模板引擎学习(4)-在standalone的java application中使用velocity及velocity-tools

    通常velocity是配合spring mvc之类的框架在web中使用,但velocity本身其实对运行环境没有过多的限制,在单独的java application中也可以独立使用,下面演示了利用ve ...

  4. Velocity模板引擎语法

    Velocity 模板引擎介绍 Velocity是一个基于java的模板引擎(template engine).它允许任何人仅仅简单的使用模板语言(template language)来引用由java ...

  5. Velocity模板引擎入门

    类似于PHP中的Smarty,Velocity是一个基于Java的模板引擎(template engine).它允许任何人仅仅简单的使用模板语言(template language)来引用由java代 ...

  6. 【转载】Velocity模板引擎的介绍和基本的模板语言语法使用

    原文地址http://www.itzhai.com/the-introduction-of-the-velocity-template-engine-template-language-syntax- ...

  7. 使用 Velocity 模板引擎快速生成代码(zhuan)

    http://www.ibm.com/developerworks/cn/java/j-lo-velocity1/ ****************************************** ...

  8. velocity模板引擎学习(3)-异常处理

    按上回继续,前面写过一篇Spring MVC下的异常处理.及Spring MVC下的ajax异常处理,今天看下换成velocity模板引擎后,如何处理异常页面: 一.404错误.500错误 <e ...

  9. 使用Velocity 模板引擎快速生成代码

    Velocity 模板引擎介绍 在现今的软件开发过程中,软件开发人员将更多的精力投入在了重复的相似劳动中.特别是在如今特别流行的MVC架构模式中,软件各个层次的功能更加独立,同时代码的相似度也更加高. ...

随机推荐

  1. [bzoj1924]P2403 [SDOI2010]所驼门王的宝藏

    tarjan+DAG 上的 dp 难点在于建图和连边,其实也不难,就是细节挺恶心 我和正解对拍拍出来 3 个错误... 传送门:luogu bzoj 题目描述 有座宫殿呈矩阵状,由 \(R\times ...

  2. 【WPF学习】第六十八章 自定义绘图元素

    上一章分析了WPF元素的内部工作元素——允许每个元素插入到WPF布局系统的MeasureOverride()和ArrangeOverride()方法中.本章将进一步深入分析和研究元素如何渲染自身. 大 ...

  3. 关于Cookie的一点简单认识

    1.Cookie Cookies是服务器在本地机器上存储的小段文本并随每一个请求发送至同一服务器,是在客户端保持状态的方案.通常每个 Cookie 的大小不能超过4KB.客户端每次向服务器发出请求,就 ...

  4. 【Hadoop离线基础总结】MapReduce参数优化

    MapReduce参数优化 资源相关参数 这些参数都需要在mapred-site.xml中配置 mapreduce.map.memory.mb 一个 MapTask 可使用的资源上限(单位:MB),默 ...

  5. Adobe Reader XI 打开后“已停止工作”的解决办法

    搜了好多方法按照步骤做完,基本无用,试了以下方法搞定. 具体方法是: 把域名解析到本机. 打开 C:\Windows\System32\drivers\etc\hosts 添加 127.0.0.1 a ...

  6. QTableWidget自定义委托

    QTableWidget单元格使用自定义的lineEdit控件导致不能选中 使用自定义委托解决 1.自定义委托 class LineEditDelegate : public QItemDelegat ...

  7. Ubuntu系统make menuconfig的依赖包ncurses安装

    Linux内核或者u-boot进行make menuconfig的时候,如果系统上没有安装ncurses,就会出现以下报错 *** Unable to find the ncurses librari ...

  8. quartus ii FFT核使用

    quartus ii FFT核使用 导入自己程序自带的txt文件,写出控制模块 时序图 FFT核文件给出的时序图输入 仿真时序图 1024个采样点数,输入结束 fft数据输出 2.代码 `timesc ...

  9. [NBUT 1458 Teemo]区间第k大问题,划分树

    裸的区间第k大问题,划分树搞起. #pragma comment(linker, "/STACK:10240000") #include <map> #include ...

  10. C++内存管理学习笔记(1)

    /****************************************************************/ /*            学习是合作和分享式的! /* Auth ...