I have a zone to show possible errors that might occur in a get* method called
by Tapestry during page rendering to retrieve the value for a property (String
myValue in the example below).
So in this getMyValue() method I set the error message into another property
(@Property String errorMessage) and I call addRender to update the template
zone errorZone, where ${errorMessage} is placed.
My problem is that this call to addRender has no effect: the errorMessage is
not shown. But if I just reload the page it is shown.
Below is a testable example. I'm trying to find out what's the right way to
update a zone from a get* method called by Tapestry during page rendering.
#####################################
TestRender.tml:
#####################################
<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd">
<t:zone id="errorZone" t:id="errorZone">
<t:if t:test="isError">
<div style="background-color:
yellow;">${errorMessage}</div>
</t:if>
</t:zone>
myValue: ${myValue}
</html>
#####################################
TestRender.java:
#####################################
import java.util.Date;
import org.apache.tapestry5.PersistenceConstants;
import org.apache.tapestry5.annotations.InjectComponent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.corelib.components.Zone;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.ajax.AjaxResponseRenderer;
public class TestRender
{
@Persist(PersistenceConstants.FLASH)
@Property
private boolean isError;
@Persist(PersistenceConstants.FLASH)
@Property
private String errorMessage;
@InjectComponent
@Property
private Zone errorZone;
@Inject
private AjaxResponseRenderer ajaxResponseRenderer;
@Persist
private String myValue;
public void setupRender()
{
System.out.println(new Date() + " setupRender() was executed. It
does nothing at all.");
}
public String getMyValue()
{
if (this.myValue == null)
{
System.out.println(new Date() + " myValue was null, so
getMyValue() is setting it...");
this.myValue = "My value at " + new Date();
this.isError = true;
this.errorMessage = "Let's say there was an error so I
want to show this error message.";
this.ajaxResponseRenderer.addRender(this.errorZone);
/* I expected this addRender to cause the error message to
be shown right away
without the need to reload the page, but this is not
the case. */
System.out.println(new Date() + " getMyValue() has just
called addRender.");
}
else
{
System.out.println(new Date() + " myValue was NOT null, so
getMyValue() did nothing at all.");
}
return this.myValue;
}
}