velocity framework on google app engine

有些话、适合烂在心里 提交于 2019-12-10 05:27:47

问题


i am trying to use velocity framework on google app engine. i wrote a small program with a main method and tried running it locally. i get the following exception :

Exception in thread "main" org.apache.velocity.exception.VelocityException: Failed to initialize an instance of org.apache.velocity.runtime.log.ServletLogChute with the current runtime configuration.
  at org.apache.velocity.runtime.log.LogManager.createLogChute(LogManager.java:206)
  at org.apache.velocity.runtime.log.LogManager.updateLog(LogManager.java:255)
  at org.apache.velocity.runtime.RuntimeInstance.initializeLog(RuntimeInstance.java:795)
  at org.apache.velocity.runtime.RuntimeInstance.init(RuntimeInstance.java:250)
  at org.apache.velocity.app.VelocityEngine.init(VelocityEngine.java:107)
  at Main.main(Main.java:10)
Caused by: java.lang.UnsupportedOperationException: Could not retrieve ServletContext from application attributes
  at org.apache.velocity.runtime.log.ServletLogChute.init(ServletLogChute.java:73)
  at org.apache.velocity.runtime.log.LogManager.createLogChute(LogManager.java:157)
  ... 5 more

Here is my program:

import java.io.StringWriter;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;

public class Main {
 public static void main(String[] args) throws Exception{
        /*  first, get and initialize an engine  */
        VelocityEngine ve = new VelocityEngine();
        ve.init();
        /*  next, get the Template  */
        Template t = ve.getTemplate( "helloworld.vm" );
        /*  create a context and add data */
        VelocityContext context = new VelocityContext();
        context.put("name", "World");
        /* now render the template into a StringWriter */
        StringWriter writer = new StringWriter();
        t.merge( context, writer );
        /* show the World */
        System.out.println( writer.toString() );    
 }
}

the same program runs perfectly fine on a normal eclipse project. what could be the problem?


回答1:


Seems to only be the ServletLogChute class that requires the ServletContext, Velocity itself can work entirely standalone from a Servlet environment.

Since you obviously don't have a servelt log, try adding the following before you call ve.init():

ve.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.NullLogChute");

...or check here if you have specific logging requirements.




回答2:


This may not be the end of the world and story, but there's a list of GAE compatible software:

http://groups.google.com/group/google-appengine-java/web/will-it-play-in-app-engine

and I didn't find Velocity in it. It's possible people just forgot to test and include it in the list, but it's also possible Velocity brings some API with it that doesn't play nicely with GAE.




回答3:


Velocity can be definitively made to run on GAE/J.

Apache Click Framework that is using Velocity as it's template engine, works without a problem on GAE/J.

It needs of course a different configuration than usual since GAE/J is a constraint environment, but nevertheless, it works.



来源:https://stackoverflow.com/questions/2014099/velocity-framework-on-google-app-engine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!