JSP tag file that either outputs its body or returns it in a variable

前端 未结 3 1384
一生所求
一生所求 2021-02-20 00:59

I have a custom tag in a \".tag\" file that computes and outputs a value. Because I cannot post the code here, let\'s assume a simple example.

Content of file mytag.tag:

相关标签:
3条回答
  • 2021-02-20 01:41

    Based on another answer, a variable alias seems to be a great workaround. Here is a simple example:

    <%@ attribute name="urlValue" required="true" type="java.lang.String" %>
    <%@ attribute name="var" required="true" type="java.lang.String" rtexprvalue="false" %>
    <%@ variable alias="varAlias" name-from-attribute="var" scope="AT_BEGIN" variable-class="java.lang.String"%>
    
    
    <spring:url value="${urlValue}" var="varAlias"/>
    
    0 讨论(0)
  • 2021-02-20 01:44

    A little late, but better late than never. Maybe someone else will find this helpful

    <%@ attribute name="var" required="true" type="java.lang.String" rtexprvalue="false"%>
    <%@ attribute name="date" required="true" type="java.sql.Timestamp" description="The date to format"%>
    <%@ variable alias="formattedDate" name-from-attribute="var" scope="AT_BEGIN" variable-class="java.lang.String"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
    <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
    
    <c:set var="formattedDate">
        <fmt:formatDate value="${ date }" pattern="hh:mma " var="time" />
        ${fn:toLowerCase(time)}<fmt:formatDate value="${ date }" pattern="MMMMM d, y" />
    </c:set>
    

    Set your var attribute (it must be required and not allow an rtexprvalue), then set your variable alias, which is what you refer to the variable as in your custom tag.

    Call your custom tag supplying the var variable

    <custom:dateFormat date="${ content.date }" var="formattedDate" />
    <c:out value="${formattedDate}" />
    
    0 讨论(0)
  • 2021-02-20 01:54

    Ran into this same issue and found a way to this without a "hard-coded" variable name that has to match between .jsp and .tag.

    <%@ taglib prefix="c"   uri="http://java.sun.com/jsp/jstl/core"%><%@ 
    taglib prefix="s"       uri="http://www.springframework.org/tags" %>
    
    <jsp:directive.attribute name="someInput" type="java.lang.Object" required="true" rtexprvalue="true" description="Input object" />
    <jsp:directive.attribute name="var" type="java.lang.String" required="false" rtexprvalue="false" description="Optional return var name" />
    
    <s:eval expression="@someService.someMethod(someInput)" var="someOutput" />
    
    <c:choose>
        <c:when test="${not empty var}">
            ${pageContext.request.setAttribute(var, someOutput)}
        </c:when>
        <c:otherwise>
            ${someOutput}
        </c:otherwise>
    </c:choose>
    

    This tag can be used in two ways:

    <%-- Option 1: renders the output of the tag directly to the page --%>
    <some:tagname someInput="${yourVar}" />
    
    <%-- Option 2: stores the output of the tag in variable called "result" and lets the caller render the output on his own --%>
    <some:tagname someInput="${yourVar}" var="result" />
    <c:out value="${result}"/>
    
    0 讨论(0)
提交回复
热议问题