The last (using 2 actions) is how it is done in the struts example
(last time i checked) with registering or changing user data.

But i also would like to have a popuplate without going through a action.

Then i could request this:

userdata.jsp?userid=1

instead of this:

userdata.do?userid=1

The form has a setUserId(int i)

and populates itself from a database.

The problem with populating from a database is that if the form
is not created or accessed through a action the servlet variable is not
set!!
So i can't get the datasource!!

I have made my own Form tag that extends the Struts formtag.

public int doStartTag() throws JspException {
        int iReturn = super.doStartTag();
        Object bean = pageContext.getAttribute(Constants.BEAN_KEY);
        if (bean instanceof ServletContextActionForm)
        {
                ((ServletContextActionForm)
bean).setServletContext(pageContext.getServletContext());
        }
        /*
         maybe popuplate like this??
         RequestUtils.populate(((ServletContextActionForm) bean),
                                                mapping.getPrefix(),
                                                mapping.getSuffix(),
        */                                      pageContext.getRequest());
        return iReturn;
}

As you can see i have implemented my own doStartTag() and if it is a special
actionform
i will set the servlet context so that ( if it works ) i can access
everything i want.

also calling populate is not implemented yet but it should work.

I would like to see this in Struts it self. Because for a very simple thing
like
filling a form that mostly is decided by a parameter like userid=X through
another
action object is very boring.

Johan

> -----Original Message-----
> From: James Howe [mailto:[EMAIL PROTECTED]]
> Sent: Wednesday, March 07, 2001 2:54 PM
> To: [EMAIL PROTECTED]
> Subject: Populating a Form
>
>
> I have what may be a stupid question regarding the usage of forms in
> Struts.  I have a web application which has a page displaying a list of
> items.  When the user clicks on one of the items I want to bring
> up a form
> which allows the user to edit the items.  Nothing too hard.  My
> question is
> about what the best way is to go about populating the form within the
> Struts framework.  If I were just displaying information, I would simply
> create an Action for the page which would populate a bean which
> would then
> be referenced by the corresponding JSP page.  However, since this is a
> form, I want to make use of Struts form handling capabilities.
> If I use a
> form action, the action occurs on submission.  How do I control
> the initial
> values of the form?
>
> One way that I can think of is to use some sort of "action"
> parameter.  If
> the action parameter is missing, the form action populates the bean
> properties, otherwise it saves the bean properties back to the
> system.  The
> problem I have with this approach is that I have to have a hidden
> field on
> the page which is used to store the "action" value.  It's not a big deal,
> but it seems clumsy.  The other approach I can think of is to have a
> regular action defined which is used to populate the page and a
> form action
> which is used for form submission.  This requires two actions for
> the same
> JSP but eliminates some of the funny business with hidden fields.
>
> I guess my question is, how do most people handle the pre-population of
> data entry forms.
>
> Thanks.
>
>

Reply via email to