开发者

Accessing Request object from custom JSP tags

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.servl开发者_JS百科et.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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