views:

647

answers:

2

Is having tags inside a jstl tag considered bad form? So for example I know the following will work but is it considered bad form to have the script tags inside my jstl tags in my jsp ?

<c:choose>
  <c:when test="${!empty bean.value}">
    <p>Its not empty</p>
  </c:when>
  <c:otherwise>
    **<script>
        callJSSomeFunction();
    </script>**
  </c:otherwise>
</c:choose>
+1  A: 

I don't think so - this is how you conditionally do things in JSTL, I don't think you should have a script tag just to have one.

stevedbrown
+1  A: 

I don't know about bad form (a bit harsh) but you might consider that to someone viewing your page without JS enabled your <c:otherwise> will effectively output nothing, and that's not very graceful.

Also, if the page is rendered incrementally your function call might execute as it has been ouput, and before the DOM has been fully loaded by the browser and thus behave unpredictably (or just not work at all - I've had this happen).

I would consider putting all function invocations in the head, and use one of the many available tricks for detecting when the DOM has loaded (jQuery's $(document).ready() for example) to enforce a neater separation, and make your life that much easier.

karim79