html:select default value in Struts

Discussion in 'Java' started by LDM, Oct 10, 2003.

  1. LDM

    LDM Guest

    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???
     
    LDM, Oct 10, 2003
    #1
    1. Advertising

  2. LDM

    Wendy S Guest

    > 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
     
    Wendy S, Oct 10, 2003
    #2
    1. Advertising

  3. LDM

    LDM Guest

    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.

    "Wendy S" <> wrote in message news:<bm6nbu$q2c$>...
    > > 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?
     
    LDM, Oct 13, 2003
    #3
  4. LDM

    Wendy S Guest

    "LDM" <> wrote in message
    news:...
    > 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
     
    Wendy S, Oct 13, 2003
    #4
  5. LDM

    LDM Guest

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


    "Wendy S" <> wrote in message news:<bmee76$h88$>...
    > "LDM" <> wrote in message
    > news:...
    > > 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?
     
    LDM, Oct 14, 2003
    #5
  6. LDM

    Wendy S Guest

    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

    "LDM" <> wrote in message
    news:...
    > 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".
    >
     
    Wendy S, Oct 15, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. msa
    Replies:
    5
    Views:
    8,391
    Sudsy
    Dec 18, 2003
  2. Lucas White
    Replies:
    1
    Views:
    10,869
    Yan Lijun
    Apr 28, 2004
  3. =?iso-8859-1?q?Jos=E9_Antonio?=
    Replies:
    6
    Views:
    649
    =?iso-8859-1?q?Jos=E9_Antonio?=
    Jun 20, 2005
  4. palmiere
    Replies:
    1
    Views:
    471
    Erwin Moller
    Feb 9, 2004
  5. WebRaster
    Replies:
    1
    Views:
    293
    Francisco Spaeth
    Oct 26, 2005
Loading...

Share This Page