Results 1 to 3 of 3

Thread: JSTL or not to test object values?

  1. #1
    Join Date
    Feb 2005
    Location
    Toulouse, France
    Posts
    5

    Default 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. #2
    Join Date
    Aug 2004
    Location
    Carlisle, UK
    Posts
    184

    Default

    Why can't you use ${evaluation.undefined} etc. in your JSTL option?
    Chris Harris
    Carlisle, UK

  3. #3
    Join Date
    Feb 2005
    Location
    Toulouse, France
    Posts
    5

    Default

    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

Similar Threads

  1. Replies: 2
    Last Post: Oct 10th, 2005, 05:12 PM
  2. Spring container fails with no exception
    By naor in forum Container
    Replies: 9
    Last Post: Oct 1st, 2005, 03:39 PM
  3. EHCaching Hibernate
    By dencamel in forum Data
    Replies: 3
    Last Post: Sep 6th, 2005, 09:03 PM
  4. Loosing my SecureContext
    By sklakken in forum Security
    Replies: 3
    Last Post: Jul 21st, 2005, 01:44 PM
  5. Replies: 2
    Last Post: May 5th, 2005, 09:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •