如何访问自定义 taglib Java 类中的 JSP 隐式对象?

发布于 2024-10-01 14:29:51 字数 1461 浏览 3 评论 0原文

我在这里研究了有关标记库和隐式对象的各种问题。虽然许多答案提出了有趣的想法,但我无法找到直接回答这个问题的答案。

为了说明我的困境,假设我有一个实现 javax.servlet.jsp.tagext.Tag 接口的 Java 类...

import javax.servlet.ServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;

class MyTag implements Tag {
    private PageContext _pageContext;

    public void setPageContext(PageContext pageContext) {
        _pageContext = pageContext;
    }
    .
    .
    .

这使我可以访问 PageContext 对象,该对象提供了有关调用 JSP 的一些有用信息。问题是,PageContext 似乎没有提供对 JSP 的许多实际隐式对象的访问...

.
.
.
public int doEndTag() throws JspException {
    ServletRequest req = _pageContext.getRequest();
    .
    .
    .
    return EVAL_PAGE;
}

上面描述的 ServletRequest 对象与 HttpServletRequest 有很大不同,后者是由隐式对象“request”实现的在 JSP 本身中。这使得在我的类中调用 HttpServletRequest.getRequestURI() 这样方便的方法会出现问题。

因此,通过阅读这里其他几个问题的答案,我留下的印象是,没有开箱即用的方法来获取实际的隐式对象。我要么必须将它们作为 JSP 中自定义标记的属性显式传递...

<mc:mytag request="<%=request%>"/>

...或者可能将它们存储在我确实可以访问的其他隐式对象的哈希中

<% session.setAttribute("request", request); %>

... ,如果是真的,自然会让我想到这个问题......你在开玩笑吗?这里一定有一些关键的东西我错过了。对于我来说,在自定义 taglib 类中访问 JSP 的隐式对象似乎是相当常见的事情。每次我需要这些步骤时都必须采取这些步骤,这让我觉得非常笨拙——尤其是当我传递诸如 PageContext 对象之类的东西时,它可以想象为我提供它们,但似乎并没有。

我真的觉得我在这里错过了一些基本的东西,但我一生都无法弄清楚那可能是什么。有人可以解释一下吗?

I've examined various questions about taglibs and implicit objects here. While many of the answers present interesting ideas, I haven't been able to find one that answers this question directly.

So to illustrate my dilemma, say I have a Java class that implements the javax.servlet.jsp.tagext.Tag interface...

import javax.servlet.ServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;

class MyTag implements Tag {
    private PageContext _pageContext;

    public void setPageContext(PageContext pageContext) {
        _pageContext = pageContext;
    }
    .
    .
    .

This gives me access to a PageContext object, which provides some useful information about the calling JSP. Problem is, PageContext doesn't seem to provide access to many of the JSP's actual implicit objects...

.
.
.
public int doEndTag() throws JspException {
    ServletRequest req = _pageContext.getRequest();
    .
    .
    .
    return EVAL_PAGE;
}

The ServletRequest object depicted above is quite different from HttpServletRequest, which is implemented by the implicit object "request" in the JSP itself. This makes calling such handy methods as HttpServletRequest.getRequestURI() problematic inside my class.

So from reading the answers to several other questions here, I'm left with the impression that there is no out-of-the-box way of getting at the actual implicit objects. I either have to pass them explicitly as attributes of the custom tag in my JSP...

<mc:mytag request="<%=request%>"/>

...or perhaps store them in the hash of some other implicit object I do have access to...

<% session.setAttribute("request", request); %>

Which, if true, naturally leads me to the question... are you friggin' kidding me?? There's got to be something key that I'm missing here. Access to the JSP's implicit objects seems like such a reasonably common thing for me to want in my custom taglib classes. It strikes me as immensely kludgy to have to take such steps every time I require them -- especially when I'm being passed something like the PageContext object that could conceivably provide them for me, but doesn't seem to.

I really feel like I'm missing something basic here, but I can't for the life of me figure out what that might be. Can someone shed some light on this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

狼性发作 2024-10-08 14:29:51

PageContext 确实允许您访问当前的HttpServletRequest,您只需要转换它:

HttpServletRequest req = (HttpServletRequest )_pageContext.getRequest();

这不是很好,但可以追溯到当时人们认为 Servlet API 中可能有一天会出现非 HTTP 的内容。

至于术语“隐式对象”的使用,这只在 JSP 页面本身的上下文中才有意义。除此之外,这个词没有任何意义。表示当前 HttpServletRequest 的“隐式对象”与您从 PageContext 获取的对象相同。

The PageContext does give you access to the current HttpServletRequest, you just need to cast it:

HttpServletRequest req = (HttpServletRequest )_pageContext.getRequest();

This isn't great, but dates back to a time when people thought there might one day be non-HTTP stuff in the Servlet API.

As for the use of the term "implicit objects", this only makes sense within the context of the JSP page itself. The term has no meaning beyond that. The "implicit object" representing the current HttpServletRequest is the same object you get from the PageContext.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文