Search code examples
javatapestry

How to do calculation in tapestry tml?


I am new in tapestry so sorry if I made a mistake, I already read the docs but have no clue about calculating data on frontend side.

The example that I have:

Hello.Java

public class Hello {

    @Property
    @Persist
    private int numberA;

    @Property
    @Persist
    private int numberB;

    void onPrepare(){
         this.numberA = 2;
         this.numberB = 3;
    }
}

Hello.tml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" t:type="layout" 
    xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd"
    xmlns:p="tapestry:parameter">

    Total: <input type="number" value="${numberA+numberB}"></input>

</html>

When running I got this error:

org.apache.tapestry5.ioc.internal.util.TapestryException: Could not convert 'numberA+numberB' into a component parameter binding

Any information will be appreciated.

Thank you


Solution

  • Tapestry's expression language is intentionally minimal and excludes things like math expressions. Anything much more complicated than property evaluation belongs in the corresponding component class (the Java file of the same name). To do otherwise leads to a gradually more and more messy mix of markup and code in the same file.

    Just create a simple getter -- e.g., getSum() -- that returns the sum you want, then in the tml file use the expression ${sum}