我在".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}
Run Code Online (Sandbox Code Playgroud)
调用者可以像这样轻松调用它:
<prefix:mytag key="foo">Body content...</prefix:mytag>
Run Code Online (Sandbox Code Playgroud)
这将插入标签的输出.但我也会让调用者做这样的事情:
<prefix:mytag key="foo" var="mytagOutput">Body content...</prefix:mytag>
Run Code Online (Sandbox Code Playgroud)
在这种情况下,输出实际上不会被写入,而是分配给变量"mytagOutput",然后调用者可以使用该变量.
我知道调用者可以通过将自定义标记包装在a中来实现这一点c:set,但这并不像简单地声明"var"那样优雅.我也知道用这个@variable指令name-from-attribute可以用来实现这个目的.但是,我不知道调用者是否给出了属性"var".(如果给定,我想分配${output}给该变量,否则我想写出来${output}.)
有没有办法让我知道是否已传入"var"属性?
另一种选择是创建第二个自定义标记,可能称为"getMytag",它始终需要"var"属性并将"mytag"包装在一个中c:set.如果我在这里找不到解决办法,我会为此而努力.
(如果之前已经问过这个问题,请指出.我做了一个快速搜索,但没有找到类似的问题.)
小智 12
有点晚了,但迟到总比没有好.也许其他人会觉得这很有帮助
<%@ 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>
Run Code Online (Sandbox Code Playgroud)
设置var属性(必须是必需的,不允许使用rtexprvalue),然后设置变量别名,这是您在自定义标记中引用的变量.
调用提供var变量的自定义标记
<custom:dateFormat date="${ content.date }" var="formattedDate" />
<c:out value="${formattedDate}" />
Run Code Online (Sandbox Code Playgroud)
Ser*_*eyB 10
进入同样的问题,找到了一种方法,没有必须在.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>
Run Code Online (Sandbox Code Playgroud)
此标记可以以两种方式使用:
<%-- 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}"/>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11135 次 |
| 最近记录: |