You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Steven Tönsing <st...@artundweise.de> on 2009/12/03 10:21:16 UTC

collection of components within a page (Tapestry 5.1)

Hi,
i'm completely new to tapestry and i got a very fundamental (for my  
project) question.

So this is my sitiation :
I'm using tapestry 5.1 and i have a collection of components (Boxes)  
which can be placed on a page (in a certain order) the only thing i  
whant to do now is to get this collection of components rendered in  
the markup (template) of the page.

Pretty much like this

<t:loop source="boxes" value="var:whatever">
	<t:box source="var:whatever">
</t:loop>

but this is obviously wrong. Is there a way to do so ?

thanks,
steve

Structure :

de.steven.tapestry.components.Box.java
de.steven.tapestry.components.Box.tml

de.steven.tapestry.pages.Overwiew.java
de.steven.tapestry.pages.Overwiew.tml

Source :

de.steven.tapestry.pages.Overview.java

public class Overview{

	private List<Box> boxes;

	public Overview(){
		boxes = new ArrayList<Box>();
	
		System.out.println("creating some boxes");
		boxes.add( new Box("box 1") );
		boxes.add( new Box("box 2") );
		boxes.add( new Box("box 3") );
		boxes.add( new Box("box 4") );
	}

	public List<Box> getBoxes() {
		return boxes;
	}

	public void setBoxes(List<Box> boxes) {
		this.boxes = boxes;
	}

}

de.steven.tapestry.compoents.Box.java

public class Box {
	@Parameter
	@Property
	private String name = "noname";

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


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: collection of components within a page (Tapestry 5.1)

Posted by Ville Virtanen <vi...@cerion.fi>.
Hi again,

also forgot to mention great source to learn from:
http://jumpstart.doublenegative.com.au:8080/jumpstart/

(http://jumpstart.doublenegative.com.au/home.html)

 - Ville


Steven Tönsing wrote:
> 
> Hi,
> i'm completely new to tapestry and i got a very fundamental (for my  
> project) question.
> 
> So this is my sitiation :
> I'm using tapestry 5.1 and i have a collection of components (Boxes)  
> which can be placed on a page (in a certain order) the only thing i  
> whant to do now is to get this collection of components rendered in  
> the markup (template) of the page.
> 
> Pretty much like this
> 
> <t:loop source="boxes" value="var:whatever">
> 	<t:box source="var:whatever">
> </t:loop>
> 
> but this is obviously wrong. Is there a way to do so ?
> 
> thanks,
> steve
> 
> Structure :
> 
> de.steven.tapestry.components.Box.java
> de.steven.tapestry.components.Box.tml
> 
> de.steven.tapestry.pages.Overwiew.java
> de.steven.tapestry.pages.Overwiew.tml
> 
> Source :
> 
> de.steven.tapestry.pages.Overview.java
> 
> public class Overview{
> 
> 	private List<Box> boxes;
> 
> 	public Overview(){
> 		boxes = new ArrayList<Box>();
> 	
> 		System.out.println("creating some boxes");
> 		boxes.add( new Box("box 1") );
> 		boxes.add( new Box("box 2") );
> 		boxes.add( new Box("box 3") );
> 		boxes.add( new Box("box 4") );
> 	}
> 
> 	public List<Box> getBoxes() {
> 		return boxes;
> 	}
> 
> 	public void setBoxes(List<Box> boxes) {
> 		this.boxes = boxes;
> 	}
> 
> }
> 
> de.steven.tapestry.compoents.Box.java
> 
> public class Box {
> 	@Parameter
> 	@Property
> 	private String name = "noname";
> 
> 	public Box(){
> 		
> 	}
> 	
> 	public Box(String name){
> 		this.name = name;
> 	}
> }
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
> 
> 
> 

-- 
View this message in context: http://old.nabble.com/collection-of-components-within-a-page-%28Tapestry-5.1%29-tp26623064p26629675.html
Sent from the Tapestry - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: collection of components within a page (Tapestry 5.1)

Posted by Josh Canfield <jo...@thedailytube.com>.
>> Pretty much like this
>> <t:loop source="boxes" value="var:whatever">
>>        <t:box source="var:whatever">
>> </t:loop>
>>
>> but this is obviously wrong. Is there a way to do so ?
>
> This looks obviously right to me. :) What could be wrong about placing a
> component inside a loop or a grid? For each iteration, a new instance be be
> created.

Component instances are created when your page is initially parsed,
not during rendering. So in this case you get one instance of the
component, not an instance for each iteration. This is important
because if you have local variables in your component they are not
reset by tapestry for each iteration of the loop. You can test this
with a simple Counter component.

public class Counter {

    @Property
    private int _count;

    int defaultCount() {
        return 0;
    }

    void setupRender() {
        _count++;
    }

