不是有个VelocityViewServlet(具体记不清了,到google上查查)的Velocity引擎(类似于jsp引擎),用它就不用自己编码来处理.vm文件了,直接把.vm文件映射到这个Servlet就行了。

解决方案 »

  1.   

    我也找不到VM文件,楼主,我的例子比你简单多了。我刚用velocity,用的例子就是网上最常见到的例子java类文件代码
    --------------------------------------------------
    import java.io.StringWriter;
    import org.apache.velocity.app.VelocityEngine;
    import org.apache.velocity.Template;
    import org.apache.velocity.VelocityContext;public class HelloWorld {
        public static void main(String[] args) throws Exception {/* first, get and initialize an engine */
            VelocityEngine ve = new VelocityEngine();
            ve.init();        Template t = ve.getTemplate("HelloWorld.vm");/* create a context and add data */
            VelocityContext context = new VelocityContext();
            context.put("name", "Eiffel Qiu");
            context.put("site", "http://www.eiffelqiu.com");/* now render the template into a StringWriter */
            StringWriter writer = new StringWriter();
            t.merge(context, writer);/* show the World */
            System.out.println(writer.toString());
        }
    }
    ---------------------------------------------
    vm文件HeoolWorld.vm代码
    ---------------------------------------------
    Hello $name! Welcome to $site world!
    ---------------------------------------------就是说找不到VM文件,为什么啊?org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource 'HelloWorld.vm'
    at org.apache.velocity.runtime.resource.ResourceManagerImpl.loadResource(ResourceManagerImpl.java:501)
    at org.apache.velocity.runtime.resource.ResourceManagerImpl.getResource(ResourceManagerImpl.java:384)
    at org.apache.velocity.runtime.RuntimeInstance.getTemplate(RuntimeInstance.java:814)
    at org.apache.velocity.runtime.RuntimeInstance.getTemplate(RuntimeInstance.java:796)
    at org.apache.velocity.app.VelocityEngine.getTemplate(VelocityEngine.java:512)
    at HelloWorld.main(HelloWorld.java:11)
    Exception in thread "main" Process terminated with exit code 1