Struts question <html:text>

Discussion in 'Java' started by Scott Phelps, Jun 30, 2005.

  1. Scott Phelps

    Scott Phelps Guest

    http://struts.apache.org/faqs/actionForm.html

    How can I make <html:text property="username" size="16"/> display the entry
    a person put in on error, but another when the property is null or ""?

    I am tring to make the user login filled in by a cookie, but still keep the
    input when the login is incorrect.

    Thanks!
     
    Scott Phelps, Jun 30, 2005
    #1
    1. Advertising

  2. Scott Phelps

    Wendy Smoak Guest

    "Scott Phelps" <> wrote
    >
    > How can I make <html:text property="username" size="16"/> display the

    entry
    > a person put in on error, but another when the property is null or ""?
    >
    > I am tring to make the user login filled in by a cookie, but still keep

    the
    > input when the login is incorrect.


    When the request first comes in, it goes to an Action. In the Action,
    decide whether this is the "first time" you are displaying the form. If so,
    read the cookie, and populate the form property: form.setUsername(
    valueFromCookie );

    Then forward to the JSP, and the framework will render the text area with
    the value you specified. When the user submits the form, the framework will
    populate the form bean with that value, and so on.

    To decide whether this is the "first time" you can do a number of things.
    If the form itself will be POSTed, then you can check whether that's true.
    Or you can set a hidden form element that will only be present if the form
    was actually submitted.

    Also take a look at StrutsDialogs:
    http://struts.sourceforge.net/strutsdialogs/index.html

    --
    Wendy Smoak
     
    Wendy Smoak, Jun 30, 2005
    #2
    1. Advertising

  3. Scott Phelps

    Scott Phelps Guest

    "Wendy Smoak" <> wrote in message
    news:da1f6s$kcg$...
    > "Scott Phelps" <> wrote
    >>
    >> How can I make <html:text property="username" size="16"/> display the

    > entry
    >> a person put in on error, but another when the property is null or ""?
    >>
    >> I am tring to make the user login filled in by a cookie, but still keep

    > the
    >> input when the login is incorrect.

    >
    > When the request first comes in, it goes to an Action. In the Action,
    > decide whether this is the "first time" you are displaying the form. If
    > so,
    > read the cookie, and populate the form property: form.setUsername(
    > valueFromCookie );
    >
    > Then forward to the JSP, and the framework will render the text area with
    > the value you specified. When the user submits the form, the framework
    > will
    > populate the form bean with that value, and so on.
    >
    > To decide whether this is the "first time" you can do a number of things.
    > If the form itself will be POSTed, then you can check whether that's true.
    > Or you can set a hidden form element that will only be present if the form
    > was actually submitted.
    >
    > Also take a look at StrutsDialogs:
    > http://struts.sourceforge.net/strutsdialogs/index.html
    >
    > --
    > Wendy Smoak
    >
    >


    Actually the first time the page is loaded it is not submited. Just the jsp
    page is called. right?
     
    Scott Phelps, Jun 30, 2005
    #3
  4. Scott Phelps

    Wendy Smoak Guest

    "Scott Phelps" <> wrote
    >
    > Actually the first time the page is loaded it is not submited. Just the

    jsp
    > page is called. right?
    >


    Obviously, that's possible, but it is not The Struts Way in which every
    request goes through an Action. Many (most?) of us go to great lengths to
    prevent a user from ever navigating directly to a JSP.

    (Either with a security constraint in web.xml, or else just putting the JSP
    files under WEB-INF where the container is forbidden to serve them
    directly.)

    You are correct in that the first time the page loads, the form will not
    have been submitted. So you might be able to look for POST in
    request.getMethod() to decide whether to use the cookie or not.

    --
    Wendy Smoak
     
    Wendy Smoak, Jun 30, 2005
    #4
  5. Scott Phelps

    Scott Phelps Guest

    "Wendy Smoak" <> wrote in message
    news:da1p6t$o3q$...
    > "Scott Phelps" <> wrote
    >>
    >> Actually the first time the page is loaded it is not submited. Just the

    > jsp
    >> page is called. right?
    >>

    >
    > Obviously, that's possible, but it is not The Struts Way in which every
    > request goes through an Action. Many (most?) of us go to great lengths to
    > prevent a user from ever navigating directly to a JSP.
    >
    > (Either with a security constraint in web.xml, or else just putting the
    > JSP
    > files under WEB-INF where the container is forbidden to serve them
    > directly.)
    >
    > You are correct in that the first time the page loads, the form will not
    > have been submitted. So you might be able to look for POST in
    > request.getMethod() to decide whether to use the cookie or not.
    >
    > --
    > Wendy Smoak
    >
    >



    Your solution worked .... but I am currios about what you posted above. I am
    still fairly new and I have always though that struts worked in this
    fashion...

    jsp displays the page .... form is submited ... call to the action ....
    action does buisness ... action returns mapping ... jsp displays again.

    But you say a form is submited even before you display a jsp page? So your
    login page would be login.do instead of login.jsp ? Why?
     
    Scott Phelps, Jul 1, 2005
    #5
  6. Scott Phelps

    Wendy Smoak Guest

    "Scott Phelps" <> wrote
    > I have always though that struts worked in this
    > fashion...
    >
    > jsp displays the page .... form is submited ... call to the action ....
    > action does buisness ... action returns mapping ... jsp displays again.
    >
    > But you say a form is submited even before you display a jsp page? So your
    > login page would be login.do instead of login.jsp ? Why?


    I didn't say a form is submitted before you display a JSP... I said *every*
    *request* must go through an Action. A browser visiting
    http://www.example.com/myapp/some.jsp submits a request (GET rather than
    POST).

    If, instead, you 'hide' that JSP, and make the browser go to
    http://www.example.com/myapp/someAction.do then the request goes through an
    Action you have a chance to do some setup (if you need to... it could also
    just be a ForwardAction that does nothing but forward to the JSP.) You also
    have MUCH more control over the application flow.

    There are always exceptions, of course, but in general this has proved to be
    a best practice for Struts apps.

    --
    Wendy Smoak
     
    Wendy Smoak, Jul 1, 2005
    #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. Katie Wright
    Replies:
    8
    Views:
    4,256
    Andrea Desole
    Jan 7, 2005
  2. Jason Us
    Replies:
    0
    Views:
    993
    Jason Us
    Oct 4, 2003
  3. jc1771
    Replies:
    0
    Views:
    408
    jc1771
    Dec 28, 2003
  4. sin
    Replies:
    3
    Views:
    726
    Sudsy
    Feb 22, 2004
  5. Pratap Das
    Replies:
    2
    Views:
    2,035
    Pratap Das
    Apr 5, 2004
Loading...

Share This Page