wood burning stoves
The moose likes Struts and the fly likes DynaValidatorForm not redisplaying scoped variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "DynaValidatorForm not redisplaying scoped variables" Watch "DynaValidatorForm not redisplaying scoped variables" New topic

DynaValidatorForm not redisplaying scoped variables

Scott Updike
Ranch Hand

Joined: Feb 16, 2006
Posts: 92
Using standard JSTL, I have a simple form in my app that pre-populates a couple of fields with corresponding properties of a scoped transfer object (at the request level). For example, a typical line would like this:

<tr><td>Master Class Name:</td><td><html:text readonly="true" property="mclassname" value="${mclassname}"/></td></tr>

You can see that the value of ${mclassname} pre-populates the Master Class Name field. Upon submission, if an error occurs (via the DynaValidatorForm) the ${mclassname} does not re-appear in the Master Class Name.

How do I get that value to re-appear in my form.

Thanks in advance.

Scott Updike
Ranch Hand

Joined: Feb 16, 2006
Posts: 92
Ok, I figured out how to display previous values in <html:text></html:text> tags, but what if the ${mclassname} (again scoped in the request) is in some text on the screen? When the error screen appears, this field is blank and hence the text is not very user friendly.

So I'd like to ask the same question more genericaly. How do I access a request scoped attribute in the resulting error screen once the DynaValidatorForm has failed validation?
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Originally posted by Scott Updike:
How do I access a request scoped attribute in the resulting error screen once the DynaValidatorForm has failed validation?

You don't. This has nothing to do with a Struts limitation. It has to do with the way Java EE Web Containers work.

Request scope means just that: It exists for the life of one request to the server and is gone after that. Once your Action class forwarded to the JSP and the container processed the JSP and sent the plain HTML back to the browser, the request that was used during the creation of the JSP became out of scope. Even before the user re-submitted the page, the request was gone with no chance of getting it back.

There are several ways to work around this:
  • Put your variables in session scope rather than request scope
  • Use ActionForm properties rather than request scoped variables. ActionForm variables are carried forward from page to page even if the ActionForm itself is in request scope. If you have an ActionForm property for which there is no input field on the page and you want to preserve its value, put it in an <html:hidden> tag.
  • Use client-side validation with JavaScript. That way if an error is reported, the page is not refreshed, so the fact that the variables are out of scope is not an issue.

  • Merrill
    Consultant, Sima Solutions
    Brent Sterling
    Ranch Hand

    Joined: Feb 08, 2006
    Posts: 948
    I am not completely sure what your question is or if Merrill has provided an answer...

    If you are saying that you have a piece of text that is shown on the page that is not in a editable field and you need to show this text when the page is refreshed after validation you have a couple options. If you put the value in an hidden field (html:hidden) the value will be submitted on the request with the rest of the form values. Another option is to populate the value on the form in the action that shows the page after the validation error. A hidden field works well for a few text fields. The second option is better if you need to reload a lot of fields or lists of data.

    - Brent
    I agree. Here's the link: http://aspose.com/file-tools
    subject: DynaValidatorForm not redisplaying scoped variables
    It's not a secret anymore!