Discussion:
html:select default value in Struts
(too old to reply)
LDM
2003-10-10 16:05:14 UTC
Permalink
Hi,

I have this Struts html form in a JSP page:

<html:select property="id" onchange="submit();">
<html:option value="">--- Choose one ---</html:option>
<html:options collection="list" property="id" labelProperty="name"/>
</html:select>

When I select an option, some data are displayed under the form,
depending on the option I've selected, and the option I've chosen
still stays selected: that's ok because I stay on this page.
Now I want that when I go to another page and come back to this page,
the first option ("Choose one") is selected. Each time I come back to
the form, the last option I've selected appears selected. How to do
this???
Wendy S
2003-10-10 16:39:27 UTC
Permalink
Post by LDM
When I select an option, some data are displayed under the form,
depending on the option I've selected, and the option I've chosen
still stays selected: that's ok because I stay on this page.
Now I want that when I go to another page and come back to this page,
the first option ("Choose one") is selected. Each time I come back to
the form, the last option I've selected appears selected. How to do
this???
In the Action, determine whether this is the "first" time it is being
called. (First is relative, of course... what you want to know is, was THIS
form just submitted, or am I coming to this Action from somewhere else.)
Then set the 'id' property of the form bean to the empty String (or not,
depending.) Struts checks the "id" property of the ActionForm to determine
which of the options should be pre-selected.

Are you always going through an Action first, and never going straight to
the JSP?
--
Wendy in Chandler, AZ
LDM
2003-10-13 08:23:50 UTC
Permalink
I use an Action to do this.
So, how to do what you said?
If we consider that I have a 'BuildListAction' action which builds my
selection list, and in my JSP a 'GetDataForm' form associated with a
'GetDataAction', I just can't see how I should refer to my
'GetDataForm' form in my 'BuildListAction' action...
Thanks for your help.
Post by Wendy S
Post by LDM
When I select an option, some data are displayed under the form,
depending on the option I've selected, and the option I've chosen
still stays selected: that's ok because I stay on this page.
Now I want that when I go to another page and come back to this page,
the first option ("Choose one") is selected. Each time I come back to
the form, the last option I've selected appears selected. How to do
this???
In the Action, determine whether this is the "first" time it is being
called. (First is relative, of course... what you want to know is, was THIS
form just submitted, or am I coming to this Action from somewhere else.)
Then set the 'id' property of the form bean to the empty String (or not,
depending.) Struts checks the "id" property of the ActionForm to determine
which of the options should be pre-selected.
Are you always going through an Action first, and never going straight to
the JSP?
Wendy S
2003-10-13 14:52:24 UTC
Permalink
Post by LDM
I use an Action to do this.
So, how to do what you said?
If we consider that I have a 'BuildListAction' action which builds my
selection list, and in my JSP a 'GetDataForm' form associated with a
'GetDataAction', I just can't see how I should refer to my
'GetDataForm' form in my 'BuildListAction' action...
Thanks for your help.
Obviously I haven't seen your code, but I don't think 'BuildListAction'
needs to be its own separate Action. It sounds like it belongs in
supporting code, perhaps part of the data access layer if this data is
coming from a database.

Then in the 'GetDataAction', you call this other code, perhaps
ListDAO.getList("something"); and place that in request or session scope.
To figure out if this is the "first" time, you can do something like look
for the presence of a hidden form field, or check whether the request has
been POSTed to you, rather than a GET.

Can you post the workflow as it stands now? What URL is visited first,
which JSP does it go to, where does the form on that JSP go?
--
Wendy in Chandler, AZ
LDM
2003-10-14 12:00:26 UTC
Permalink
The workflow looks like this:

BuildList.do
page1.jsp ---------------> page2.jsp

BuildListAction is called, and builds a selection list in page2.jsp
with data retrieved from a database, as you guessed.

GetData.do
page2.jsp ---------------> page2.jsp (updated)

page2.jsp contains GetDataForm with the <html:select> valued to
"Choose one". When this form is submitted, page2.jsp is updated by
GetDataAction with other data retrieved from the database.
And I'd like the <html:select> staying with the value I've just
selected.

When I go back to page1.jsp and then return to page2.jsp,
<html:select> must be valued to "Choose one".
Post by Wendy S
Post by LDM
I use an Action to do this.
So, how to do what you said?
If we consider that I have a 'BuildListAction' action which builds my
selection list, and in my JSP a 'GetDataForm' form associated with a
'GetDataAction', I just can't see how I should refer to my
'GetDataForm' form in my 'BuildListAction' action...
Thanks for your help.
Obviously I haven't seen your code, but I don't think 'BuildListAction'
needs to be its own separate Action. It sounds like it belongs in
supporting code, perhaps part of the data access layer if this data is
coming from a database.
Then in the 'GetDataAction', you call this other code, perhaps
ListDAO.getList("something"); and place that in request or session scope.
To figure out if this is the "first" time, you can do something like look
for the presence of a hidden form field, or check whether the request has
been POSTed to you, rather than a GET.
Can you post the workflow as it stands now? What URL is visited first,
which JSP does it go to, where does the form on that JSP go?
Wendy S
2003-10-15 18:01:49 UTC
Permalink
Are you ever visiting a URL with .jsp in it? In general, EVERY request has
to go through the Action, or things start falling apart. You have arrows
going _to_ page2.jsp, which worries me. I think it should be
page1.jsp ----> GetData.do

I still don't quite understand what's happening, but I think putting a
hidden form field on page2.jsp might help. Then in GetDataAction, look for
that hidden field in the form bean. If it's not there, set the property
that corresponds to the drop down to "" before forwarding to page2.jsp. If
the hidden property _is_ there, that tells you that the form on page2.jsp
was just submitted, and you need to preserve the value.

It also sounds like you have your form beans in session scope, and you might
find request scope more appropriate. Request scope forms have their own set
of problems, however!
--
Wendy in Chandler, AZ
Post by LDM
BuildList.do
page1.jsp ---------------> page2.jsp
BuildListAction is called, and builds a selection list in page2.jsp
with data retrieved from a database, as you guessed.
GetData.do
page2.jsp ---------------> page2.jsp (updated)
page2.jsp contains GetDataForm with the <html:select> valued to
"Choose one". When this form is submitted, page2.jsp is updated by
GetDataAction with other data retrieved from the database.
And I'd like the <html:select> staying with the value I've just
selected.
When I go back to page1.jsp and then return to page2.jsp,
<html:select> must be valued to "Choose one".
Continue reading on narkive:
Loading...