You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Rasoul Hajikhani <ra...@rhythm.com> on 2001/09/19 22:51:18 UTC

New Bee

Hi there,
I am using Velocity to generate dynamic content. I am new to Velocity
and have read the documentation. However, some thing very simple has
escaped me. Here is my code snippet:

ctx.put("theMessage",message); /* string works fine*/
ctx.put("primes",currentPrimes); /* vector works fine*/
ctx.put("Primes",numberPrimes); /* string */
ctx.put("Digits",numberDigits); /* string */

and in my .vm I have:

<INPUT TYPE=HIDDEN NAME="Digits" VALUE=$Digits>
<INPUT TYPE=HIDDEN NAME="Primes" VALUE=$Primes>

But "$Digits" and "$Primes" are never substituted for their real value.
Any ideas?

I apologize if this is way a rudimentary question.
-r

Re: How to set valocity property

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 10/6/01 6:16 PM, "James You" <jy...@hywy.com> wrote:

> Hi, all:
> I am new to Velocity Tempalte Engine. The follwing code is used to
> create a Velocity context.

[SNIP]

> 
> Can any one help me to solve this property? i.e. how to define
> valocity's property (resource.loader.1.resource.path...etc.)
> so as to let velocity to load those tempalte file (*.vm) under the
> sepcific location instead of in the current working directory?
>

First, read the documentation on configuring resource loaders in the
developers guide.

Second - where are you getting the

Resource.loader.1.path

format?  We haven't used that since pre-1.0 release?  Where are you finding
it?

Have you considered giving it an absolute path?  Like

file.resource.loader.path = C:\foobar\templates


Let us know.

Geir

-- 
Geir Magnusson Jr.     geirm@optonline.net
System and Software Consulting
"Whoever would overthrow the liberty of a nation must begin by subduing the
freeness of speech." - Benjamin Franklin



How to set valocity property

Posted by James You <jy...@hywy.com>.
Hi, all:
I am new to Velocity Tempalte Engine. The follwing code is used to
create a Velocity context.
		
                // first, we init the runtime engine.
                Properties prop = new Properties();

                String templatePropertyFile =
this.getTemplateFileRoot_() + "velocity.properties";
            
                File tempProperty = new File(templatePropertyFile);
                FileInputStream fiStr = new
FileInputStream(tempProperty);
                prop.load(fiStr);
                
            
                prop.setProperty("velocimacro.library",
"template/VelocityMacro.vm");


                Velocity.init( prop );

                Log_.info(THIS + " Template source path --> " +
Velocity.getProperty("resource.loader.1.resource.path"));

                // then we init the define properties for runtime
engine.
                Velocity.init();

                // let's make a Context and put data into it */
                vContext = new VelocityContext();
 

I am using JBuilder to develop the project
	the project's current working directory is in sub directory of
"classes" under the my project's home
		$MyProject_Home\classes
In fact, my tempalte file (called myTemplate.vm) is located in 
		$MyProject_Home\classes\template\myTemplate.vm

In the property file (velocity.properties), I set the the tempalte
file's root directory

		resource.loader.1.resource.path = .\\template\\

The problem I had is that 
		Velocity always looking for myTemplat.vm find in the current
directory, i.e.
		under the directory $MyProject_Home\classes\		
		I had to put myTemplate.vm file under the "classes' the directory
		i.e:  $MyProject_Home\classes\myTemplate.vm

If I put myTemplate.vm into
$MyProject_Home\classes\template\myTemplate.vm, it will never
find it no matter how I change the property
(resource.loader.1.resource.path) value formats in the
velocity.properties file.
		resource.loader.1.resource.path = template
		resource.loader.1.resource.path = /template/
		resource.loader.1.resource.path = /template
		resource.loader.1.resource.path = //template
		resource.loader.1.resource.path = \\template\\
		....
		
Does the format of proerty setting for resource.loader.1.resource.path
(resource.loader.1.resource.path = .\\template\\)
is wrong???

Can any one help me to solve this property? i.e. how to define 
valocity's property (resource.loader.1.resource.path...etc.)
so as to let velocity to load those tempalte file (*.vm) under the
sepcific location instead of in the current working directory?


Thanks you in advanced!



JamesLong

Re: New Bee

