Announcement Announcement Module
Collapse
No announcement yet.
JSTL or not to test object values? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • JSTL or not to test object values?

    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.

    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;
        }
    ...
    
    }
    Then a JSP which differently displays the evaluation whether it is unevaluated, partly evaluated et completly evaluated.
    So to test the evaluation state you have 2 different possibilities:

    The first one:


    Code:
    <c&#58;choose>
    	<c&#58;when test="$&#123;evaluation.firstCriterion != '-1' && evaluation.secondCriterion != '-1' && evaluation.thirdCriterion != '-1'&#125;">
    		<!--display for a complete evaluation -->
    	</c&#58;when>
    	<c&#58;when test="$&#123;evaluation.firstCriterion == '-1' && evaluation.secondCriterion == '-1' && evaluation.thirdCriterion == '-1'&#125;">
    		<!--display for an undefined evaluation -->
    	</c&#58;when>
    	<c&#58;otherwise>
    		<!--display for an incomplete evaluation -->
    	</c&#58;otherwise>
    </c&#58;choose>
    And the second one:
    Code:
    <%
    if &#40;evaluation.isComplete&#40;&#41;&#41; &#123;
    %>
    <!--display for a complete evaluation -->
    <%
    &#125; else if &#40;evaluation.isUndefined&#40;&#41;&#41; &#123;
    %>
    <!--display for an undefined evaluation -->
    <%
    &#125; else &#123;
    %>
    <!--display for an incomplete evaluation -->
    <%
    &#125;
    %>
    But my two solutions have weaknesses.

    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

  • #2
    Why can't you use ${evaluation.undefined} etc. in your JSTL option?

    Comment


    • #3
      Oh!

      I didn't know it was possible. It's wonderfull!
      Thanks a lot!!

      Indeed, I have tried ${evaluation.isUndefined} instead of ${evaluation.undefined}

      But where can I find a doc which explain this? In the bean specifications?

      So for incredulous people, I sum up:
      if you have a method
      Code:
      public boolean isSomething&#40;&#41;
      you can use
      Code:
      <c&#58;when test="$&#123;myObj.something&#125;">
      in your JSP!

      But your method must absolutly be called isXxxxx.
      And it doesn't work with a name like hasXxxx

      Comment

      Working...
      X