程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> Velocity模版加載(classpath、webapp)

Velocity模版加載(classpath、webapp)

編輯:C++入門知識

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) {
        List 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模版文件





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

web.xml部署文件配置

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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved