Re: Documentation regarding Custom Utility Class for Velocity
"O. Olson" <[email protected]> Thu, 8 Aug 2013 19:37:35 +0100 (BST)
| Newsgroups | gmane.comp.jakarta.velocity.user |
|---|---|
| Message-ID | <[email protected]> |
I think I figured out the answer to some of my questions by
just trying it out. I wish this was documented somewhere, and I hope this helps
others.
1. First,
this object placed into the Context is instantiated with each request i.e. it
does not need to be static.
I passed an instance of Logger to the constructor of this
object, and I printed out its hashcode like:
public MyUtil(Logger log) {
log.info("Called HowardUtil Constructor: " + this.hashCode());
}
This printed out once per request, and with different
hashCodes, so I know that this object is being recreated with each instance,
and from the different hashCodes they are truly different objects.
2. I
think to pass values you need the bean syntax. For e.g. in my Java Code, I did:
public int getNumRows() {
return 232;
}
Then in the template I did:
$howard.NumRows
This gave me the expected 232. I then tried the
following in the Java code:
public static String MY_CONSTANT = "sdfsdsd";
In the template I did:
$howard.MY_CONSTANT
This gave me $howard.MY_CONSTANT as the
result. I don’t know why this did not work? I'd appreciate any ideas?
Finally I tried in my Java code:
public String getBEAN_CONSTANT() {
return "My Bean Constant";
}
Then in my template I did:
$howard.BEAN_CONSTANT
This resulted in the expected "My Bean
Constant"; in the rendered result.
Is there any other way to pass in constants
other than the bean syntax?
Thank you,
O. O.
----- Messaggio originale -----
Da: O. Olson <[email protected]>
A: Velocity Users List <[email protected]>
Cc:
Inviato: Giovedì 8 Agosto 2013 12:29
Oggetto: Re: Documentation regarding Custom Utility Class for Velocity
Thank you Sergiu for your quick response. I think what I
wanted was really a Plain Old Java Object to be placed in the Velocity Context.
Regarding this Object placed in the Velocity Context:
1. 1. Would
this Object be instantiated each time a new request is placed? i.e. does this
need to be static?
2. 2. Do
we need the bean syntax for properties? Please see my original post for an
example.
Thank you again,
O. O.