You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Kim Brianne Go <ki...@gmail.com> on 2006/07/19 06:53:13 UTC

Problem with Validator it does not reload request attributes

Good Day,

I was just wondering if anyone has a clue on how to retain request values (
request.setAttribute()) forwarded to a jsp form page and once a form has
been submitted or validated then returns with error messages the values from
request.setAttribute() is no longer accessible.

Here's an example...

A form has an OptionsCollections populated using the request.setAttribute()...
from an action class.  I applied the Validator (Server-side) to perform
input validations, everything works well aside from the problem specific to
the OptionCollections... since it requires the values sent to the jsp page
prior to submission and validation.

A temporary solution was to put the OptionsCollections inside the
bean:present tag to avoid disrupting the entire page's execution.  But this
is not acceptable in practice since the input field that requires
OptionsCollections is necessary.

Does anyone have an idea how to go about this aside from doing a
bean:present or putting the values in session?

Thanks,

Brian

Re: Problem with Validator it does not reload request attributes

Posted by Kim Brianne Go <ki...@gmail.com>.
Thanks for the suggestions.

I tried this and it works if the content of the list boxes are static.  But
in my case, the values that is being used by this widget is pulled out from
the database then forwarded to the jsp page using request.setAttribute.

Unfortunately, i dont think it will be possible in struts to access a
datasource using the FormBean.  Well this is my current problem, and would
really like to know if somebody would say otherwise and or have other
suggestions.  Or the least we could do is put those values in session, but I
agree that it would be a waste of resource.

Here's a sample...

This basically loads data to request scope to be used by a JSP page...
....
List zones = buildSelectionForm("zones", request); // this method does the
DB access...
request.setAttribute("zones", zones);
....

Then using the html:optionsCollection... we can use the data to prefill the
list box, for example this zones are information pulled from the database
and sent to request scope in an ArrayList for display.
....
<logic:present name="zones">
     <html:select property="zoneId">
          <html:optionsCollection name="zones" value="zoneId"
label="zoneCode"/>
      </html:select>
</logic:present>
....

This works well, unfortunately if validation occurs what happens is the data
on the request scope will be nullified and will not be available again for
the html:optionsCollection.  That's the reason I have to put that widget
inside a logc:present tag.

Brian

On 7/19/06, Adam Gordon <ad...@readytalk.com> wrote:
>
> Kim-
>
> I had the same problem with some static form elements (e.g. contents of
> list boxes that were not part of any model data I cared to keep, yet it
> needed to be in the JSP page.)
>
> One solution I found was to set the properties on the session, but I
> decided that that was unnecessarily cluttering up the session and since
> only one form needed it was overkill.  Note the method signature of the
> validate(...) method in your ValidatorFormBean (or DynaForm...):
>
>   public ActionErrors validate(ActionMapping mapping, HttpServletRequest
> request) {
>
>     // will not return null
>     ActionErrors errors = super.validate(mapping, request);
>
>     ...
> }
>
> It takes a HttpServletRequest object.  Thus, you can set any attributes
> or parameters you need to on the request after a failed (or even
> successful) validation.  What I do is if the validation fails (the
> ActionErrors instance will not be empty - use this as your flag) and I
> set the parameters to populate the static form content widgets in my JSP
> so when the JSP page is reloaded with the error messages displayed, it
> displays correctly.
>
> Doing this avoids two things:  I'm not storing non-bean data in my bean
> and the data is segregated from the rest of the web container because
> it's only available to the request.  Now, if your situation differs, you
> can set it on the session instead and all your JSP pages will have
> access to the content.
>
> Hope this helps.
>
> Cheers,
>
> -Adam
>
> Kim Brianne Go wrote:
> > Good Day,
> >
> > I was just wondering if anyone has a clue on how to retain request
> > values (
> > request.setAttribute()) forwarded to a jsp form page and once a form has
> > been submitted or validated then returns with error messages the values
> > from
> > request.setAttribute() is no longer accessible.
> >
> > Here's an example...
> >
> > A form has an OptionsCollections populated using the
> > request.setAttribute()...
> > from an action class.  I applied the Validator (Server-side) to perform
> > input validations, everything works well aside from the problem specific
> > to
> > the OptionCollections... since it requires the values sent to the jsp
> > page
> > prior to submission and validation.
> >
> > A temporary solution was to put the OptionsCollections inside the
> > bean:present tag to avoid disrupting the entire page's execution.  But
> > this
> > is not acceptable in practice since the input field that requires
> > OptionsCollections is necessary.
> >
> > Does anyone have an idea how to go about this aside from doing a
> > bean:present or putting the values in session?
> >
> > Thanks,
> >
> > Brian
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

Re: Problem with Validator it does not reload request attributes

Posted by Adam Gordon <ad...@readytalk.com>.
Kim-

I had the same problem with some static form elements (e.g. contents of 
list boxes that were not part of any model data I cared to keep, yet it 
needed to be in the JSP page.)

One solution I found was to set the properties on the session, but I 
decided that that was unnecessarily cluttering up the session and since 
only one form needed it was overkill.  Note the method signature of the 
validate(...) method in your ValidatorFormBean (or DynaForm...):

  public ActionErrors validate(ActionMapping mapping, HttpServletRequest 
request) {

    // will not return null
    ActionErrors errors = super.validate(mapping, request);

    ...
}

It takes a HttpServletRequest object.  Thus, you can set any attributes 
or parameters you need to on the request after a failed (or even 
successful) validation.  What I do is if the validation fails (the 
ActionErrors instance will not be empty - use this as your flag) and I 
set the parameters to populate the static form content widgets in my JSP 
so when the JSP page is reloaded with the error messages displayed, it 
displays correctly.

Doing this avoids two things:  I'm not storing non-bean data in my bean 
and the data is segregated from the rest of the web container because 
it's only available to the request.  Now, if your situation differs, you 
can set it on the session instead and all your JSP pages will have 
access to the content.

Hope this helps.

Cheers,

-Adam

Kim Brianne Go wrote:
> Good Day,
>
> I was just wondering if anyone has a clue on how to retain request
> values (
> request.setAttribute()) forwarded to a jsp form page and once a form has
> been submitted or validated then returns with error messages the values
> from
> request.setAttribute() is no longer accessible.
>
> Here's an example...
>
> A form has an OptionsCollections populated using the
> request.setAttribute()...
> from an action class.  I applied the Validator (Server-side) to perform
> input validations, everything works well aside from the problem specific
> to
> the OptionCollections... since it requires the values sent to the jsp
> page
> prior to submission and validation.
>
> A temporary solution was to put the OptionsCollections inside the
> bean:present tag to avoid disrupting the entire page's execution.  But
> this
> is not acceptable in practice since the input field that requires
> OptionsCollections is necessary.
>
> Does anyone have an idea how to go about this aside from doing a
> bean:present or putting the values in session?
>
> Thanks,
>
> Brian
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org