You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Ned Seagoon <th...@hotmail.com> on 2001/02/21 09:40:26 UTC

Re: help! confused... now contains a proposal

Hi Hal, thanks for your mail

Yep that would be one solution if the population if the bean required on the
form page was 'static', but in the case I was referring too it needed the
item id to look up. I would imagine that it might be done by sending
actionforms with the id in backwards and forwards but this would become a
maintenance nightmare.

I reckon the way to approach this is to modify ActionForm handling. I would
add a field to this which holds the HttpServletRequest which gets set when
the form is created/retrieved in ActionServlet.processActionForm. The
ActionForm should also have a method called something like populateForm()
which the user developer could override to retrieve any values required from
sessions/databases etc. This would be called automatically when
formValidation *fails* and is probably better left to the developer to call
from Action.performAction if validation succeeds. My example would then be
along the lines of:

ActionForm {
    ... other stuff...

    HttpServletRequest request;
    void setRequest(HttpServletRequest r) {.....}
    void populateForm() {}
}

MyOrderActionForm extends ActionForm {
    String itemId;
    Object item;

    // get/set item id and get/set item omitted

    void populateForm() {
        ItemMap map= (ItemMap) request.getSession.getAttribute("item map");
        item = map.get(itemId);
    }
}

and for the buyit.do action (remember, called from /buyit.do?itemid=52)

perform(.....) {

    MyOrderActionForm form = new MyOrderActionForm();
    form.setItemId( request.getParameter("itemid") );
    form.populateForm();

    request.setAttribute("orderform", form);

  return mapping........
}

And on the view, the object bean could be got from the form for displaying.

Any thoughts?

It would be better to place this into the ActionForm class and do it
properly, but I'll wait until the feature freeze is over before trying to
submit it. In the mean time i'm going to have to derive my own classes and
override functions.

Regards
Ned

----- Original Message -----
From: "Deadman, Hal" <ha...@tallan.com>
To: <st...@jakarta.apache.org>
Sent: Tuesday, February 20, 2001 11:28 PM
Subject: RE: help! confused over forms and errors and oooh, other stuff :- )


I don't know if I understand your question but would making the input
attribute of the action another action give you the opportunity to retrieve
data that the jsp form needs before the jsp is displayed? I have a form that
needs a drop down populated from the database before the form is displayed.
I can never go to the jsp form directly, I always reference the action that
gets the data needed by the jsp. The action returns a forward which forwards
to the jsp with redirect="false".

Hal



Re: help! confused... now contains a proposal

Posted by Ned Seagoon <th...@hotmail.com>.
> Ned Seagoon wrote:
> > I reckon the way to approach this is to modify ActionForm handling. I
would
> > add a field to this which holds the HttpServletRequest which gets set
when
> > the form is created/retrieved in ActionServlet.processActionForm.


From: "Craig R. McClanahan" <Cr...@eng.sun.com>
> Are you planning on keeping this reference to the HttpServletRequest
object past
> the end of the current request?  If so, you are pretty much guaranteed to
have
> problems on any servlet container (such as Tomcat) that recycles request
and
> response objects.  The 2.3 servlet spec is also likely to clarify that
it's not
> legal to maintain references to the request and response objects after
your
> servlet's service() method returns.

D'oh! Oh yes, of course. I was thinking of keeping the request around so
that in a function like setItemId it could use the request to do a lookup
for any information and set it into the form bean. Fine, I'll just change
the populate() function to take the HttpServletRequest there.

This pattern works, I modified ActionServlet.ProcessValidate so that if the
validation fails, a class cast is attempted and populate() called on the
form. This then picks up the required object from the session (via the
request) and stores it in the form, ready to be displayed by the view.

So what do you think? Is this worthy of being incorporated? Maybe this
solution is not correct, but there is definately a need for what it is
fixing...

regards
Ned.

Re: help! confused... now contains a proposal

Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.
Ned Seagoon wrote:

>
> I reckon the way to approach this is to modify ActionForm handling. I would
> add a field to this which holds the HttpServletRequest which gets set when
> the form is created/retrieved in ActionServlet.processActionForm.

Are you planning on keeping this reference to the HttpServletRequest object past
the end of the current request?  If so, you are pretty much guaranteed to have
problems on any servlet container (such as Tomcat) that recycles request and
response objects.  The 2.3 servlet spec is also likely to clarify that it's not
legal to maintain references to the request and response objects after your
servlet's service() method returns.

The alternative would be to copy the request parameters into some private
structure if you need to repopulate later.

Craig