標籤:velocity
classpath資源路徑載入:
velocity.properties檔案配置
file.resource.loader.class = org.apache.velocity.runtime.resource.loader.ClasspathResourceLoaderinput.encoding = UTF-8output.encoding = UTF-8
Servlet代碼
/** * cn.jbit.servlet.ListServlet * 2014-5-8 * gyy */package cn.jbit.servlet;import java.io.IOException;import java.io.InputStream;import java.util.ArrayList;import java.util.List;import java.util.Properties;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.velocity.Template;import org.apache.velocity.app.VelocityEngine;import org.apache.velocity.context.Context;import org.apache.velocity.tools.view.VelocityViewServlet;import cn.jbit.entity.Order;public class ListServlet extends VelocityViewServlet { private static final long serialVersionUID = -2874204863438528905L; @Override protected Template handleRequest(HttpServletRequest request, HttpServletResponse response, Context ctx) { List<Order> orders = new ArrayList<Order>(); Order order = new Order(); order.setItem("Iphone4"); order.setType("智能手機"); order.setPrice("5900"); orders.add(order); order = new Order(); order.setItem("PSP3003"); order.setType("多媒體產品"); order.setPrice("1750"); orders.add(order); order = new Order(); order.setItem("Kinest"); order.setType("體感器"); order.setPrice("1150"); orders.add(order); // 初始化Velocity引擎 VelocityEngine ve = new VelocityEngine(); InputStream is = this.getClass().getResourceAsStream("/velocity.properties"); Properties properties = new Properties(); try { properties.load(is); } catch (IOException e) { e.printStackTrace(); } ve.init(properties); Template template = ve.getTemplate("list.vm"); // 為模版中的元素賦值 ctx.put("orders", orders); return template; }}
velocity模版檔案
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Velocity模版</title></head><body> 您好,親愛的顧客,您的訂單是: <table> <tr> <th>物品</th> <th>各類</th> <th>價格</th> </tr> #foreach($order in $orders) <tr> <td>$order.item</td> <td>$order.type</td> <td>$order.getPrice()</td> </tr> #end </table></body></html>
顯示結果
webapp載入:
velocity.properties檔案配置
resource.loader = webappwebapp.resource.loader.class = org.apache.velocity.tools.view.servlet.WebappLoader webapp.resource.loader.path = /WEB-INF/vm/input.encoding = UTF-8output.encoding = UTF-8
Servlet代碼
/** * cn.jbit.servlet.ListServlet * 2014-5-8 * gyy */package cn.jbit.servlet;import java.util.ArrayList;import java.util.List;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.velocity.Template;import org.apache.velocity.context.Context;import org.apache.velocity.tools.view.VelocityViewServlet;import cn.jbit.entity.Order;public class ListServlet extends VelocityViewServlet {private static final long serialVersionUID = -2874204863438528905L;@Overrideprotected Template handleRequest(HttpServletRequest request,HttpServletResponse response, Context ctx) {List<Order> orders = new ArrayList<Order>();Order order = new Order();order.setItem("Iphone4");order.setType("智能手機");order.setPrice("5900");orders.add(order);order = new Order();order.setItem("PSP3003");order.setType("多媒體產品");order.setPrice("1750");orders.add(order);order = new Order();order.setItem("Kinest");order.setType("體感器");order.setPrice("1150");orders.add(order);// 為模版中的元素賦值ctx.put("orders", orders);return getTemplate("list.vm");}}
web.xml部署檔案配置
好了,結束了,希望對大家有用吧。