Victor Portela
Victor Portela

Reputation: 122

Handling events in Tapestry layout

I have a problem in my tapestry project.

Every time I load one page, it triggers onActivate method if is defined in the page in question. But I don't know how to catch that event in my layout template.

If I define a variable in the layout.java, for example:

@Property
String a = "foo";

And I pick that variable value at the template (layout.tml):

<p>${a}</p>

Ok, that will print "foo" in the HTML of all pages that use that layout, but If I want to change that value every time that the page reloads, for example defining onActivate in the layout.java.

void onActivate(){
    a="bar";
}

This method doesn't trigger in the layout.java, only in the child pages when it's defined. (In the child pages I include the layout like Nathan Q says) How can I refresh the variable value?

Any ideas?

Upvotes: 0

Views: 1019

Answers (2)

Victor Portela
Victor Portela

Reputation: 122

Ok, I find a way to refresh my property value. And It was very simple:

Instead of declare a property and update that value in the onActivate method, I declare a public method in the layout.java to get that value and make the updating changes there.

private String a = "foo";

public String getA(){

    a = "bar";
    return a;
}

This way, I can make any changes to the a variable every time the page loads.

Upvotes: 0

Nathan Q
Nathan Q

Reputation: 1902

I guess layout is a component in this case. Only pages have an activation context, that's why the onActivate() is not fired.

I don't know your exact use case, but:


If it's a page specific variable then you can just pass a parameter to your Layout component.

Layout.java

@Parameter
@Property
private String a;

SomePage.tml

<html t:a="someString" t:type="Layout" ... />

SomePage.java

@Property
private String someString;

...

void onActivate() {
   someString = "something specific for this page";
}

OR

If this variable needs to be set every render and it's not a page specific value, you can just use the SetupRender event of your Layout component.

void setupRender() {
   a = ...;
}

Upvotes: 2

Related Questions