Java Apache Velocity 无法初始化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1586133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 16:30:38  来源:igfitidea点击:

Apache Velocity can not Initialize

javavelocity

提问by Hamza Yerlikaya

When i try to initialize velocity engine using

当我尝试使用

VelocityEngine engine = new VelocityEngine();
engine.init();

I encounter the same error when i try

我尝试时遇到同样的错误

Velocity.init();

org.apache.velocity.exception.VelocityException: Failed to initialize an instance of org.apache.velocity.runtime.log.ServletLogChute with the current runtime configuration.

org.apache.velocity.exception.VelocityException:无法使用当前运行时配置初始化 org.apache.velocity.runtime.log.ServletLogChute 的实例。

What may cause this exception?

什么可能导致此异常?

采纳答案by user192127

try something like this:

尝试这样的事情:

Properties p = new Properties();
p.setProperty("resource.loader", "class");
p.setProperty("class.resource.loader.description", "Velocity Classpath Resource Loader");
p.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");

    try {
      Velocity.init(p);
    } catch(...., and handle excpetion
  }

you'll now be able to call:

您现在可以调用:

VelocityContext vContext = new VelocityContext(context);
//put things into vContext
StringWriter sw = new StringWriter();
    try {
      template.merge(vContext, sw);

etc.

等等。

回答by Brian Agnew

The LogManager sourcesuggests the originating exception is wrapped in the VelocityException. That wrapped exception should give you more info.

日志管理源表明始发异常包裹在VelocityException。包装的异常应该为您提供更多信息。

Note the related comment in the code.

注意代码中的相关注释。

/* If the above failed, that means either the user specified a
 * logging class that we can't find, there weren't the necessary
 * dependencies in the classpath for it, or there were the same
 * problems for the default loggers, log4j and Java1.4+.
 * Since we really don't know and we want to be sure the user knows
 * that something went wrong with the logging, let's fall back to the
 * surefire SystemLogChute. No panicking or failing to log!!
 */

回答by André Ricardo

The problem was solved putting the code below in my app:

问题解决了,将下面的代码放在我的应用程序中:

java.util.Properties p = new java.util.Properties();
p.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.NullLogSystem");
try {
    Velocity.init(p);
} catch (Exception e) {
    System.out.println("FAIL!");
}

回答by vsilva007

A clean way:

一个干净的方法:

java.util.Properties props = new java.util.Properties();
props.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS,    NullLogChute.class.getName());                        
Velocity.init(props);

回答by Fadid

Use

static {
    /** Initialisation du moteur velocity */
    Velocity.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS, 
            "org.apache.velocity.runtime.log.NullLogSystem");
    Velocity.setProperty(RuntimeConstants.RESOURCE_LOADER,
            EjbConstants.VELOCITY_RESOURCE_LOADER_TYPE);
    Velocity.setProperty(EjbConstants.VELOCITY_CLASSPATH_RESOURCE_LOADER,
            ClasspathResourceLoader.class.getName());
}

public static String createXMLFlux(final RemiseEffetBean bean, 
        final String maquetteId) {

    try {
        final VelocityContext context = new VelocityContext();
        final StringWriter swOut = new StringWriter();

        // Initialisation
        Velocity.init();

        final Template template = Velocity.getTemplate(
                EjbConstants.XML_TEMPLATE_FILE, CharEncoding.UTF_8);

        context.put(EjbConstants.VELOCITY_REMISE_EFFET, bean);

        // id de la maquette pdf a generer
        context.put(EjbConstants.VELOCITY_MAQUETTE_ID, maquetteId);

        template.merge(context, swOut);

        return swOut.toString();
    } catch (final ResourceNotFoundException e) {
        LOGGER.error("La template n'a pas été trouvée", e);
    } catch (final ParseErrorException e) {
        LOGGER.error("Erreur du parsing de la template", e);
    } catch (final MethodInvocationException e) {
        LOGGER.error("Erreur lors de la substitution des données", e);
    } catch (final Exception e) {
        LOGGER.error("Erreur lors du traitement du fichier", e);
    }

    return null;
}