Struts question <html:text>

S

Scott Phelps

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

Wendy Smoak

Scott Phelps said:
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
 
S

Scott Phelps

Wendy Smoak said:
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

Actually the first time the page is loaded it is not submited. Just the jsp
page is called. right?
 
W

Wendy Smoak

Scott Phelps said:
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.
 
S

Scott Phelps

Wendy Smoak said:
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.


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

Wendy Smoak

Scott Phelps said:
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.
 

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

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top