首页 > 代码库 > Velocity模版加载(classpath、webapp)

Velocity模版加载(classpath、webapp)

classpath资源路径加载:


velocity.properties文件配置

file.resource.loader.class = 
org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
input.encoding = UTF-8
output.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 = 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-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;

	@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);
		
		// 为模版中的元素赋值
		ctx.put("orders", orders);
		return getTemplate("list.vm");
	}
}

web.xml部署文件配置

好了,结束了,希望对大家有用吧。