2

当我尝试使用初始化速度引擎时

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

我尝试时遇到同样的错误

Velocity.init();

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

什么可能导致此异常?

4

5 回答 5

10

尝试这样的事情:

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
  }

您现在可以调用:

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

等等

于 2009-10-18T22:40:29.117 回答
4

问题已解决,将以下代码放入我的应用程序中:

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!");
}
于 2010-11-02T17:45:57.390 回答
0

LogManager 源建议原始异常包含在 VelocityException 中。包装的异常应该为您提供更多信息。

注意代码中的相关注释。

/* 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!!
 */
于 2009-10-18T22:34:45.750 回答
0

一个干净的方法:

java.util.Properties props = new java.util.Properties();
props.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS,    NullLogChute.class.getName());                        
Velocity.init(props);
于 2013-10-17T16:30:23.010 回答
0

采用

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;
}
于 2014-02-18T16:38:35.910 回答