Hi,
I wonder what is the best way to test an object state in a JSP. I explain. You can whether use the test attribute with a jstl tag or use a classic if.
For exemple:
first of all, imagine an object "evaluation" which can be in different states and which have methods to test its state. Moreover its state depends on severals attributes so it's more complexe to determine.
Then a JSP which differently displays the evaluation whether it is unevaluated, partly evaluated et completly evaluated.Code:public class Evaluation { private int firstCriterion; private int secondCriterion; private int thirdCriterion; private static int VERY_GOOD = 3; private static int GOOD = 2; private static int NO_GOOD = 1; private static int VERY_BAD = 0; private static int UNEVALUATED_CRITERIA = -1; /** * Tests whether the evaluation is undefined (ie none criterion is evaluated) or not */ public boolean isUndefined() { return (!isFirstCriterionEvaluated() && !isSecondCriterionEvaluated() && !isThirdCriterionEvaluated()); } /** * Tests whether the evaluation is complete (ie all criterions are evaluated) or not */ public boolean isComplete() { return (isFirstCriterionEvaluated() && isSecondCriterionEvaluated() && isThirdCriterionEvaluated()); } /** * Tests whether the evaluation is incomplete (ie some criterions are evaluated and some aren't) or not */ public boolean isIncomplete() { return (!isUndefined() && !isComplete()); } /** * Tests whether the first criteria is evaluated */ public boolean isFirstCriterionEvaluated() { return firstCriterion != UNEVALUATED_CRITERIA; } ... }
So to test the evaluation state you have 2 different possibilities:
The first one:
And the second one:Code:<c:choose> <c:when test="${evaluation.firstCriterion != '-1' && evaluation.secondCriterion != '-1' && evaluation.thirdCriterion != '-1'}"> <!--display for a complete evaluation --> </c:when> <c:when test="${evaluation.firstCriterion == '-1' && evaluation.secondCriterion == '-1' && evaluation.thirdCriterion == '-1'}"> <!--display for an undefined evaluation --> </c:when> <c:otherwise> <!--display for an incomplete evaluation --> </c:otherwise> </c:choose>
But my two solutions have weaknesses.Code:<% if (evaluation.isComplete()) { %> <!--display for a complete evaluation --> <% } else if (evaluation.isUndefined()) { %> <!--display for an undefined evaluation --> <% } else { %> <!--display for an incomplete evaluation --> <% } %>
The first one dont' allows to use the 3 methods which test the evaluation state and force to rewrite them for each use. More over if you decide to change the type or the value of the 3 criterions or to add new criterion, in the Evaluation class, you have to rewrite all your test in all your JSP.
The second one don't allows to use JSTL (or I don't know how)
So my question is : is there a third solution? Or what is the best of my two ones?
Thank you for your advice.
Lor


Reply With Quote