Velocity template Loading (classpath, WebApp)

Source: Internet
Author: User

Classpath Resource Path loading:


Velocity.properties file Configuration

File.resource.loader.class = Org.apache.velocity.runtime.resource.loader.ClasspathResourceLoaderinput.encoding = utf-8output.encoding = UTF-8
Servlet code

/** * 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> ();   &NBSP;&NBSP;&NBSp          order order = New Order ();         Order.setitem ("Iphone4");        order.settype ("Smartphone");         order.setprice ("5900");        orders.add (order);         order = New Order ();        order.setitem ("PSP3003");        order.settype ("Multimedia Products");        order.setprice ("1750");         orders.add (order);        order = New Order ();         order.setitem ("Kinest");        order.settype ( "Body sensor");        order.setprice ("1150");         Orders.add (order);        //initialize velocity engine          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") ;        //Assign values to elements in the template         ctx.put ("Orders", orders);         return template;    }}
Velocity template File

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
Show results


WebApp Loading:


Velocity.properties file Configuration

Servlet code

/** * 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 ("smartphone"); Order.setprice (" 5900 "), Orders.add (order), Order = New Order (), Order.setitem (" PSP3003 "); Order.settype (" Multimedia Product "); Order.setprice (" 1750 Orders.add (order), Order = New Order (), Order.setitem ("Kinest"), Order.settype ("Body Sensor"), Order.setprice ("1150"); O Rders.add (Order);//Assign Value Ctx.put ("Orders", orders) to the elements in the template, return gettemplate ("LIST.VM");}} 

Web. XML deployment File Configuration

Well, it's over, I hope it works for everyone.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.