从自定义 JSP 标记访问 Request 对象
我正在尝试制作一组封装表单元素(标记和验证)的自定义标签。
有一种方法可以轻松检索“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的类正在扩展 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