从自定义 JSP 标记访问 Request 对象

发布于 2024-09-04 07:55:33 字数 1146 浏览 3 评论 0原文

我正在尝试制作一组​​封装表单元素(标记和验证)的自定义标签。

有一种方法可以轻松检索“Out”对象:

JspWriter out = getJspContext().getOut();

但是我不知道如何获取请求对象。我希望能够直接从 Tag 类中访问提交的表单值,以便我可以验证每个字段。

文档非常稀疏,所以我想也许我可以使用 JspContext< /a> 对象以某种方式获取请求属性。但我不明白不同的范围。

System.out.println(getJspContext().findAttribute("field1"));

总是打印“null”。

Enumeration e = getJspContext().getAttributeNamesInScope(1);

循环并打印出枚举只会给我一个不存在的类列表:

javax.servlet.jsp.jspOut
javax.servlet.jsp.jspPage
javax.servlet.jsp.jspSession
javax.servlet.jsp.jspApplication
javax.servlet.jsp.jspPageContext
javax.servlet.jsp.jspConfig
javax.servlet.jsp.jspResponse
javax.servlet.jsp.jspRequest

那么这可能吗?

如果没有,有人可以向我指出一个处理表单显示和验证的标签库吗?我在互联网上搜索了几个小时,似乎每个都已停产,我无法下载它们。或者建议一个更好的处理表单的替代方案。

编辑:标签扩展了 SimpleTagSupport 类。

I'm trying to make a set of custom tags that encapsulate form elements (markup and validation).

There's a method given to retrieve the "Out" object easily:

JspWriter out = getJspContext().getOut();

However I can't figure out how to get the request object. I want to be able to directly access the submitted form values from within the Tag class so that I can validate each field.

The documentation is quite sparse, so I thought maybe I could use the JspContext object to somehow get the request attributes. But I don't understand the different scopes.

System.out.println(getJspContext().findAttribute("field1"));

always prints "null".

Enumeration e = getJspContext().getAttributeNamesInScope(1);

Looping through and printing out the enumeration just gives me a list of classes that don't exist:

javax.servlet.jsp.jspOut
javax.servlet.jsp.jspPage
javax.servlet.jsp.jspSession
javax.servlet.jsp.jspApplication
javax.servlet.jsp.jspPageContext
javax.servlet.jsp.jspConfig
javax.servlet.jsp.jspResponse
javax.servlet.jsp.jspRequest

So is this even possible?

If not, could anyone point me to a tag library that deals with form display and validation? I searched the internet for a couple hours and it seemed every single one was discontinued and I couldn't download them. Either that or suggest a better alternative for handling forms.

Edit: The tags extend the SimpleTagSupport class.

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

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

发布评论

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

评论(1

离去的眼神 2024-09-11 07:55:33

如果您的类正在扩展 TagSupport,您可以访问受保护的 pageContext 变量。从中您可以检索请求对象。

http://java.sun .com/webservices/docs/1.5/api/javax/servlet/jsp/tagext/TagSupport.html#pageContext

If your class is extending TagSupport, you can access the protected pageContext variable. From that you're able to retrieve the request object.

http://java.sun.com/webservices/docs/1.5/api/javax/servlet/jsp/tagext/TagSupport.html#pageContext

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