    void beginRender(MarkupWriter writer) {
        writer.element("div");
        writer.write(Integer.toString(_count));
        writer.end();
    }
}

Josh


On Thu, Dec 3, 2009 at 3:28 AM, Thiago H. de Paula Figueiredo
<th...@gmail.com> wrote:
> Em Thu, 03 Dec 2009 07:21:16 -0200, Steven Tönsing
> <st...@artundweise.de> escreveu:
>
>> Hi,
>
> Hi!
>
>> So this is my sitiation :
>> I'm using tapestry 5.1 and i have a collection of components (Boxes) which
>> can be placed on a page (in a certain order) the only thing i whant to do
>> now is to get this collection of components rendered in the markup
>> (template) of the page.
>
>>                boxes.add( new Box("box 1") );
>
> Don't forget that you *cannot* instantiate pages or components directly.
>
>> Pretty much like this
>> <t:loop source="boxes" value="var:whatever">
>>        <t:box source="var:whatever">
>> </t:loop>
>>
>> but this is obviously wrong. Is there a way to do so ?
>
> This looks obviously right to me. :) What could be wrong about placing a
> component inside a loop or a grid? For each iteration, a new instance be be
> created.
>
>> public class Box {
>>        @Parameter
>>        @Property
>>        private String name = "noname";
>
> This isn't the right way of giving a default value to a parameter. Anyway,
> it is almost always a bad idea to initialize page or component fields in
> their declaration. You shoud use the value attribute of @Parameter to do
> that.
>
> --
> Thiago H. de Paula Figueiredo
> Independent Java, Apache Tapestry 5 and Hibernate consultant, developer, and
> instructor
> Owner, software architect and developer, Ars Machina Tecnologia da
> Informação Ltda.
> http://www.arsmachina.com.br
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>



-- 
--
TheDailyTube.com. Sign up and get the best new videos on the internet
delivered fresh to your inbox.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: collection of components within a page (Tapestry 5.1)

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
Em Thu, 03 Dec 2009 07:21:16 -0200, Steven Tönsing  
<st...@artundweise.de> escreveu:

> Hi,

Hi!

> So this is my sitiation :
> I'm using tapestry 5.1 and i have a collection of components (Boxes)  
> which can be placed on a page (in a certain order) the only thing i  
> whant to do now is to get this collection of components rendered in the  
> markup (template) of the page.

> 		boxes.add( new Box("box 1") );

Don't forget that you *cannot* instantiate pages or components directly.

> Pretty much like this
> <t:loop source="boxes" value="var:whatever">
> 	<t:box source="var:whatever">
> </t:loop>
>
> but this is obviously wrong. Is there a way to do so ?

This looks obviously right to me. :) What could be wrong about placing a  
component inside a loop or a grid? For each iteration, a new instance be  
be created.

> public class Box {
> 	@Parameter
> 	@Property
> 	private String name = "noname";

This isn't the right way of giving a default value to a parameter. Anyway,  
it is almost always a bad idea to initialize page or component fields in  
their declaration. You shoud use the value attribute of @Parameter to do  
that.

-- 
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,  
and instructor
Owner, software architect and developer, Ars Machina Tecnologia da  
Informação Ltda.
http://www.arsmachina.com.br

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: collection of components within a page (Tapestry 5.1)

Posted by Ville Virtanen <vi...@cerion.fi>.
Hi,

Don't loop your components, loop your data. If it is the name of the
component that is your data, then loop over the names and give those as
parameter to your boxes. Normally it would be orders or products or users or
what not that are looped through.

You don't have to give unique identifiers to your components if you don't
want to, so you can just have components without any parameters. (Obviously
:))

Take a look at http://tapestry.apache.org/tapestry5/tutorial1/hilo.html it
shows how you can make a loop that loops 10 times and creates new actionlink
every time. (Creating guessable links part) Source is the list that you
loop, and value is the variable that contains one list item at every
iteration of the loop. (So your component can be given one object at a time
as parameter from the list.) Tapestry takes care of your component
instantiation and instrumentation if you just follow the normal project
layout. (So that T5 knows which classes are components / pages. You can of
course use non-standard packages and instruct T5 to treat any class as
component.)

Hopefully this clarifies it a bit.

 - Ville


