Hello everyone,
I am trying to introduce a simple POJO as my own Tool. I have tried to reduce it to a minimal Example which I will list below. I can see that the POJO ist available in the template, but I am not able to access its function, whatever I try. There must be something I am doing wrong here. We include Version 2.3 in our build.gradle. implementation ('org.apache.velocity:velocity-engine-core:2.3') Maybe somebody here is able to give me a hint? I would be very grateful about any advice because I only find examples which make me think that my code should be alright... cheers Stefan ///////////////// MyClass.java Begin package mypackage; public class MyClass { class Toolbox { String rot13(String s) { StringBuffer ret = new StringBuffer(); for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (c >= 'a' && c <= 'm') c += 13; else if (c >= 'A' && c <= 'M') c += 13; else if (c >= 'n' && c <= 'z') c -= 13; else if (c >= 'N' && c <= 'Z') c -= 13; ret.append(c); } return ret.toString(); } } public String render() throws Exception { VelocityContext context = new VelocityContext(); // put a POJO to make some useful functions accessible in templates context.put("TEMPLATE_NAME", "mytemplate"); context.put("toolbox", new Toolbox()); VelocityEngine ve = new VelocityEngine(); // define the logger name that is used in log messages by Velocity ve.setProperty(VelocityEngine.RUNTIME_LOG_NAME, "VelocityEngine"); // activate the Classpath resource loader (see also https://velocity.apache.org/engine/devel/apidocs/index.html) ve.setProperty("resource.loaders", "class"); ve.setProperty("resource.loader.class.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); ve.setProperty("runtime.strict_mode.enable", "true"); // after setting properties we may now initialize the engine ve.init(); Template template = ve.getTemplate("mytemplate.vm", "UTF-8"); StringWriter sw = new StringWriter(); template.merge( context, sw ); return sw.toString()); } } ///////////////// MyClass.java End This is my template: ######### mytemplate.vm Begin {PC001;0030,0015,1,1,O,11,B=$toolbox.rot13("test") $toolbox.Rot13($TEMPLATE_NAME) $TEMPLATE_NAME $toolbox |} ######### mytemplate.vm End With the strict mode, I learn that Velocity does not see the rot13() method: ------------------ Output Start org.apache.velocity.exception.MethodInvocationException: Object 'mypackage.MyClass$Toolbox' does not contain method rot13(java.lang.String) at velocity/header.vm[line 8, column 38] ------------------- Output End Without the strict mode, I see the output: {PC001;0030,0015,1,1,O,11,B=$toolbox.rot13("test") $toolbox.Rot13($TEMPLATE_NAME) artikel mypackage.MyClass$Toolbox@6b419da |} JETZT NEWSLETTER ABONNIEREN: https://hammerbacher.com/newsletter-anmeldung/ Hammerbacher GmbH Geschäftsführer Bernhard Hammerbacher, Ursula Hammerbacher Registergericht Nürnberg HRB 10908 Hausanschrift Daimlerstraße 4-6 D 92318 Neumarkt Telefon +49(0)9181 2592-0 Telefax +49(0)9181 2592-28 E-Mail i...@hammerbachergmbh.de www.hammerbacher.com Haftungsausschluss / Disclaimer Die Informationen, die in dieser Kommunikation enthalten sind, sind ausschließlich und allein für den Empfänger bestimmt. Die Verwendung durch Dritte ist untersagt. Die Firma Hammerbacher GmbH ist nur für die von ihr eingegeben Informationen verantwortlich, jedoch nicht für die einwandfreie Übertragung oder im Zusammenhang mit der Übertragung oder dem Empfang eingetretene Veränderungen oder Verzögerungen. Diese E-Mail enthält vetrtrauliche und/oder rechtlich geschützte Informationen. Wenn Sie nicht der richtige Adressat sind oder diese E-Mail irrtümlich erhalten haben, informieren Sie bitte sofort den Absender und vernichten Sie diese E-Mail. Das unerlaubte Kopieren sowie die unbefugte Weitergabe dieser Mail ist nicht gestattet.