JSP标记文件,它可以输出其主体或以变量形式返回它 [英] JSP tag file that either outputs its body or returns it in a variable

查看:77
本文介绍了JSP标记文件,它可以输出其主体或以变量形式返回它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在".tag"文件中有一个自定义标签,该标签计算并输出一个值.因为我无法在此处发布代码,所以让我们假设一个简单的示例.

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.

mytag.tag文件的内容:

Content of file 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}

呼叫者可以像这样轻松地调用它:

The caller can easily call it like this:

<prefix:mytag key="foo">Body content...</prefix:mytag>

这将插入标签的输出.但是我还可以使调用者执行以下操作:

This will insert the output of the tag. But I would also enable the caller to do something like this:

<prefix:mytag key="foo" var="mytagOutput">Body content...</prefix:mytag>

在这种情况下,实际上不会写入输出,而是将输出分配给调用方可以使用的变量"mytagOutput".

In this case, the output would not actually be written, but assigned to the variable "mytagOutput", which the caller then can use.

我知道调用者可以通过将自定义标签包装在c:set中来实现此目的,但是这比简单地声明"var"要优雅.我还知道,可以将带有name-from-attribute@variable指令用于实现此目的.但是然后,我不知道属性"var"是否已由调用者提供. (如果有的话,我想将${output}分配给该变量,否则我只想写出${output}.)

I know that the caller can achieve this by wrapping the custom tag in a c:set, but this is less elegant than simply declaring a "var". I also know that the @variable directive with the name-from-attribute can be used to achieve this. But then, I do not know if the attribute "var" has been given by the caller or not. (If given, I want to assign ${output} to that variable, otherwise I want to just write out ${output}.)

有没有一种方法可以确定是否已传递"var"属性?

Is there a way how I can find out if the "var" attribute has been passed in or not?

另一种选择是创建第二个自定义标签,可能称为"getMytag",该标签始终期望使用"var"属性,并将"mytag"包装在c:set中.如果我在这里找不到解决方案,那我会去的.

Another option would be to create a second custom tag, maybe called "getMytag", which always expects the "var" attribute and just wraps the "mytag" in a c:set. If I don't find a solution here, I will go for that.

(如果以前曾问过这个问题,请指向我.我进行了快速搜索,但没有找到类似的问题.)

(If this question has been asked before, please point me to it. I did a quick search, but did not find a similar question.)

推荐答案

遇到同样的问题,并找到了解决此问题的方法,而无需在".jsp"和".tag"之间进行匹配的硬编码"变量名. >

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

这篇关于JSP标记文件,它可以输出其主体或以变量形式返回它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