Steven Tönsing wrote:
> 
> Hi,
> i'm completely new to tapestry and i got a very fundamental (for my  
> project) question.
> 
> So this is my sitiation :
> I'm using tapestry 5.1 and i have a collection of components (Boxes)  
> which can be placed on a page (in a certain order) the only thing i  
> whant to do now is to get this collection of components rendered in  
> the markup (template) of the page.
> 
> Pretty much like this
> 
> <t:loop source="boxes" value="var:whatever">
> 	<t:box source="var:whatever">
> </t:loop>
> 
> but this is obviously wrong. Is there a way to do so ?
> 
> thanks,
> steve
> 
> Structure :
> 
> de.steven.tapestry.components.Box.java
> de.steven.tapestry.components.Box.tml
> 
> de.steven.tapestry.pages.Overwiew.java
> de.steven.tapestry.pages.Overwiew.tml
> 
> Source :
> 
> de.steven.tapestry.pages.Overview.java
> 
> public class Overview{
> 
> 	private List<Box> boxes;
> 
> 	public Overview(){
> 		boxes = new ArrayList<Box>();
> 	
> 		System.out.println("creating some boxes");
> 		boxes.add( new Box("box 1") );
> 		boxes.add( new Box("box 2") );
> 		boxes.add( new Box("box 3") );
> 		boxes.add( new Box("box 4") );
> 	}
> 
> 	public List<Box> getBoxes() {
> 		return boxes;
> 	}
> 
> 	public void setBoxes(List<Box> boxes) {
> 		this.boxes = boxes;
> 	}
> 
> }
> 
> de.steven.tapestry.compoents.Box.java
> 
> public class Box {
> 	@Parameter
> 	@Property
> 	private String name = "noname";
> 
> 	public Box(){
> 		
> 	}
> 	
> 	public Box(String name){
> 		this.name = name;
> 	}
> }
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
> 
> 
> 

-- 
View this message in context: http://old.nabble.com/collection-of-components-within-a-page-%28Tapestry-5.1%29-tp26623064p26629581.html
Sent from the Tapestry - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: collection of components within a page (Tapestry 5.1)

Posted by Geoff Callender <ge...@gmail.com>.
Hi Steven, give this a try:


Box.java:

package myproject.model;

public class Box {
	private String name = "noname";

	public Box() {
	}

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

	public String getName() {
		return name;
	}
}


BoxOutput.java:

package myproject.components;

import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.annotations.Property;

import myproject.model.Box;

public class BoxOutput {

	@SuppressWarnings("unused")
	@Property
	@Parameter
	private Box box;

}


BoxOutput.tml:

<t:container xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">

	<div style="height: 100px; width: 100px; background: aqua; text-align: center; margin: 10px; ">
		${box.name}
	</div>

</t:container>


Overview.java:

package myproject.web.pages;

import java.util.ArrayList;
import java.util.List;

import org.apache.tapestry5.annotations.Property;

import myproject.model.Box;

public class Overview {

	@Property
	private List<Box> boxes;

	@SuppressWarnings("unused")
	@Property
	private Box box;

	void setupRender() {
		boxes = new ArrayList<Box>();

		System.out.println("creating some boxes");
		boxes.add(new Box("box 1"));
		boxes.add(new Box("box 2"));
		boxes.add(new Box("box 3"));
		boxes.add(new Box("box 4"));
	}

}


Overview.tml:

<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd" xmlns:p="tapestry:parameter">

	<t:loop source="boxes" value="box">
		<t:boxoutput box="box"/>
	</t:loop>
	
</html>

HTH,

Geoff
http://jumpstart.doublenegative.com.au


On 03/12/2009, at 8:21 PM, Steven Tönsing wrote:

> Hi,
> i'm completely new to tapestry and i got a very fundamental (for my project) question.
> 
> So this is my sitiation :
> I'm using tapestry 5.1 and i have a collection of components (Boxes) which can be placed on a page (in a certain order) the only thing i whant to do now is to get this collection of components rendered in the markup (template) of the page.
> 
> Pretty much like this
> 
> <t:loop source="boxes" value="var:whatever">
> 	<t:box source="var:whatever">
> </t:loop>
> 
> but this is obviously wrong. Is there a way to do so ?
> 
> thanks,
> steve
> 
> Structure :
> 
> de.steven.tapestry.components.Box.java
> de.steven.tapestry.components.Box.tml
> 
> de.steven.tapestry.pages.Overwiew.java
> de.steven.tapestry.pages.Overwiew.tml
> 
> Source :
> 
> de.steven.tapestry.pages.Overview.java
> 
> public class Overview{
> 
> 	private List<Box> boxes;
> 
> 	public Overview(){
> 		boxes = new ArrayList<Box>();
> 	
> 		System.out.println("creating some boxes");
> 		boxes.add( new Box("box 1") );
> 		boxes.add( new Box("box 2") );
> 		boxes.add( new Box("box 3") );
> 		boxes.add( new Box("box 4") );
> 	}
> 
> 	public List<Box> getBoxes() {
> 		return boxes;
> 	}
> 
> 	public void setBoxes(List<Box> boxes) {
> 		this.boxes = boxes;
> 	}
> 
> }
> 
> de.steven.tapestry.compoents.Box.java
> 
> public class Box {
> 	@Parameter
> 	@Property
> 	private String name = "noname";
> 
> 	public Box(){
> 		
> 	}
> 	
> 	public Box(String name){
> 		this.name = name;
> 	}
> }
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>