Apache Tapestry - IF with several conditions
Asked Answered
B

3

6

I need to get 2 java fields in the one Tapestry table column. Every of my fields can be null. Can I write if condition in single line (2 fields in one IF operator), or I must write inner condition for second field?

Now I have this:

<t:if test="${subject.subjectQuantity}">
    <t:if test="${subject.unitMeasure}">
        <tr>
            <td>Subject count:</td>
            <td>${subject.subjectQuantity} ${subject.unitMeasure}</td>
        </tr>
    </t:if>
</t:if>
Brockbrocken answered 16/3, 2015 at 8:38 Comment(0)
I
6

JAVA

public boolean isSubjectQuantityAndUnitMeasurePopulated() {
    return subject.subjectQuantity != null && subject.unitMeasure != null;
}

TML

<t:if test="subjectQuantityAndUnitMeasurePopulated">
    <tr>
        <td>Subject count:</td>
        <td>${subject.subjectQuantity} ${subject.unitMeasure}</td>
    </tr>
</t:if>
Illfounded answered 18/3, 2015 at 14:48 Comment(0)
E
1

You can have any no of condition in your java code. Please refer below code.

Scrub.tml

  <t:if test="sitelistUtility">
        <label> ${sitelist.utility.name}</label>
  </t:if>

Scrub.java

public boolean isSitelistUtility() {
      return sitelist != null && sitelist.getUtility() != null;
  }
Earlap answered 23/1, 2020 at 7:4 Comment(0)
E
-1

This is a good example of when you should put the logic in your component class rather than in the template. Just create a getter that returns the String that you want to display. Put your conditions in that getter.

Eng answered 16/3, 2015 at 11:17 Comment(2)
Thanks for advice. It is just for example. I want to know is it possible to set more than one condition, and how to do it.Brockbrocken
To my knowledge there isn't a way to have 2 tests in one "if". By the way, you shouldn't use the ${...} syntax for component parameters. It causes an unneeded type coercion to String. It should be just <t:if test="subject.subjectQuantity">Eng

© 2022 - 2024 — McMap. All rights reserved.