是否可以在 jsp 2.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
事实证明,请求对象实际上可用,但仅在标记的 EL 部分中可用。 所以这会起作用:
但不是这个:
或者这个:
Turns out that the request object actually is available, but only in the EL portion of a tag. So this would work:
But not this:
Or this:
我认为在标签代码中,您可以检查请求对象及其 url,并从中确定页面。
I think that within the tag code, you can examine the request object and its url, and determine the page from that.
可以通过
pageContext
成员变量从标记文件内访问请求。It is possible to access the request from within the tag file, via the
pageContext
member variable.请求对象在标记中可用。 使用类或标记文件并不重要。 在标记文件中,它可以在 Java scriptlet 和 EL 中使用。
但是,它可以作为 ServletRequest 对象而不是 HttpServletRequest 对象使用(在 EL 中,对象的类并不重要,但在 scriptlet 中则重要)。
此外,在您的 scriptlet 中,您需要访问完整的方法,而不仅仅是属性名称。 所以你的代码应该是:
但即使这样也行不通,因为 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:
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()