Posted by Rasoul Hajikhani <ra...@rhythm.com>.
Lane Sharman wrote:
> 
> Greetings Rasoul,
> 
> This is not a velocity problem since it works in one case and not in another
> unless we are operating in a different universe here ( and it seems today that
> the world is not the same it was 9 days ago, sadly).
> 
> I suspect that your thread is not depositing $Digit and $Prime into the context
> with valid references. Therefore, they render as null the second time. Without
> looking at your code, this is the prime suspect: your rendering/evaluation of
> the template is preceding before the references to $Digit and $Prime are valid.
> And, given the JMM, if you are setting these refs in a separate thread, then
> you are treading on the JMM model in which a reference in one thread can be
> completely different in another unsynchronized thread.  Check out the many
> excellent articles/books on the JMM.
> 
> Your threads are interacting with one another and causing side-effects.
> 
> -lane
> 
> Rasoul Hajikhani wrote:
> 
> > Bojan Smojver wrote:
> > >
> > > Rasoul Hajikhani wrote:
> > >
> > > > Yes, $theMessage does get rendered properly. I think the problem is that
> > > > I ask for a Refresh if thtread is not finished and when the request
> > > > comes in again after 5 secs, the first time they are what they are
> > > > supposed to be but on subsequent "Refresh" their value is not rendered.
> > > > Somehow, the rendering never takes place.
> > >
> > > Maybe you can be a bit more verbose in your Java code and actually log
> > > the values of those things when they go into context. I'd also use
> > > something like this in Velocity, just to verify what those things really
> > > are when they are being used:
> > >
> > > ----------------------------------------------
> > > #if($Primes)
> > >   Do the input thing here...
> > > #else
> > >   <p>DEBUG: The Primes reference was null!</p>
> > > #end
> > > ----------------------------------------------
> > >
> >
> > I have done that, and I can see the value sof $Digit and $Prime OK the
> > first time the .vm is returned. However, the values are lost after 5
> > seconds when the document is refreshed.
> >
> > > You also mentioned thread not being finished. I'm not sure I understand
> > > what you meant there... Is this a multithreaded application of some
> > > sort? Or are you referring to a servlet thread?
> > Yes. It is a multithreaded application. And no, I am not refering to
> > servlet threads.
> > >
> > > Bojan
> 
> --
> Lane Sharman
> http://corporate.acctiva.com/lane



Thanks for your help and advice.
-r

Re: New Bee

Posted by Lane Sharman <la...@san.rr.com>.
Greetings Rasoul,

This is not a velocity problem since it works in one case and not in another
unless we are operating in a different universe here ( and it seems today that
the world is not the same it was 9 days ago, sadly).

I suspect that your thread is not depositing $Digit and $Prime into the context
with valid references. Therefore, they render as null the second time. Without
looking at your code, this is the prime suspect: your rendering/evaluation of
the template is preceding before the references to $Digit and $Prime are valid.
And, given the JMM, if you are setting these refs in a separate thread, then
you are treading on the JMM model in which a reference in one thread can be
completely different in another unsynchronized thread.  Check out the many
excellent articles/books on the JMM.

Your threads are interacting with one another and causing side-effects.

-lane

Rasoul Hajikhani wrote:

> Bojan Smojver wrote:
> >
> > Rasoul Hajikhani wrote:
> >
> > > Yes, $theMessage does get rendered properly. I think the problem is that
> > > I ask for a Refresh if thtread is not finished and when the request
> > > comes in again after 5 secs, the first time they are what they are
> > > supposed to be but on subsequent "Refresh" their value is not rendered.
> > > Somehow, the rendering never takes place.
> >
> > Maybe you can be a bit more verbose in your Java code and actually log
> > the values of those things when they go into context. I'd also use
> > something like this in Velocity, just to verify what those things really
> > are when they are being used:
> >
> > ----------------------------------------------
> > #if($Primes)
> >   Do the input thing here...
> > #else
> >   <p>DEBUG: The Primes reference was null!</p>
> > #end
> > ----------------------------------------------
> >
>
> I have done that, and I can see the value sof $Digit and $Prime OK the
> first time the .vm is returned. However, the values are lost after 5
> seconds when the document is refreshed.
>
> > You also mentioned thread not being finished. I'm not sure I understand
> > what you meant there... Is this a multithreaded application of some
> > sort? Or are you referring to a servlet thread?
> Yes. It is a multithreaded application. And no, I am not refering to
> servlet threads.
> >
> > Bojan

