You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by James Howe <jw...@allencreek.com> on 2001/03/07 14:54:29 UTC

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.


RE: Populating a Form (a proposal)

Posted by Johan Compagner <jc...@j-com.nl>.
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:jwh@allencreek.com]
> Sent: Wednesday, March 07, 2001 2:54 PM
> To: struts-user@jakarta.apache.org
> 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.
>
>


RE: Populating a Form

Posted by Johan Compagner <jc...@j-com.nl>.
Mostly at my place this is nothing more then setting one id!!
I would love to have the abillity that i didn't need any action for this.

johan


> -----Original Message-----
> From: Michael McCallister [mailto:r2126c@email.sps.mot.com]
> Sent: Wednesday, March 07, 2001 4:16 PM
> To: struts-user@jakarta.apache.org
> Subject: Re: Populating a Form
> 
> 
> I am strongly in favor of using two actions, one to pre-populate 
> the form, 
> the other to receive the user's input.  My rationale is that in 
> substantial 
> forms, there is enough logic involved in each of these activities that 
> lumping them together makes for an overly large and confusing Action.
> 
> 
> Mike
> 
> At 07:55 AM 3/7/2001, you wrote:
> >I guess my question is, how do most people handle the pre-population of 
> >data entry forms.
> 
> 

Re: Populating a Form

Posted by Michael McCallister <r2...@email.sps.mot.com>.
I am strongly in favor of using two actions, one to pre-populate the form, 
the other to receive the user's input.  My rationale is that in substantial 
forms, there is enough logic involved in each of these activities that 
lumping them together makes for an overly large and confusing Action.


Mike

At 07:55 AM 3/7/2001, you wrote:
>I guess my question is, how do most people handle the pre-population of 
>data entry forms.