JSP标记文件,输出其主体或以变量形式返回

JSP标记文件,输出其主体或以变量形式返回,jsp,jsp-tags,Jsp,Jsp Tags,我在“.tag”文件中有一个自定义标记,用于计算和输出值。因为我不能在这里发布代码,让我们假设一个简单的例子 文件mytag.tag的内容: <@tag dynamic-attributes="dynamicParameters"> <%@attribute name="key" required="true"%> <%-- this works fine, in spite of dynamic-attributes --%> <jsp:doBody

我在“.tag”文件中有一个自定义标记,用于计算和输出值。因为我不能在这里发布代码,让我们假设一个简单的例子

文件mytag.tag的内容:

<@tag dynamic-attributes="dynamicParameters">
<%@attribute name="key" required="true"%> <%-- this works fine, in spite of dynamic-attributes --%>
<jsp:doBody var="bodyContent/">
<%-- ... here is some code to compute the value of variable "output" --%>
${output}

${output}
调用者可以这样轻松地调用它:

<prefix:mytag key="foo">Body content...</prefix:mytag>
<prefix:mytag key="foo" var="mytagOutput">Body content...</prefix:mytag>
正文内容。。。
这将插入标记的输出。但我也允许调用方执行以下操作:

<prefix:mytag key="foo">Body content...</prefix:mytag>
<prefix:mytag key="foo" var="mytagOutput">Body content...</prefix:mytag>
正文内容。。。
在这种情况下,输出实际上不会被写入,而是分配给变量“mytagOutput”,然后调用者可以使用该变量

我知道调用者可以通过在
c:set
中包装自定义标记来实现这一点,但这并没有简单地声明“var”那么优雅。我还知道,带有
name from属性的
@variable
指令可以用来实现这一点。但是,我不知道调用方是否给出了属性“var”。(如果给定,我想将
${output}
赋值给该变量,否则我只想写出
${output}

有没有办法知道是否传入了“var”属性

另一个选项是创建第二个自定义标记,可能称为“getMytag”,它总是需要“var”属性,只需将“mytag”包装在
c:set
中。如果我在这里找不到解决办法,我会去的


(如果以前有人问过这个问题,请指给我看。我快速搜索了一下,但没有找到类似的问题。)

晚了一点,但总比没有好。也许其他人会觉得这很有帮助

<%@ 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>

${fn:toLowerCase(time)}
设置var属性(它必须是必需的,并且不允许使用rtexprvalue),然后设置变量别名,即在自定义标记中引用变量的别名

调用提供var变量的自定义标记

<custom:dateFormat date="${ content.date }" var="formattedDate" />
<c:out value="${formattedDate}" />

遇到了同样的问题,并找到了一种方法来解决这个问题,而不需要在.jsp和.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>

${pageContext.request.setAttribute(var,someOutput)}
${someOutput}
此标签可通过两种方式使用:

<%-- 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}"/>

根据另一个答案,变量别名似乎是一个很好的解决办法。下面是一个简单的例子:

<%@ 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"/>


这很好,也很有效,但“var”是必需的属性。因此,此标记将无法打印其输出或将其设置为变量。它将仅将其设置为变量。在标记内部工作时,未定义pageContext隐式对象。除非Spring的taglib以某种方式定义了它?但我在没有Spring的情况下尝试了这个解决方案,但它不起作用。