0

Is there a JSTL tag specifically for displaying messages, and only if there are messages to display? So that I don't have to surround the displaying with an "c:if" tag.

If so, how do I use it? How should I add the messages to be displayed?

Henrique Ordine
  • 3,337
  • 4
  • 44
  • 70

1 Answers1

1

This sounds like a job for c:out. For example, this should display the value of person.name, or nothing if it is null.

    <c:out value="${person.name}" />

If you want to display a default value in the case of a null, then:

    <c:out value="${person.name}" default="no name" />

or

    <c:out value="${person.name}">no name</c:out>

(If this doesn't answer your question, you need to be more explicit about what you mean by "messages" ... and how you want them to be displayed.)


If you have zero or more messages in a collection:

    <c:forEach items="${messages}" var="message">
        <c:out value="${message}"/>
    </c:forEach>
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Thanks, but what if I have multiple messages? I mean, if I have a Collection of messages, and I want to display them as a list. – Henrique Ordine Oct 10 '12 at 10:14
  • @HenriqueOrdine, you could use a simple EL expression checking if the collection is empty, like: `${not empty yourCollection} ? 'not empty' : 'empty'` – Zaki Oct 10 '12 at 11:01
  • @Zaki, this way if my collection is not empty, instead of displaying a list of messages on the page, I'd just display the String "not empty". – Henrique Ordine Oct 10 '12 at 11:37
  • @HenriqueOrdine, it's a ternary expression in EL; you could test if the collection is empty or not empty,for example: `${empty yourCollection} ? 'do your thing here if its empty' : 'do your thing here if not empty'` – Zaki Oct 10 '12 at 11:49
  • Thanks, but I'm afraid I don't understand what you mean by 'do your thing here if not empty'. That just looks like a string to me. – Henrique Ordine Oct 10 '12 at 12:03