classpath資源路徑加載:
velocity.properties文件配置<喎?http://www.Bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+PC9wPgo8cHJlIGNsYXNzPQ=="brush:java;">file.resource.loader.class = org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader input.encoding = UTF-8 output.encoding = UTF-8Servlet代碼
/** * 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) { Listvelocity模版文件orders = new ArrayList (); 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模版 您好,親愛的顧客,您的訂單是:物品 各類 價格 #foreach($order in $orders) $order.item $order.type $order.getPrice() #end
webapp加載:
velocity.properties文件配置
resource.loader = webapp webapp.resource.loader.class = org.apache.velocity.tools.view.servlet.WebappLoader webapp.resource.loader.path = /WEB-INF/vm/ input.encoding = UTF-8 output.encoding = UTF-8Servlet代碼
/** * 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; @Override protected Template handleRequest(HttpServletRequest request, HttpServletResponse response, Context ctx) { Listorders = new ArrayList (); 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部署文件配置
好了,結束了,希望對大家有用吧。