velocity官方的examples运行报各种错误

黑牙菜菜 发布于 2012/06/21 21:36
阅读 4K+
收藏 0

1. app_example1 是一个通过velocity.properties初始华velocity

 

 import org.apache.velocity.app.Velocity;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.Template;

import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;

import java.io.*;
import java.util.ArrayList;



public class Example
{
    public Example(String templateFile)
    {
        try
        {
            /*
             * setup
             */

            Velocity.init("velocity.properties");


            VelocityContext context = new VelocityContext();
            context.put("list", getNames());


            Template template =  null;

            try
            {
                template = Velocity.getTemplate(templateFile);
            }
            catch( ResourceNotFoundException rnfe )
            {
                System.out.println("Example : error : cannot find template " + templateFile );
            }
            catch( ParseErrorException pee )
            {
                System.out.println("Example : Syntax error in template " + templateFile + ":" + pee );
            }

           

            BufferedWriter writer = writer = new BufferedWriter(
                new OutputStreamWriter(System.out));

            if ( template != null)
                template.merge(context, writer);

        
            writer.flush();
            writer.close();
        }
        catch( Exception e )
        {
            System.out.println(e);
        }
    }

    public ArrayList getNames()
    {
        ArrayList list = new ArrayList();

        list.add("ArrayList element 1");
        list.add("ArrayList element 2");
        list.add("ArrayList element 3");
        list.add("ArrayList element 4");

        return list;
    }

    public static void main(String[] args)
    {
        Example t = new Example("example.vm");
    }
}

 

    运行的错误信息:org.apache.velocity.exception.VelocityException: Error reading properties from 'velocity.properties'

这个问题我解决不了,也不知道是什么问题.

问怎么才能加载velocity.properties

2.app_example2

运行错误信息: org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource 'example2.vm'

import java.io.StringWriter;
import java.util.Properties;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.VelocityContext;

import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.MethodInvocationException;


public class Example2
{
    public static void main( String args[] )
    {
        /* first, we init the runtime engine.  Defaults are fine. */

        try
        {
            Velocity.init();
        }
        catch(Exception e)
        {
            System.out.println("Problem initializing Velocity : " + e );
            return;
        }

        /* lets make a Context and put data into it */

        VelocityContext context = new VelocityContext();

        context.put("name", "Velocity");
        context.put("project", "Jakarta");

        /* lets render a template */

        StringWriter w = new StringWriter();

        try
        {
            Velocity.mergeTemplate("example2.vm", "ISO-8859-1", context, w );
        }
        catch (Exception e )
        {
            System.out.println("Problem merging template : " + e );
        }

        System.out.println(" template : " + w );

       
        String s = "We are using $project $name to render this.";
        w = new StringWriter();

        try
        {
            Velocity.evaluate( context, w, "mystring", s );
        }
        catch( ParseErrorException pee )
        {
          
            System.out.println("ParseErrorException : " + pee );
        }
        catch( MethodInvocationException mee )
        {
         
            System.out.println("MethodInvocationException : " + mee );
        }
        catch( Exception e )
        {
            System.out.println("Exception : " + e );
        }

        System.out.println(" string : " + w );
    }
}

 

 

 

这个问题自己能解决

Properties p=new Properties();
p.setProperty("file.resource.loader.class",
   "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");  
Velocity.init(p);

问默认的file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.FilesourceLoader 的模板怎么加载

 

召唤@红薯

 

加载中
1
黑牙菜菜
黑牙菜菜

引用来自“史嗷嗷”的答案

路径改成这样:   ./src/velocity.properties
多谢各位,找到原因了 因为我是在eclipse里测试的所以FileResourceLoader加载器以项目目录为当前目录了,如果用直接用命令行运行就没会出现这问题了
0
zplswf
zplswf
很明显吗 路径不对 找不到
黑牙菜菜
黑牙菜菜
vm文件我是和.class文件放一起的
黑牙菜菜
黑牙菜菜
那为什么Properties p=new Properties(); p.setProperty("file.resource.loader.class",    "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); Velocity.init(p); 这里能仿问到啊
0
zplswf
zplswf
不用这写这句话,velocity有个装载器!
0
黑牙菜菜
黑牙菜菜
FileResourceLoader 装载器的 getTamplate("这里怎么写")
0
aoaoxiong
aoaoxiong
路径改成这样:   ./src/velocity.properties
返回顶部
顶部