--
Lane Sharman
http://corporate.acctiva.com/lane




Re: New Bee

Posted by Rasoul Hajikhani <ra...@rhythm.com>.
Bojan Smojver wrote:
> 
> Rasoul Hajikhani wrote:
> 
> > Yes, $theMessage does get rendered properly. I think the problem is that
> > I ask for a Refresh if thtread is not finished and when the request
> > comes in again after 5 secs, the first time they are what they are
> > supposed to be but on subsequent "Refresh" their value is not rendered.
> > Somehow, the rendering never takes place.
> 
> Maybe you can be a bit more verbose in your Java code and actually log
> the values of those things when they go into context. I'd also use
> something like this in Velocity, just to verify what those things really
> are when they are being used:
> 
> ----------------------------------------------
> #if($Primes)
>   Do the input thing here...
> #else
>   <p>DEBUG: The Primes reference was null!</p>
> #end
> ----------------------------------------------
> 

I have done that, and I can see the value sof $Digit and $Prime OK the
first time the .vm is returned. However, the values are lost after 5
seconds when the document is refreshed.

> You also mentioned thread not being finished. I'm not sure I understand
> what you meant there... Is this a multithreaded application of some
> sort? Or are you referring to a servlet thread?
Yes. It is a multithreaded application. And no, I am not refering to
servlet threads.
> 
> Bojan

Re: New Bee

Posted by Bojan Smojver <bo...@binarix.com>.
Rasoul Hajikhani wrote:

> Yes, $theMessage does get rendered properly. I think the problem is that
> I ask for a Refresh if thtread is not finished and when the request
> comes in again after 5 secs, the first time they are what they are
> supposed to be but on subsequent "Refresh" their value is not rendered.
> Somehow, the rendering never takes place.

Maybe you can be a bit more verbose in your Java code and actually log
the values of those things when they go into context. I'd also use
something like this in Velocity, just to verify what those things really
are when they are being used:

----------------------------------------------
#if($Primes)
  Do the input thing here...
#else
  <p>DEBUG: The Primes reference was null!</p>
#end
----------------------------------------------

You also mentioned thread not being finished. I'm not sure I understand
what you meant there... Is this a multithreaded application of some
sort? Or are you referring to a servlet thread?

Bojan

Re: New Bee

Posted by Rasoul Hajikhani <ra...@rhythm.com>.
"Geir Magnusson Jr." wrote:
> 
> On 9/19/01 6:55 PM, "Rasoul Hajikhani" <ra...@rhythm.com> wrote:
> 
> > "Geir Magnusson Jr." wrote:
> >>
> >> On 9/19/01 4:51 PM, "Rasoul Hajikhani" <ra...@rhythm.com> wrote:
> >>
> >>> Hi there,
> >>> I am using Velocity to generate dynamic content. I am new to Velocity
> >>> and have read the documentation. However, some thing very simple has
> >>> escaped me. Here is my code snippet:
> >>>
> >>> ctx.put("theMessage",message); /* string works fine*/
> >>> ctx.put("primes",currentPrimes); /* vector works fine*/
> >>> ctx.put("Primes",numberPrimes); /* string */
> >>> ctx.put("Digits",numberDigits); /* string */
> >>>
> >>> and in my .vm I have:
> >>>
> >>> <INPUT TYPE=HIDDEN NAME="Digits" VALUE=$Digits>
> >>> <INPUT TYPE=HIDDEN NAME="Primes" VALUE=$Primes>
> >>>
> >>> But "$Digits" and "$Primes" are never substituted for their real value.
> >>
> >> What do you see?
> >
> > They show up as "$Digits" and "$Primes", not substituted...
> >
> 
> Hm.
> 
> Can you ensure that they aren't null when you put them in the context?
> 
> I assume that $theMessage does get rendered properly?

Yes, $theMessage does get rendered properly. I think the problem is that
I ask for a Refresh if thtread is not finished and when the request
comes in again after 5 secs, the first time they are what they are
supposed to be but on subsequent "Refresh" their value is not rendered.
Somehow, the rendering never takes place.
-r
> 
> --
> Geir Magnusson Jr.     geirm@optonline.net
> System and Software Consulting
> Developing for the web?  See http://jakarta.apache.org/velocity/
> If you look up, there are no limits - Japanese Proverb

