html:select default value in Struts

L

LDM

Hi,

I have this Struts html form in a JSP page:

<html:select property="id" onchange="submit();">
<html:eek:ption value="">--- Choose one ---</html:eek:ption>
<html:eek:ptions 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???
 
W

Wendy S

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?
 
L

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.
 
W

Wendy S

LDM said:
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?
 
L

LDM

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".
 
W

Wendy S

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top