Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
java
java
package org.example.app.components;

import org.apache.tapestry5.annotations.AfterRender;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.annotations.SetupRender;

public class Count
{
    @Parameter
    private int start = 1;

    @Parameter(required = true)
    private int end;

    @Parameter
    private int value;

    private boolean increment;

    @SetupRender
    void initializeValue()
    {
        value = start;

        increment = start < end;
    }

    @AfterRender
    boolean next()
    {
        if (increment)
        {
            int newValue = value + 1;

            if (newValue <= end)
            {
                value = newValue;
                return false; 
            }
        }
        else
        {
            int newValue = value - 1;

            if (newValue >= end)
            {
                value = newValue;
                return false; 
            }
        }

        return true;
    }
}

The name of the parameter is derived from the field name (by stripping leading "_" and "$" characters). Here, the parameter names are "start", "end" and "value".

...

Code Block
xml
xml
<t:layout xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd">
    <p> Countdown:
        <t:count start="5" end="1" value="index"> 
          ${index} ...  
        </t:count>
    </p>
</t:layout>

...

Inherited bindings are useful for complex components; they are often used when an inner component has a default value for a parameter, and the outer component wants to make it possible to override that default.

More examples on this coming soon.

Parameter Binding Defaults

Code Block
xml
xml
titleIndex.tml

<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd">
  <body>
    <div t:type="layout" t:menuTitle="literal:The Title">
      ...
    </div>
  </body>
</html>
Code Block
xml
xml
titleLayout.tml

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

	<div t:type="title" t:title="inherit:menuTitle"></div>

	<t:body />

</t:container>
Code Block
java
java
titleTitle.java

package org.example.app.components;

import org.apache.tapestry5.annotations.Parameter;

public class Title {

	@Parameter
	private String title;

}

Parameter Binding Defaults

The @Parameter annotation's value() attribute can be used to specify a binding expression that will be the default binding for the parameter is otherwise left unbound. Typically, this is the name of a The @Parameter annotation's value() attribute can be used to specify a binding expression that will be the default binding for the parameter is otherwise left unbound. Typically, this is the name of a property that that will compute the value on the fly.

...

Code Block
java
java
  @Parameter("defaultMessage")
  private String message;
  
  @Parameter(required=true)
  private int maxLength;
  
  public String getDefaultMessage()
  {
    return String.format("Maximum field length is %d.", _maxLength);
  }

...

Code Block
java
java
  @Parameter
  private String message;
  
  @Parameter(required=true)
  private int maxLength;
  
  @Inject
  private ComponentResources resources;
  
  @Inject
  private BindingSource bindingSource;
  
  Binding defaultMessage()
  {
    return bindingSource.newBinding("default value", resources, "basicMessage");
  }
  
  public String getBasicMessage()
  {
    return String.format("Maximum field length is %d.", maxLength);
  }

...

Code Block
java
java
  @Parameter
  private String message;
  
  @Parameter(required=true)
  private int maxLength;
  
  @Inject
  private ComponentResources resources;
  
  String defaultMessage()
  {
    return String.format("Maximum field length is %d.", maxLength);
  }  

This form is more like using the "literal:" binding prefix, except that the literal value is computed by the defaultMessage() method.

...

Code Block
java
java
public class MyComponent
{
  @Parameter
  private int myParam;
  
  @Inject
  private ComponentResources resources;
  
  @BeginRender
  void setup()
  {
      if (resources.isBound("myParam"))
      {
        . . .
      }
  }
}

...