JSP Tag Libraries

Скачать в pdf «JSP Tag Libraries»


<cond:if id=»someid» object=»…» property=»…»>


Some JSP if the condition is true.


</cond:if>


<cond:else id=»someid»>


Some JSP if the condition is false.


</cond:else>


Another possible alternative is:


<cond:if object=»…» property=»…»>


Some JSP if the condition is true.


<cond:else>


Some JSP if the condition is false.


</cond:else>


</cond:if>


Though legal JSP, both options have problems that render them undesirable. Namely:


■    In the first option, there is no implicit link between if and else. To rectify this, we had to link if and else explicitly by supplying an id in <cond:if> and referencing this id in <cond:else>. This syntax is somewhat messy and places an unneeded burden on the JSP developer.


   The second option, though possible if we use a BodyTag to implement our <cond:if> and <cond:else> tags, runs the risk of introducing unwanted side effects. Since <cond:else> resides within the body of <cond:if>, the <cond:if> must always evaluate its body to determine whether a <cond:else> exists within it. Hence, the body of the <cond:if> is evaluated, even if the condition is false (in which case, it would be evaluated and ignored). This is a problem, because evaluating the body even when the condition is false will cause any scriptlets or tags within the body to be executed, potentially causing side effects like throwing an exception or wasting processing time. It’s similar to having a standard Java if statement that executes both branches when evaluated, but returns the results of only the proper branch.


   In both cases, the type of condition you impose is bound to be simple since the number of attributes in the tags is becoming unmanageable (we need attributes to point to the property/object, to link the if and else, and to specify a complex condition).

Скачать в pdf «JSP Tag Libraries»