是否可以在 jsp 2.0 自定义标记内获取调用页面名称?

发布于 2024-07-08 15:03:09 字数 89 浏览 6 评论 0原文

我正在使用 JSP 2 标记文件编写自定义 JSP 标记。 在我的标签内,我想知道哪个页面调用了该标签来构建 URL。 这是否可以在不通过属性传递的情况下实现?

I'm writing a custom JSP tag using the JSP 2 tag files. Inside my tag I would like to know which page called the tag in order to construct URLs. Is this possible with out passing it through an attribute?

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

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

发布评论

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

评论(4

拥醉 2024-07-15 15:03:09

事实证明,请求对象实际上可用,但仅在标记的 EL 部分中可用。 所以这会起作用:

<form action="${pageContext.request.requestURI}">

但不是这个:

<form action="<%=request.requestURI%>">

或者这个:

<form action="<%=pageContext.request.requestURI%>">

Turns out that the request object actually is available, but only in the EL portion of a tag. So this would work:

<form action="${pageContext.request.requestURI}">

But not this:

<form action="<%=request.requestURI%>">

Or this:

<form action="<%=pageContext.request.requestURI%>">
混浊又暗下来 2024-07-15 15:03:09

我认为在标签代码中,您可以检查请求对象及其 url,并从中确定页面。

I think that within the tag code, you can examine the request object and its url, and determine the page from that.

独守阴晴ぅ圆缺 2024-07-15 15:03:09

可以通过 pageContext 成员变量从标记文件内访问请求。

public class YourTag extends TagSupport {
    public int doStartTag() throws JspException {
        HttpServletRequest req = (HttpServletRequest) pageContext.getRequest();
        String pathInfo = req.getPathInfo();

It is possible to access the request from within the tag file, via the pageContext member variable.

public class YourTag extends TagSupport {
    public int doStartTag() throws JspException {
        HttpServletRequest req = (HttpServletRequest) pageContext.getRequest();
        String pathInfo = req.getPathInfo();
§对你不离不弃 2024-07-15 15:03:09

请求对象在标记中可用。 使用类或标记文件并不重要。 在标记文件中,它可以在 Java scriptlet 和 EL 中使用。
但是,它可以作为 ServletRequest 对象而不是 HttpServletRequest 对象使用(在 EL 中,对象的类并不重要,但在 scriptlet 中则重要)。

此外,在您的 scriptlet 中,您需要访问完整的方法,而不仅仅是属性名称。 所以你的代码应该是:

<form action="<%= pageContext.getRequest().getRequestURI() %>">

但即使这样也行不通,因为 getRequestURI() 是 HttpServletRequest [1] 的方法,而不是 ServletRequest 的方法。 因此,要么使用 EL,要么在标记文件中使用更长的 scriptlet 并转换请求对象。

[1] http:// /java.sun.com/javaee/5/docs/api/javax/servlet/http/HttpServletRequest.html#getRequestURI()

The request object is available in the tag. It doesn't matter if you use a class or a tag file. In tag files, it is available in Java scriptlets as well as in EL.
However, it is available as a ServletRequest object and not an HttpServletRequest object (in EL the class of the object doesn't matter, but it does in scriptlets).

In addition, in your scriptlets you need to access the full method, not just a property name. So your code is supposed to be:

<form action="<%= pageContext.getRequest().getRequestURI() %>">

but even that won't work because getRequestURI() is a method of HttpServletRequest [1], not of ServletRequest. So either use EL, or use longer scriptlets in your tag file and cast the request object.

[1] http://java.sun.com/javaee/5/docs/api/javax/servlet/http/HttpServletRequest.html#getRequestURI()

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