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

时间:2022-11-26 13:30:32

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

文档非常稀疏,所以我想也许我可以使用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 SimpleTagSupport class.

编辑:标签扩展SimpleTagSupport类。

1 个解决方案

#1


13  

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

#1


13  

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