You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Stefan Großhauser <st...@hammerbachergmbh.de> on 2022/01/26 08:54:38 UTC
Cannot access method of POJO
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
info@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.
Re: Cannot access method of POJO
Posted by Stefan Großhauser <st...@hammerbachergmbh.de>.
Alright, maybe I have misunderstood your answer.
The method can be accessed if the Toolbox class is not an inner class but if it is defined in its own file.
thank you for your help!
Stefan
Von: Stefan Großhauser <st...@hammerbachergmbh.de>
An: Velocity Users List <us...@velocity.apache.org>
Gesendet: 26.01.2022 10:04
Betreff: Re: Cannot access method of POJO
Thank you for having a look.
Actually, trying all sorts of things, I had removed the 'public' keywords.
I have just tried it again with a public inner class containing a public method, but unfortunately that does not make any difference, the error stays the same.
///////////////
public class Toolbox {
public 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();
}
}
/////////////////////////
Stefan Großhauser
IT
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
info@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.
Re: Cannot access method of POJO
Posted by Stefan Großhauser <st...@hammerbachergmbh.de>.
Thank you for having a look.
Actually, trying all sorts of things, I had removed the 'public' keywords.
I have just tried it again with a public inner class containing a public method, but unfortunately that does not make any difference, the error stays the same.
///////////////
public class Toolbox {
public 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();
}
}
/////////////////////////
Stefan Großhauser
IT
Telefon +49 (0) 9181 2592-83
E-Mail stefan.grosshauser@hammerbachergmbh.de
Hammerbacher GmbH, Daimlerstraße 4+6, 92318 Neumarkt
Internet: www.hammerbacher.com
Von: Ranil Wijeyratne <ra...@ergon.ch>
An: Velocity Users List <us...@velocity.apache.org>
Gesendet: 26.01.2022 9:58
Betreff: Re: Cannot access method of POJO
I think the problem is that your rot13 method is package private and not public.
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
info@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.
Re: Cannot access method of POJO
Posted by Ranil Wijeyratne <ra...@ergon.ch>.
I think the problem is that your rot13 method is package private and not public.
On 26.01.22, 09:54, "Stefan Großhauser" <st...@hammerbachergmbh.de> wrote:
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
info@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.