You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by "O. Olson" <ol...@yahoo.it> on 2013/08/08 18:06:18 UTC

Documentation regarding Custom Utility Class for Velocity

Hi,

    I am wondering if there is any documentation on writing your own Utility Class or Custom Tool to add to Velocity. The only place I found some help is http://www.sergiy.ca/how-to-create-custom-tools-for-apache-velocity/  (There is something regarding Custom Tools at http://velocity.apache.org/tools/releases/2.0/creatingtools.html   - but I could not find anything regarding  Utility Classes that we can push into the Velocity Context.)

    Originally, I was thinking of doing my processing/customization in Velocimacros, but that is turning out to be a bit more complex/cumbersome than I expected. I am thinking of doing this in Java, and pushing the results to the Velocity Context as a single object/utility class instance. For purposes of discussion let us assume, I do something like the following in Java:
    VelocityContext context;
    context.put("myUtil", new MyUtil());
    
    I'd appreciate if I could get answers/ideas on:

1.    I am wondering if MyUtil() needs to be static across requests.  I intend MyUtil() to provide easy access and manipulation of the current request. Hence, I would probably do something like the following in Java:

context.put("myUtil", new MyUtil(currentWebRequest));

    i.e. I would instantiate MyUtil with the current Web Request. MyUtil could then provide easy access to say the Number of Rows in the Request. So in the Template I could do:

    $myUtil.NumRows

If MyUtil() is static, this would not work, because different requests would have different number of Rows. Should the Utility Class/Object be static? The example mentioned above shows only static methods, hence my question.

2.    Should my Utility class follow the bean syntax i.e. for the above example of  $myUtil.NumRows in the template, would I need to declare something like the following in the MyUtil Java class:

public int getNumRows() { }

3.    What would be a good way to pass a Constant from Java to the Template? Should I use the bean syntax again? E.g. in the template
$myUtil.JAVA_CONSTANT

    Would I need to have something like:
public int getJAVA_CONSTANT () { }

Thank you in advance for your help,
O. O.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org


Re: Documentation regarding Custom Utility Class for Velocity

Posted by "O. Olson" <ol...@yahoo.it>.

Thank you very much Nathan for clarifying this. I somehow
missed the documentation where it says that Velocity does not expose fields. 
 
            I liked
your suggestion for FieldMethodizer. This does exactly what I wanted. I think I
am set now :-).

 
Thanks again,
O. O.


----- Messaggio originale -----
Da: Nathan Bubna <nb...@gmail.com>
A: Velocity Users List <us...@velocity.apache.org>; O. Olson <ol...@yahoo.it>
Cc: 
Inviato: Giovedì 8 Agosto 2013 14:06
Oggetto: Re: Documentation regarding Custom Utility Class for Velocity

