代码之家  ›  专栏  ›  技术社区  ›  Hamza Yerlikaya

Apache Velocity无法初始化

  •  2
  • Hamza Yerlikaya  · 技术社区  · 15 年前

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

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

    我在尝试时遇到同样的错误

    Velocity.init();
    

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

    什么可能导致这个例外?

    5 回复  |  直到 11 年前
        1
  •  10
  •   user192127    15 年前

    尝试如下操作:

    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);
    

    等。

        2
  •  4
  •   André Ricardo    14 年前

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

    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!");
    }
    
        3
  •  0
  •   Brian Agnew    13 年前

    这个 LogManager source 建议将原始异常包装在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!!
     */
    
        4
  •  0
  •   James A Mohler Nika    11 年前

    干净的方式:

    java.util.Properties props = new java.util.Properties();
    props.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS,    NullLogChute.class.getName());                        
    Velocity.init(props);
    
        5
  •  0
  •   Warlord    11 年前

    使用

    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;
    }