You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Rafael Steil <ra...@insanecorp.com> on 2002/11/16 06:03:52 UTC

Accessing objects from an ArrayList

	Hi, supose I have a class:

class MyClass
{
	private String name;
	
	public MyClass(String name)
	{
		this.name = name;
	}

	public String getName()
	{
		return name;
	}
}

Now I want to make 'n' MyClass objects availiable to velocity. Why can't
I do

myArrayList.add(new MyClass("name 1"));
myArrayList.add(new MyClass("name 2"));
...
context.put("list", myArrayLIst);

and then use

#foreach ($item in list)
	$list.getName()
#end

Why should I use instead

Map map = new HashMap();
map.put("name", myClassVar.getName());
...
myArrayList.add("list", map);

and then use
#foreach ($item in $list)
	$item.name
#end

There isn't a way to acess MyClass' objects directly? 

Thanks in advance

Rafael


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: Accessing objects from an ArrayList

Posted by Rafael Steil <ra...@insanecorp.com>.
	GREAT, GREAT! :))

Thanks dude, it works!! 

Rafael

On Sat, 2002-11-16 at 15:13, Peter Romianowski wrote:
>   I think you have to declare "MyClass" as a public
> class within its own source-file (MyClass.java).
> Otherwise introspection won't work and thus the getName()-
> method can not be found.
> 
> Peter



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: Accessing objects from an ArrayList

Posted by Peter Romianowski <me...@gmx.de>.
  I think you have to declare "MyClass" as a public
class within its own source-file (MyClass.java).
Otherwise introspection won't work and thus the getName()-
method can not be found.

Peter

> -----Original Message-----
> From: Rafael Steil [mailto:rafael@insanecorp.com] 
> Sent: Saturday, November 16, 2002 11:48 AM
> To: Velocity Users List
> Subject: Re: Accessing objects from an ArrayList
> 
> 
> 
> 	Nope, the output generated is 
> 
> "$item.getName()"
> 
> I have searched in the Net and source code examples for something 
> like that, but no sucess. The relevant code partes of a simple example
> are:
> 
> ..
> class MyClass
> {
> 	private String name;
> 
> 	public MyClass(String name)
> 	{
> 		this.name = name;
> 	}
> 
> 	public String getName()
> 	{
> 		return name;
> 	}
> }
> 
> public class VelocityListProcess
> {
> 	public static void main(String args[])
> 	{
> 		try {
> 			VelocityEngine ve = new VelocityEngine();
> 			ve.init();
> 
> 			VelocityContext context = new VelocityContext();
> 			Template t = ve.getTemplate("template1.vm");
> 		
> 			List list = new ArrayList();
> 			list.add(new MyClass("Item 1"));
> 			list.add(new MyClass("Item 2"));
> 			list.add(new MyClass("Item 3"));
> 			list.add(new MyClass("Item 4"));
> 			list.add(new MyClass("Item 5"));
> 	
> 			context.put("list", list);
> 
> 			StringWriter writer = new StringWriter();
> 			t.merge(context, writer);
> 
> 			System.out.println(writer.toString());
> 		}
> 		catch (Exception e) {
> 			e.printStackTrace();
> 		}
> 	}
> }
> 
> and the template was written as:
> 
> 
> #foreach ($item in $list)
>  $item.getName()
> #end
> 
> output:
> $item.getName()
> $item.getName()
> $item.getName()
> $item.getName()
> $item.getName()
> 
> I'm a bit lost..
> 
> Rafael
> 
> > 
> > $item.getName()
> > Doesn't work?
> > 
> > -- Denis.
> > 
> > --
> > To unsubscribe, e-mail:   
> <mailto:velocity-user-> unsubscribe@jakarta.apache.org>
> > For 
> additional commands, 
> e-mail: 
> > <ma...@jakarta.apache.org>
> > 
> > 
> 
> 
> 
> --
> To unsubscribe, e-mail:   
> <mailto:velocity-user-> unsubscribe@jakarta.apache.org>
> For 
> additional commands, 
> e-mail: <ma...@jakarta.apache.org>
> 


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Accessing objects from an ArrayList

Posted by Rafael Steil <ra...@insanecorp.com>.
	Nope, the output generated is 

"$item.getName()"

I have searched in the Net and source code examples for something 
like that, but no sucess. The relevant code partes of a simple example
are:

..
class MyClass
{
	private String name;

	public MyClass(String name)
	{
		this.name = name;
	}

	public String getName()
	{
		return name;
	}
}

public class VelocityListProcess
{
	public static void main(String args[])
	{
		try {
			VelocityEngine ve = new VelocityEngine();
			ve.init();

			VelocityContext context = new VelocityContext();
			Template t = ve.getTemplate("template1.vm");
		
			List list = new ArrayList();
			list.add(new MyClass("Item 1"));
			list.add(new MyClass("Item 2"));
			list.add(new MyClass("Item 3"));
			list.add(new MyClass("Item 4"));
			list.add(new MyClass("Item 5"));
	
			context.put("list", list);

			StringWriter writer = new StringWriter();
			t.merge(context, writer);

			System.out.println(writer.toString());
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
}

and the template was written as:


#foreach ($item in $list)
 $item.getName()
#end

output:
$item.getName()
$item.getName()
$item.getName()
$item.getName()
$item.getName()

I'm a bit lost..

Rafael

> 
> $item.getName()
> Doesn't work?
> 
> -- Denis.
> 
> --
> To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>
> 
> 



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Accessing objects from an ArrayList

Posted by Denis <ji...@respublica.fr>.
Hi Rafael
samedi 16 novembre 2002, à 05:03  am, Rafael Steil a écrit :

>
> 	Hi, supose I have a class:
>
> class MyClass
> {
> 	private String name;
> 	
> 	public MyClass(String name)
> 	{
> 		this.name = name;
> 	}
>
> 	public String getName()
> 	{
> 		return name;
> 	}
> }
>
> Now I want to make 'n' MyClass objects availiable to velocity. Why 
> can't
> I do
>
> myArrayList.add(new MyClass("name 1"));
> myArrayList.add(new MyClass("name 2"));
> ...
> context.put("list", myArrayLIst);
>
> and then use
>
> #foreach ($item in list)
> 	$list.getName()
> #end

$item.getName()
Doesn't work?

-- Denis.

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>