[英]Apache Velocity can not Initialize
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? 什么可能导致这个例外?
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. 等等
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!");
}
The LogManager source suggests the originating exception is wrapped in the VelocityException. LogManager源表明原始异常包含在VelocityException中。 That wrapped exception should give you more info. 包装异常应该会给你更多信息。
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!!
*/
A clean way: 干净的方式:
java.util.Properties props = new java.util.Properties();
props.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS, NullLogChute.class.getName());
Velocity.init(props);
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.