Velocity模版載入(classpath、webapp)

來源:互聯網
上載者:User

標籤: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部署檔案配置

好了,結束了,希望對大家有用吧。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.