If you are putting the MyUtil class into the context yourself (as your
code seems to suggest, then it is up to you whether you want to create
a single static instance or a new instance for each request.  This is
out of Velocity's control as you describe things.

If you'd like tool instantiation to be managed for you, you may use
the VelocityTools library, either the VelocityView or even just the
GenericTools support. I can go into this further if the documentation
and examples aren't clear.

Bean syntax is optional for methods. You can always call methods with
the full name if you like: $myUtil.getNumRows()

Velocity does not (by default) expose fields, even public final
constant ones.  This is documented thoroughly.  You can adapt the
object to expose fields:
http://velocity.apache.org/engine/releases/velocity-1.7/apidocs/org/apache/velocity/app/FieldMethodizer.html
or Velocity itself:
http://maven-doccheck.sourceforge.net/samples/ShinobuDemo/apidocs/org/apache/velocity/tools/generic/introspection/PublicFieldUberspect.html

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org


Re: Documentation regarding Custom Utility Class for Velocity

Posted by Nathan Bubna <nb...@gmail.com>.
If you are putting the MyUtil class into the context yourself (as your
code seems to suggest, then it is up to you whether you want to create
a single static instance or a new instance for each request.  This is
out of Velocity's control as you describe things.

If you'd like tool instantiation to be managed for you, you may use
the VelocityTools library, either the VelocityView or even just the
GenericTools support. I can go into this further if the documentation
and examples aren't clear.

Bean syntax is optional for methods. You can always call methods with
the full name if you like: $myUtil.getNumRows()

Velocity does not (by default) expose fields, even public final
constant ones.  This is documented thoroughly.  You can adapt the
object to expose fields:
http://velocity.apache.org/engine/releases/velocity-1.7/apidocs/org/apache/velocity/app/FieldMethodizer.html
or Velocity itself:
http://maven-doccheck.sourceforge.net/samples/ShinobuDemo/apidocs/org/apache/velocity/tools/generic/introspection/PublicFieldUberspect.html

On Thu, Aug 8, 2013 at 9:06 AM, O. Olson <ol...@yahoo.it> wrote:
> Hi,
>
>     I am wondering if there is any documentation on writing your own Utility Class or Custom Tool to add to Velocity. The only place I found some help is http://www.sergiy.ca/how-to-create-custom-tools-for-apache-velocity/  (There is something regarding Custom Tools at http://velocity.apache.org/tools/releases/2.0/creatingtools.html   - but I could not find anything regarding  Utility Classes that we can push into the Velocity Context.)
>
>     Originally, I was thinking of doing my processing/customization in Velocimacros, but that is turning out to be a bit more complex/cumbersome than I expected. I am thinking of doing this in Java, and pushing the results to the Velocity Context as a single object/utility class instance. For purposes of discussion let us assume, I do something like the following in Java:
>     VelocityContext context;
>     context.put("myUtil", new MyUtil());
>
>     I'd appreciate if I could get answers/ideas on:
>
> 1.    I am wondering if MyUtil() needs to be static across requests.  I intend MyUtil() to provide easy access and manipulation of the current request. Hence, I would probably do something like the following in Java:
>
> context.put("myUtil", new MyUtil(currentWebRequest));
>
>     i.e. I would instantiate MyUtil with the current Web Request. MyUtil could then provide easy access to say the Number of Rows in the Request. So in the Template I could do:
>
>     $myUtil.NumRows
>
> If MyUtil() is static, this would not work, because different requests would have different number of Rows. Should the Utility Class/Object be static? The example mentioned above shows only static methods, hence my question.
>
> 2.    Should my Utility class follow the bean syntax i.e. for the above example of  $myUtil.NumRows in the template, would I need to declare something like the following in the MyUtil Java class:
>
> public int getNumRows() { }
>
> 3.    What would be a good way to pass a Constant from Java to the Template? Should I use the bean syntax again? E.g. in the template
> $myUtil.JAVA_CONSTANT
>
>     Would I need to have something like:
> public int getJAVA_CONSTANT () { }
>
> Thank you in advance for your help,
> O. O.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org


Re: Documentation regarding Custom Utility Class for Velocity

Posted by "O. Olson" <ol...@yahoo.it>.

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 <ol...@yahoo.it>
A: Velocity Users List <us...@velocity.apache.org>
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.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org


Re: Documentation regarding Custom Utility Class for Velocity

Posted by "O. Olson" <ol...@yahoo.it>.

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.


----- Messaggio originale -----
Da: Sergiu Dumitriu <se...@gmail.com>
A: Velocity Users List <us...@velocity.apache.org>
Cc: 
Inviato: Giovedì 8 Agosto 2013 12:10
Oggetto: Re: Documentation regarding Custom Utility Class for Velocity

It depends on what you mean by velocity tools.

As the description on http://velocity.apache.org/tools/releases/2.0/
says, a tool is just a Plain Old Java Object, so any Java object can act
as a tool.

Officially, a "tool" is something that can be automatically placed in
the context when you have a more complex/automatic Velocity Engine
setup, like VelocityViewServlet or VelocityStruts, or using one of the
configuration mechanisms listed on
http://velocity.apache.org/tools/devel/config.html

Unofficially, if you're working directly with the engine, you can place
any object in the VelocityContext when rendering a parsed template.

So, there's no restriction on the class that you're using as a tool,
other than:

- only public methods can be invoked from Velocity, but there can be
helper private methods if they help you organize the code
- if the tool is automatically instantiated, it must have a public
constructor with no parameters; if you instantiate it manually, you can
pass as many arguments as you want in the constructor   

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org


Re: Documentation regarding Custom Utility Class for Velocity

Posted by Sergiu Dumitriu <se...@gmail.com>.
It depends on what you mean by velocity tools.

As the description on http://velocity.apache.org/tools/releases/2.0/
says, a tool is just a Plain Old Java Object, so any Java object can act
as a tool.

Officially, a "tool" is something that can be automatically placed in
the context when you have a more complex/automatic Velocity Engine
setup, like VelocityViewServlet or VelocityStruts, or using one of the
configuration mechanisms listed on
http://velocity.apache.org/tools/devel/config.html

Unofficially, if you're working directly with the engine, you can place
any object in the VelocityContext when rendering a parsed template.

So, there's no restriction on the class that you're using as a tool,
other than:

- only public methods can be invoked from Velocity, but there can be
helper private methods if they help you organize the code
- if the tool is automatically instantiated, it must have a public
constructor with no parameters; if you instantiate it manually, you can
pass as many arguments as you want in the constructor

On 08/08/2013 12:06 PM, O. Olson wrote:
> Hi,
> 
>     I am wondering if there is any documentation on writing your own Utility Class or Custom Tool to add to Velocity. The only place I found some help is http://www.sergiy.ca/how-to-create-custom-tools-for-apache-velocity/  (There is something regarding Custom Tools at http://velocity.apache.org/tools/releases/2.0/creatingtools.html   - but I could not find anything regarding  Utility Classes that we can push into the Velocity Context.)
> 
>     Originally, I was thinking of doing my processing/customization in Velocimacros, but that is turning out to be a bit more complex/cumbersome than I expected. I am thinking of doing this in Java, and pushing the results to the Velocity Context as a single object/utility class instance. For purposes of discussion let us assume, I do something like the following in Java:
>     VelocityContext context;
>     context.put("myUtil", new MyUtil());
>     
>     I'd appreciate if I could get answers/ideas on:
> 
> 1.    I am wondering if MyUtil() needs to be static across requests.  I intend MyUtil() to provide easy access and manipulation of the current request. Hence, I would probably do something like the following in Java:
> 
> context.put("myUtil", new MyUtil(currentWebRequest));
> 
>     i.e. I would instantiate MyUtil with the current Web Request. MyUtil could then provide easy access to say the Number of Rows in the Request. So in the Template I could do:
> 
>     $myUtil.NumRows
> 
> If MyUtil() is static, this would not work, because different requests would have different number of Rows. Should the Utility Class/Object be static? The example mentioned above shows only static methods, hence my question.
> 
> 2.    Should my Utility class follow the bean syntax i.e. for the above example of  $myUtil.NumRows in the template, would I need to declare something like the following in the MyUtil Java class:
> 
> public int getNumRows() { }
> 
> 3.    What would be a good way to pass a Constant from Java to the Template? Should I use the bean syntax again? E.g. in the template
> $myUtil.JAVA_CONSTANT
> 
>     Would I need to have something like:
> public int getJAVA_CONSTANT () { }
> 
> Thank you in advance for your help,
> O. O.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
> 


-- 
Sergiu Dumitriu
http://purl.org/net/sergiu

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org