Re: New Bee

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 9/19/01 6:55 PM, "Rasoul Hajikhani" <ra...@rhythm.com> wrote:

> "Geir Magnusson Jr." wrote:
>> 
>> On 9/19/01 4:51 PM, "Rasoul Hajikhani" <ra...@rhythm.com> wrote:
>> 
>>> Hi there,
>>> I am using Velocity to generate dynamic content. I am new to Velocity
>>> and have read the documentation. However, some thing very simple has
>>> escaped me. Here is my code snippet:
>>> 
>>> ctx.put("theMessage",message); /* string works fine*/
>>> ctx.put("primes",currentPrimes); /* vector works fine*/
>>> ctx.put("Primes",numberPrimes); /* string */
>>> ctx.put("Digits",numberDigits); /* string */
>>> 
>>> and in my .vm I have:
>>> 
>>> <INPUT TYPE=HIDDEN NAME="Digits" VALUE=$Digits>
>>> <INPUT TYPE=HIDDEN NAME="Primes" VALUE=$Primes>
>>> 
>>> But "$Digits" and "$Primes" are never substituted for their real value.
>> 
>> What do you see?
> 
> They show up as "$Digits" and "$Primes", not substituted...
> 

Hm.

Can you ensure that they aren't null when you put them in the context?

I assume that $theMessage does get rendered properly?

-- 
Geir Magnusson Jr.     geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
If you look up, there are no limits - Japanese Proverb


Re: New Bee

Posted by Rasoul Hajikhani <ra...@rhythm.com>.
"Geir Magnusson Jr." wrote:
> 
> On 9/19/01 4:51 PM, "Rasoul Hajikhani" <ra...@rhythm.com> wrote:
> 
> > Hi there,
> > I am using Velocity to generate dynamic content. I am new to Velocity
> > and have read the documentation. However, some thing very simple has
> > escaped me. Here is my code snippet:
> >
> > ctx.put("theMessage",message); /* string works fine*/
> > ctx.put("primes",currentPrimes); /* vector works fine*/
> > ctx.put("Primes",numberPrimes); /* string */
> > ctx.put("Digits",numberDigits); /* string */
> >
> > and in my .vm I have:
> >
> > <INPUT TYPE=HIDDEN NAME="Digits" VALUE=$Digits>
> > <INPUT TYPE=HIDDEN NAME="Primes" VALUE=$Primes>
> >
> > But "$Digits" and "$Primes" are never substituted for their real value.
> 
> What do you see?

They show up as "$Digits" and "$Primes", not substituted...

> 
> > Any ideas?
> >
> 
> Weird.
> 
> Are you 100% sure that numberPrimes and numberDigits have values?
> 
> > I apologize if this is way a rudimentary question.
> > -r
> 
> --
> Geir Magnusson Jr.     geirm@optonline.net
> System and Software Consulting
> Developing for the web?  See http://jakarta.apache.org/velocity/
> If you look up, there are no limits - Japanese Proverb

Re: New Bee

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 9/19/01 4:51 PM, "Rasoul Hajikhani" <ra...@rhythm.com> wrote:

> Hi there,
> I am using Velocity to generate dynamic content. I am new to Velocity
> and have read the documentation. However, some thing very simple has
> escaped me. Here is my code snippet:
> 
> ctx.put("theMessage",message); /* string works fine*/
> ctx.put("primes",currentPrimes); /* vector works fine*/
> ctx.put("Primes",numberPrimes); /* string */
> ctx.put("Digits",numberDigits); /* string */
> 
> and in my .vm I have:
> 
> <INPUT TYPE=HIDDEN NAME="Digits" VALUE=$Digits>
> <INPUT TYPE=HIDDEN NAME="Primes" VALUE=$Primes>
> 
> But "$Digits" and "$Primes" are never substituted for their real value.

What do you see?  

> Any ideas?
>

Weird.

Are you 100% sure that numberPrimes and numberDigits have values?
 
> I apologize if this is way a rudimentary question.
> -r

-- 
Geir Magnusson Jr.     geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
If you look up, there are no limits - Japanese Proverb