无法从自定义 JSP 标记文件中的方法内访问隐式对象

发布于 2024-09-03 12:28:56 字数 361 浏览 3 评论 0原文

我正在尝试创建自定义 jsp 标记。一切工作正常,除了我的请求似乎超出了我的自定义函数的范围。

以下是 .tag 文件中的相关部分:

<%!
private  String process(String age, BigDecimal amount)
        {
//Attempting to access request here results in an compile time error trying to:
String url=request.getURL;
        }
%>

我对 JSP 非常陌生,所以我确信我遗漏了一些明显的东西..但我似乎不知道是什么。任何帮助表示赞赏。

I'm attempting to create a custom jsp tag. Everything is working fine, except for the fact that I the request seems to be out-of-scope for my custom function.

Here is the relevant bit from the .tag file:

<%!
private  String process(String age, BigDecimal amount)
        {
//Attempting to access request here results in an compile time error trying to:
String url=request.getURL;
        }
%>

I'm very new to JSP so I'm sure I'm missing something obvious..but I can't seem to figure out what. Any help is appreciated.

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

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

发布评论

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

评论(1

指尖上得阳光 2024-09-10 12:28:57

我怀疑这是因为自定义函数本身不是在 JSP 服务调用的主执行中定义的,而是在生成的 JSP 类中定义为单独的方法。因此,request 变量对其隐式不可见。

为了澄清这一点,如果您查看了 JSP 编译器生成的 java 源代码(特定于应用程序服务器),您将看到它是如何挂在一起的。

我认为您必须将请求对象声明为函数的参数,并在调用它时将其传入。

<%!
private String process(String age, BigDecimal amount, ServletRequest request) {
   String url=request.getURL;
   ....
}
%>

I suspect that's because the custom function itself is not defined within the main execution of the JSP's service call, but is defined as a separate method within the generated JSP class. As such, the request variable is not visible tot it implicitly.

To clarify, if you had a look at the java source that the JSP compiler generates (which is appserver specific), you'll see how it hangs together.

I think you'll have to declare the request object as a parameter to your function, and pass it in when you invoke it.

<%!
private String process(String age, BigDecimal amount, ServletRequest request) {
   String url=request.getURL;
   ....
}
%>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文