You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Elton Hughes <eh...@novaworks.org> on 2005/04/06 20:30:04 UTC

Odd Behavior

Hello All,

I am learning Velocity and I am experiencing some odd behavior. I am 
getting output from the template, but just the last line. The template 
has this:

----------
My First Name is $firstName
My Last Name is $lastName
----------

The program is this:

----------
import java.io.StringWriter;
import java.io.Writer;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;

public class ContextChaining {

	public static void main(String[] args) throws Exception {
	
		// initialize Velocity
		Velocity.init();
		
		// get the template
		Template template = Velocity.getTemplate("ContextChaining.vm");
		
		// create two contexts for Velocity
		VelocityContext context1 = new VelocityContext();
		VelocityContext context2 = new VelocityContext(context1);
		
		// set the who variable
		context1.put("firstName", "Gandalf");
		context2.put("lastName", "Stormcrow");
		
		// create the output
		Writer writer = new StringWriter();
		template.merge(context2, writer);
		
		// write out
		System.out.println(writer.toString());
		
	} // end of main method
	
} // End of class
----------

Any thoughts?

Elton
=========================================================
  NOVA                        505 W. Olive Ave. Suite 550
  Elton Hughes (IT)			          Sunnyvale CA 94086
  Phone: 408-730-7235                   Fax: 408-730-7643
---------------------------------------------------------


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


Re: Odd Behavior - Solved!

Posted by Elton Hughes <eh...@novaworks.org>.
Hi Florin,

I believe Macs use \r while Unix based systems use \n.

Elton

On Apr 6, 2005, at 10:16 PM, Florin Vancea wrote:

> Sorry for OT, but just curious: What's the EOL char (or chars) on Mac?
>
> ----- Original Message -----
> From: "Elton Hughes" <eh...@novaworks.org>
> To: "Velocity Users List" <ve...@jakarta.apache.org>
> Sent: Wednesday, April 06, 2005 11:39 PM
> Subject: Re: Odd Behavior - Solved!
>
>
>> Hi Mike,
>>
>> Thank you for the suggestion and thank you for the time you put into
>> this, but that did not work either.
>>
>> But here is what did work.
>>
>> I use a Mac running MacOS 10.3.8. I am using BBEdit as my text editor.
>> It defaults to using Mac line endings. When I changed the line endings
>> of the template to Unix line endings, it worked. Both lines appeared. 
>> I
>> did not even touch the Velocity application.
>>
>> So, it appears that the template requires either Unix or Windows line
>> endings.
>>
>> Thank you for your help.
>>
>> Elton
>>
>> On Apr 6, 2005, at 12:14 PM, Mike Curwen wrote:
>>
>>> I've just copy and pasted your original code (with one modification 
>>> to
>>> get
>>> the resource loader path where I wanted it), and it "works for me".
>>>
>>> The one mod:
>>> Velocity.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, 
>>> "c:\\tmpl\\");
>>> immediately before the call to Velocity.init();
>>>
>>> I get both firstName and lastName output, nothing clipped.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
>> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>
>


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


Re: Odd Behavior - Solved!

Posted by Florin Vancea <fv...@maxiq.ro>.
Sorry for OT, but just curious: What's the EOL char (or chars) on Mac?

----- Original Message ----- 
From: "Elton Hughes" <eh...@novaworks.org>
To: "Velocity Users List" <ve...@jakarta.apache.org>
Sent: Wednesday, April 06, 2005 11:39 PM
Subject: Re: Odd Behavior - Solved!


> Hi Mike,
> 
> Thank you for the suggestion and thank you for the time you put into 
> this, but that did not work either.
> 
> But here is what did work.
> 
> I use a Mac running MacOS 10.3.8. I am using BBEdit as my text editor. 
> It defaults to using Mac line endings. When I changed the line endings 
> of the template to Unix line endings, it worked. Both lines appeared. I 
> did not even touch the Velocity application.
> 
> So, it appears that the template requires either Unix or Windows line 
> endings.
> 
> Thank you for your help.
> 
> Elton
> 
> On Apr 6, 2005, at 12:14 PM, Mike Curwen wrote:
> 
> > I've just copy and pasted your original code (with one modification to 
> > get
> > the resource loader path where I wanted it), and it "works for me".
> >
> > The one mod:
> > Velocity.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, "c:\\tmpl\\");
> > immediately before the call to Velocity.init();
> >
> > I get both firstName and lastName output, nothing clipped.
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
> 
> 


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


Re: Odd Behavior - Solved!

Posted by Elton Hughes <eh...@novaworks.org>.
Hi Mike,

Thank you for the suggestion and thank you for the time you put into 
this, but that did not work either.

But here is what did work.

I use a Mac running MacOS 10.3.8. I am using BBEdit as my text editor. 
It defaults to using Mac line endings. When I changed the line endings 
of the template to Unix line endings, it worked. Both lines appeared. I 
did not even touch the Velocity application.

So, it appears that the template requires either Unix or Windows line 
endings.

Thank you for your help.

Elton

On Apr 6, 2005, at 12:14 PM, Mike Curwen wrote:

> I've just copy and pasted your original code (with one modification to 
> get
> the resource loader path where I wanted it), and it "works for me".
>
> The one mod:
> Velocity.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, "c:\\tmpl\\");
> immediately before the call to Velocity.init();
>
> I get both firstName and lastName output, nothing clipped.


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


RE: Odd Behavior

Posted by Mike Curwen <g_...@globallyboundless.com>.
I've just copy and pasted your original code (with one modification to get
the resource loader path where I wanted it), and it "works for me".
 
The one mod:
Velocity.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, "c:\\tmpl\\");
immediately before the call to Velocity.init();

I get both firstName and lastName output, nothing clipped.

JDK1.4.2_04
Velocity 1.3.1

> -----Original Message-----
> From: Elton Hughes [mailto:ehughes@novaworks.org] 
> Sent: Wednesday, April 06, 2005 1:45 PM
> To: Velocity Users List
> Subject: Re: Odd Behavior
> 
> 
> Hi Mike,
> 
> I just tried it, but it had no effect on the output.
> 
> Thanks for you effort.
> 
> Elton
> 
> On Apr 6, 2005, at 11:41 AM, Mike Curwen wrote:
> 
> > have you tried a call to writer.flush() , before System.out.println?
> > 		writer.flush()  // ?
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
> 


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


Re: Odd Behavior

Posted by Elton Hughes <eh...@novaworks.org>.
Hi Mike,

I just tried it, but it had no effect on the output.

Thanks for you effort.

Elton

On Apr 6, 2005, at 11:41 AM, Mike Curwen wrote:

> have you tried a call to writer.flush() , before System.out.println?
> 		writer.flush()  // ?


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


RE: Odd Behavior

Posted by Mike Curwen <g_...@globallyboundless.com>.
have you tried a call to writer.flush() , before System.out.println?


> -----Original Message-----
> From: Elton Hughes [mailto:ehughes@novaworks.org] 
> Sent: Wednesday, April 06, 2005 1:30 PM
> To: velocity-user@jakarta.apache.org
> Subject: Odd Behavior
> 
> 
> 

<snip />
 
> 		// create the output
> 		Writer writer = new StringWriter();
> 		template.merge(context2, writer);
> 
		writer.flush()  // ?
		
> 		// write out
> 		System.out.println(writer.toString());
> 		
> 	} // end of main method
> 	
> } // End of class
> ----------
> 
>


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