1.首先下载velocity .下载地址 http://velocity.apache.org/

2.下载tomcat 6.0 (windows Service installer),安装在D:\Program Files\Apache Software Foundation\

3.在MyEclipse 里创建一个WebSite vtl。默认的website的文件夹是WebRoot.

在WebRoot里添加下面testVtlclass

package com.cl.vtl;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
public class testVtl extends HttpServlet {
    
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
     {
            
try
            {
            
               
/* Properties props = new Properties();
                props.setProperty(Velocity.INPUT_ENCODING, "GBK");
                props.setProperty(Velocity.OUTPUT_ENCODING, "GBK");
                props.setProperty(Velocity.RESOURCE_LOADER, "file");
                props.setProperty("file.resource.loader.description", " Velocity File Resource Loader");
                props.setProperty("file.resource.loader.class","org.apache.velocity.runtime.resource.loader.FileResourceLoader");
                props.setProperty("file.resource.loader.path", "D:/Program Files/Apache Software Foundation/Tomcat 6.0/webapps/vtl");
                props.setProperty("file.resource.loader.cache", "true");
                props.setProperty("file.resource.loader.modificationCheckInterval", "1");
                props.setProperty("directive.foreach.counter.name", "velocityCount");
                props.setProperty("directive.foreach.counter.initial.value", "1");
                
*/
                Properties p 
= new Properties();
                p.setProperty(
"resource.loader""class");
                p.setProperty(
"class.resource.loader.description""Velocity Classpath Resource Loader");
                
//"设置vm存放路径,这里设置到放在Classes目录。"
                p.setProperty("class.resource.loader.class""org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
                p.setProperty (
"runtime.log.logsystem.class" ,"org.apache.velocity.runtime.log.NullLogSystem");
                Velocity.init(p);
//初始化模板设置
                
//Velocity.init("velocity.properties");
                Template template = Velocity.getTemplate("/tm/index.vm""GBK");//调用模板文件
                
                VelocityContext context 
= new VelocityContext();//建立上下文链接
                context.put("s"new String [] { "redwood""maple""oak""pine" });
                BufferedWriter writer  
= new BufferedWriter(new OutputStreamWriter(response.getOutputStream(),"GBK"));
                
if ( context != null)
                    template.merge(context, writer);
//输出
                writer.flush();
                writer.close();
           }
           
catch(Exception ex)
           {
              ex.printStackTrace();
            }
        }
    
public void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException
    {
            
this.doGet(req,resp) ;
 
    }
 
}

相关文章:

  • 2022-02-25
  • 2021-07-26
  • 2021-07-11
  • 2022-12-23
  • 2022-12-23
  • 2021-11-19
  • 2021-10-07
  • 2022-12-23
猜你喜欢
  • 2021-04-26
  • 2021-09-16
  • 2022-02-15
  • 2021-08-31
  • 2021-07-03
  • 2021-12-22
  • 2021-04-20
相关资源
相似解决方案