is empty / null in a tag file? I recently discovered jsp tags and use them to avoid duplicating the common p...">

How to check if <jsp: doBody / "> is empty / null in a tag file?

I recently discovered jsp tags and use them to avoid duplicating the common part of my views.

So in my JSP submissions I have

<web-component:mytag>
<!-- HTML specific to that page -->
</web-component:mytag>

And in my tag file I get HTML with <jsp:doBody/>.

I have a question, how can I check if it is <jsp:doBody/>empty so that I can put the default HTML content?

Sort of

<c:choose>
    <c:when test="${!doBody.empty}">
        <jsp:doBody/>
    </c:when>
    <c:otherwise>
    <!-- My default HTML content here -->
    </c:otherwise>
</c:choose>

Therefore, I am looking for the correct expression inserted in doBody.empty

Thanks in advance.

+6
source share
2 answers

You have three options, each with its own pros and cons.

1. <jsp:doBody var="bodyText"/>

This is probably the most common and obvious solution.

, , .

<jsp:doBody var="bodyText"/>
<c:choose>
  <c:when test="${not empty fn:trim(bodyText)}">
    ${bodyText}
  </c:when>
  <c:otherwise>
    <!-- Default content here -->
  </c:otherwise>
</c:choose>

${not empty fn:trim(bodyText)} ${empty bodyText}? @ach, , , , , trimDirectiveWhitespaces trimDirectiveWhitespaces.

Pros

  • , , , .

Cons

  • . , ( ) .

  • . .

  • ( ${not empty fn:trim(bodyText)} ${not empty fn:trim(bodyText)}. ${empty bodyText}.)

    ${empty bodyText} , ${empty bodyText} , , JSP trimDirectiveWhitespaces.

    <%@ page trimDirectiveWhitespaces="true" %>
    <my:tag value="a">
    </my:tag>
    

    trimDirectiveWhitespaces="true", , . trimDirectiveWhitespaces="false", , .

    , , / . , <my:tag> , , :

    • getJspBody() .
    • , doAfterBody() .

2.

, Java. , , .

<c:choose>
  <c:when test="<%= super.getJspBody() != null %>">
    <jsp:doBody />
  </c:when>
  <c:otherwise>
    <!-- My default HTML content here -->
  </c:otherwise>
</c:choose>

, , SimpleTagSupport (. Javax.servlet.jsp.tagext), doTag() doTag(). , super.getJspBody() == null , .

Pros

  • .

  • .

Cons

  • . ( , scripting-invalid scripting-invalid JSP.)

3.

, .

, , . getParent() :

class HasBody extends SimpleTag {
  @Override
  public void doTag() throws JspException {
    TagAdapter adapter = (TagAdapter)this.getParent();
    SimpleTagSupport parent = (SimpleTagSupport)adapter.getAdaptee();

    // 'getJspBody()' is a protected method, so actually you must use reflection.
    JspFragment body = parent.getJspBody();

    this.getJspContext().setAttribute("hasBody", body == null);
  }
}

:

<my:hasBody />
<c:choose>
  <c:when test="${hasBody}">
    <jsp:doBody />
  </c:when>
  <c:otherwise>
    <!-- Default content here -->
  </c:otherwise>
</c:choose>

Pros

  • .
  • .

Cons

  • . , .
  • .

: JSPFragment

@Ryan JSPFragment. , EL ( ), :

<%@ attribute name="frag" fragment="true" %>
<c:choose>
  <c:when test="${frag != null}">
    <jsp:invoke fragment="frag" />
  </c:when>
  <c:otherwise>
    <!-- Default content here -->
  </c:otherwise>
</c:choose>
+8

.

<jsp:doBody var="bodyText"/>
<c:if test="${not empty bodyText}">
    //will shown if its not empty
</c:if>
0

All Articles