开发者

How can I present a boolean as "yes" or "no"?

开发者 https://www.devze.com 2023-01-02 08:45 出处:网络
How can I replace true and false with yes or no using JSP and JSTL? I have 开发者_高级运维values TRUE and FALSE in my table.

How can I replace true and false with yes or no using JSP and JSTL?

I have 开发者_高级运维values TRUE and FALSE in my table.

I want when I retrieve these values using jstl on my jsp pages the true false will replace with YES AND NO


I would suggest using a tagfile.

Create the tagfile, (say, /WEB-INF/tags/yesno.tag) with something like this:

<%@ attribute name="value" type="java.lang.Boolean" required="true" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:choose><c:when test="${value}">yes</c:when><c:otherwise>no</c:otherwise></c:choose>

Then in your JSP:

<%@ taglib prefix="tags" tagdir="/WEB-INF/tags"%>

<tags:yesno value="${MyBoolean}"/>

The tagfile is a bit cumbersome, but it's well encapsulated and reusable.


Another option is to take advantage of JSP Expression Language and use the following in your JSPs:

${myBoolean ? 'Yes' : 'No'}
0

精彩评论

暂无评论...
验证码 换一张
取 消