Jstl If-Statement Inside HTML-Attribute

JSTL if-statement inside HTML-attribute

It's also possible to use an EL expression directly like this:

<div class="${booleanExpr ? 'cssClass' : 'otherCssClass'}">
</div>

if...else within JSP or JSTL

Should I use JSTL ?

Yes.

You can use <c:if> and <c:choose> tags to make conditional rendering in jsp using JSTL.

To simulate if , you can use:

<c:if test="condition"></c:if>

To simulate if...else, you can use:

<c:choose>
<c:when test="${param.enter=='1'}">
pizza.
<br />
</c:when>
<c:otherwise>
pizzas.
<br />
</c:otherwise>
</c:choose>

Conditional HTML attribute in JSPX

The problem is that Jasper tries to validate well-formness of JSP before processing EL.

This happens because JSPX extension that your file supposedly has means that it is a JSP Document. And JavaServer Pages Specification says:

It is a translation-time error for a file that is identified as a JSP
document to not be a well-formed, namespace-aware, XML document.

I couldn't find any way to instruct Jasper to disable XML well-formness validation.

The Ant task to pre-compile JSP files as described in Tomcat docs has got validateXml parameter. But it just skips checks for a valid XML, not for well-formed XML.

So your options are either to rename your file to JSP, or add <is-xml>false</is-xml> to web.xml, or to follow @damo_inc's suggestion.

Conditionally set an attribute on an element with JSP Documents (JSPX)

I use a custom JSP tag with dynamic attributes. You use it like this:

<util:element elementName="button" name="btn1" disabled="$(isDisabled ? 'disabled' : '')"/>

Basically, what this tag does is generate an XML element with elementName and puts all attributes present in the tag, but skips the empty ones.

The tag itself is pretty easy to implement, my implementation is just 44 lines long.

JSTL error on when / otherwise statement multiple condition

    <tr> 
<td width="17%" class=label > <b>Place of Birth</b>  </td>
<td colspan="3" class=value> <div align="left"> <%=HtmlUtil.setListOptions("birthPlace","onchange ='javascript:SetEnabled()'",birth_place_option,birth_place_code,true,"")%>
if others, please specify remarks
<c:choose>
<c:when test="${birth_place_code != null && birth_place_code == '99'}">
<c:when test="${other_place_birth!=null}">
<input type="text" size=41 maxlength=66 name="birthPlaceT" value="<c:out value="${other_place_birth}"/>">
</c:when>
<c:otherwise>
<input type="text" size=41 maxlength=66 name="birthPlaceT" value="">
</c:otherwise>
</c:when>
<c:otherwise>
<input type="text" size=41 maxlength=66 name="birthPlaceT" value=" " disabled>
</c:otherwise>
</c:choose>
</div></td>

Problem is in your c:out. Not in c:when
You cant have ternary (or any other) conditions in c:out . It is used only to display values.

JSTL presentation problem- if statement

Here is the equivalent if-else statement of your code if the second IF in your code as the ELSE.

<c:forEach items="${row}" var="cell">
<c:choose>
<c:when test="${cell ne 'Read' or cell ne 'Write'}">
<td id="access">${cell}</td>
</c:when>
<c:otherwise>
<td>${cell}</td>
</c:otherwise>
</c:choose>
</c:forEach>

Check your conditional statement in IF. As voidnnull point out about your first IF instead of using || (or) use && (and).

how to use c:when condition inside value attribute of input tag?

<input id="textid" type="button" value="${texttest == 'Y' ? 'Add' : 'Edit'}">

JSTL dsp:input submit button value HTML entites not decoded

Use dsp:tagAttribute:

<fmt:message var="applyText" key="checkout.order.summary.coupon.apply" />
<dsp:input iclass="btn green rev validatePromoCode" id="atg_store_applyCoupon" bean="CouponFormHandler.claimCoupon" type="submit">
<dsp:tagAttribute name="value" value="${applyText}"/>
</dsp:input>


Related Topics



Leave a reply



Submit