java 从自定义 JSP 标签访问请求对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2953678/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 23:39:59  来源:igfitidea点击:

Accessing Request object from custom JSP tags

javajsprequestjsp-tags

提问by Lotus Notes

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:

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

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.

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

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

文件是相当稀疏,所以我想也许我可以使用的JspContext对象以某种方式获得请求的属性。但我不明白不同的范围。

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

always prints "null".

总是打印“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 SimpleTagSupportclass.

编辑:标签扩展了SimpleTagSupport类。

回答by Fil

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

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

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

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