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

时间:2022-11-26 10:43:19

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?

我正在使用JSP 2标记文件编写自定义JSP标记。在我的标签内部,我想知道哪个页面称为标签以构建URL。这是否可以通过属性传递?

4 个解决方案

#1


2  

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

事实证明,请求对象实际上是可用的,但仅限于标记的EL部分。这样可行:

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

But not this:

但不是这个:

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

Or this:

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

#2


1  

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

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

#3


1  

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

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

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

#4


0  

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).

请求对象在标记中可用。使用类或标记文件无关紧要。在标记文件中,它可以在Java scriptlet和EL中使用。但是,它可用作ServletRequest对象而不是HttpServletRequest对象(在EL中,对象的类无关紧要,但它在scriptlet中有效)。

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

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

<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.

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

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

#1


2  

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

事实证明,请求对象实际上是可用的,但仅限于标记的EL部分。这样可行:

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

But not this:

但不是这个:

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

Or this:

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

#2


1  

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

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

#3


1  

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

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

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

#4


0  

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).

请求对象在标记中可用。使用类或标记文件无关紧要。在标记文件中,它可以在Java scriptlet和EL中使用。但是,它可用作ServletRequest对象而不是HttpServletRequest对象(在EL中,对象的类无关紧要,但它在scriptlet中有效)。

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

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

<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.

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

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