Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Tomcat 5/Struts 1.1: Simple login form shows errors when accessed 1st time

Reply
Thread Tools

Tomcat 5/Struts 1.1: Simple login form shows errors when accessed 1st time

 
 
Brian O. Bush
Guest
Posts: n/a
 
      01-10-2004
All.
I have a login jsp page, and when i first access the login jsp page
(below), the validation errors are displayed as if someone had already
submitted the form. I thought that the bean was only validated upon
submit? Is there something else going on with the loginForm bean that
i have defined in the struts-config.xml. Another page I have does the
same thing..., and everything works fine, except for the fact that the
user sees error messages prior to doing anything. BTW, I have already
shift-reloaded the form to make sure it wasn't a caching issue.

Any help would be great!

Thanks, Brian

----------------------------------------------------------------
login.jsp:

....
<html:form action="/login" focus="username">
....
<html:text property="username" size="24" maxlength="18"/>
<html:errors property="username"/></td>
....
<htmlassword property="password" size="24" maxlength="18"
redisplay="false"/>
<html:errors property="password"/></td>
....
<html:submit><bean:message key="button.submit"/></html:submit>
</html:form>



----------------------------------------------------------------
validation.xml:

<form-validation>
<formset>

<form name="loginForm">
<field property="username"
depends="required, minlength, maxlength,
email">
<arg0 key="forms.login.username"/>
<arg1 key="${var:minlength}" name="minlength"
resource="false"/>
<arg2 key="${var:maxlength}" name="maxlength"
resource="false"/>
<var>
<var-name>maxlength</var-name>
<var-value>16</var-value>
</var>
<var>
<var-name>minlength</var-name>
<var-value>3</var-value>
</var>
</field>

....
</form>
</formset>
</form-validation>

----------------------------------------------------------------
struts-config.xml:
<form-beans>
<form-bean name="loginForm"
type="org.apache.struts.validator.DynaValidatorFor m">
<form-property name="username" type="java.lang.String"/>
<form-property name="password" type="java.lang.String"/>
</form-bean>
</form-beans>
....
<action path="/login"
type="com.blah.actions.LoginAction"
input="main.loginForm"
scope="session"
name="loginForm">
<forward name="next" path="index" />
</action>
----------------------------------------------------------------
com.blah.actions.LoginAction:


execute(...){
....
if (valid) {
HttpSession session = request.getSession();
if (session != null) {
session.setAttribute(Constants.USER_KEY, user);
}
} else {
errors.add(ActionErrors.GLOBAL_ERROR,
new ActionError("action.error.login"));
return(mapping.getInputForward());
}
....
return(mapping.findForward(Constants.NEXT));
 
Reply With Quote
 
 
 
 
Rick Osborn
Guest
Posts: n/a
 
      01-10-2004
Brian,

Can we see the validation error?



(Brian O. Bush) wrote in message news:<. com>...
> All.
> I have a login jsp page, and when i first access the login jsp page
> (below), the validation errors are displayed as if someone had already
> submitted the form. I thought that the bean was only validated upon
> submit? Is there something else going on with the loginForm bean that
> i have defined in the struts-config.xml. Another page I have does the
> same thing..., and everything works fine, except for the fact that the
> user sees error messages prior to doing anything. BTW, I have already
> shift-reloaded the form to make sure it wasn't a caching issue.
>
> Any help would be great!
>
> Thanks, Brian
>
> ----------------------------------------------------------------
> login.jsp:
>
> ...
> <html:form action="/login" focus="username">
> ...
> <html:text property="username" size="24" maxlength="18"/>
> <html:errors property="username"/></td>
> ...
> <htmlassword property="password" size="24" maxlength="18"
> redisplay="false"/>
> <html:errors property="password"/></td>
> ...
> <html:submit><bean:message key="button.submit"/></html:submit>
> </html:form>
>
>
>
> ----------------------------------------------------------------
> validation.xml:
>
> <form-validation>
> <formset>
>
> <form name="loginForm">
> <field property="username"
> depends="required, minlength, maxlength,
> email">
> <arg0 key="forms.login.username"/>
> <arg1 key="${var:minlength}" name="minlength"
> resource="false"/>
> <arg2 key="${var:maxlength}" name="maxlength"
> resource="false"/>
> <var>
> <var-name>maxlength</var-name>
> <var-value>16</var-value>
> </var>
> <var>
> <var-name>minlength</var-name>
> <var-value>3</var-value>
> </var>
> </field>
>
> ...
> </form>
> </formset>
> </form-validation>
>
> ----------------------------------------------------------------
> struts-config.xml:
> <form-beans>
> <form-bean name="loginForm"
> type="org.apache.struts.validator.DynaValidatorFor m">
> <form-property name="username" type="java.lang.String"/>
> <form-property name="password" type="java.lang.String"/>
> </form-bean>
> </form-beans>
> ...
> <action path="/login"
> type="com.blah.actions.LoginAction"
> input="main.loginForm"
> scope="session"
> name="loginForm">
> <forward name="next" path="index" />
> </action>
> ----------------------------------------------------------------
> com.blah.actions.LoginAction:
>
>
> execute(...){
> ...
> if (valid) {
> HttpSession session = request.getSession();
> if (session != null) {
> session.setAttribute(Constants.USER_KEY, user);
> }
> } else {
> errors.add(ActionErrors.GLOBAL_ERROR,
> new ActionError("action.error.login"));
> return(mapping.getInputForward());
> }
> ...
> return(mapping.findForward(Constants.NEXT));

 
Reply With Quote
 
 
 
 
Brian O. Bush
Guest
Posts: n/a
 
      01-11-2004
In validation errors, I mean the resources printed out from code in
the login.jsp, e.g.,

<html:errors property="username"/>
and
<html:errors property="password"/>

Where the user would see the following:

Login
Username: --------
Username is required.
Password: --------
Password is required.


Thanks,
Brian
 
Reply With Quote
 
Sudsy
Guest
Posts: n/a
 
      01-11-2004
Brian O. Bush wrote:
> All.
> I have a login jsp page, and when i first access the login jsp page
> (below), the validation errors are displayed as if someone had already
> submitted the form. I thought that the bean was only validated upon
> submit? Is there something else going on with the loginForm bean that
> i have defined in the struts-config.xml. Another page I have does the
> same thing..., and everything works fine, except for the fact that the
> user sees error messages prior to doing anything. BTW, I have already
> shift-reloaded the form to make sure it wasn't a caching issue.
>
> Any help would be great!
>
> Thanks, Brian


Have users go directly to login.jsp rather than the login Action.
For more on processing flow, check out this article:
<http://www.sudsy.net/technology/struts-arch.html>
It shows that as soon as you enter the action, the corresponding
ActionForm is reset() then populated with the request parameters.
If validate="true" is specified then validate() is invoked on the
ActionForm.

 
Reply With Quote
 
Wendy S
Guest
Posts: n/a
 
      01-12-2004
"Sudsy" <> wrote in message
news:...
> Have users go directly to login.jsp rather than the login Action.


Sudsy, I'm shocked! IMO, users should *always* go through the Action. In
fact, my JSP's live under WEB-INF-- you can't get to them directly. All you
need is a bit of logic to determine whether this is the "first time" and if
so, skip validation.

In the ActionForm (or whatever descendant you're using, I use
DynaValidatorForm):

public ActionErrors validate( ActionMapping mapping,
HttpServletRequest request )
{
ActionErrors errors = new ActionErrors();

// decide whether this is the "first time"

if ( !firstTime ) {
log.debug( "validating..." );
errors = super.validate( mapping, request );
}

return errors;
}

The decision can be any number of things. Was the form POSTed to you, or
was it a GET? Does a hidden field on the form have a value?

--
Wendy in Chandler, AZ


 
Reply With Quote
 
Sudsy
Guest
Posts: n/a
 
      01-13-2004
Wendy S wrote:
> "Sudsy" <> wrote in message
> news:...
>
>>Have users go directly to login.jsp rather than the login Action.

>
>
> Sudsy, I'm shocked! IMO, users should *always* go through the Action. In
> fact, my JSP's live under WEB-INF-- you can't get to them directly. All you
> need is a bit of logic to determine whether this is the "first time" and if
> so, skip validation.


Perhaps you'd like to compose a reply describing those approaches
in detail? (The GET/POST differentiation isn't applicable in a
number of scenarios, BTW.) My posted solution works and is easily
implemented.

 
Reply With Quote
 
Brian O. Bush
Guest
Posts: n/a
 
      01-13-2004
Ok, thanks for the reply... however, now I am totally confused. None
of the books or examples I have on this mention this problem. Or is
this just expected behavior...?

Also (1) I am using tiles (hence I don't really have a login.jsp, just
a combination of login_content.jsp, header.jsp, etc)
(2) I don't have a LoginForm (I once did, but retired it when I moved
to DynaValidatorForms...) so I don't have a validate method to check
for the first time it was accessed.

The workarounds mentioned are nice, but somehow it feels like this is
a problem (it happens on all my forms) and is not proper behavior. Has
the struts developers intentionally designed it so that the form upon
creation has the validate method called?

Brian


Sudsy <> wrote in message news:<>...
> Wendy S wrote:
> > "Sudsy" <> wrote in message
> > news:...
> >
> >>Have users go directly to login.jsp rather than the login Action.

> >
> >
> > Sudsy, I'm shocked! IMO, users should *always* go through the Action. In
> > fact, my JSP's live under WEB-INF-- you can't get to them directly. All you
> > need is a bit of logic to determine whether this is the "first time" and if
> > so, skip validation.

>
> Perhaps you'd like to compose a reply describing those approaches
> in detail? (The GET/POST differentiation isn't applicable in a
> number of scenarios, BTW.) My posted solution works and is easily
> implemented.

 
Reply With Quote
 
Tim
Guest
Posts: n/a
 
      01-13-2004
Since I'm just learning Struts myself, I've been avoiding getting in on
this conversation, however, since it's been going on for a while....
I have an example that I've found where the validation only occurs when
the submit button has been clicked.
Therefore, I would think this is the correct result.

Brian O. Bush wrote:
> All.
> I have a login jsp page, and when i first access the login jsp page
> (below), the validation errors are displayed as if someone had already
> submitted the form. I thought that the bean was only validated upon
> submit? Is there something else going on with the loginForm bean that
> i have defined in the struts-config.xml. Another page I have does the
> same thing..., and everything works fine, except for the fact that the
> user sees error messages prior to doing anything. BTW, I have already
> shift-reloaded the form to make sure it wasn't a caching issue.
>
> Any help would be great!
>
> Thanks, Brian
>
> ----------------------------------------------------------------
> login.jsp:
>
> ...
> <html:form action="/login" focus="username">
> ...
> <html:text property="username" size="24" maxlength="18"/>
> <html:errors property="username"/></td>
> ...
> <htmlassword property="password" size="24" maxlength="18"
> redisplay="false"/>
> <html:errors property="password"/></td>
> ...
> <html:submit><bean:message key="button.submit"/></html:submit>
> </html:form>
>
>
>
> ----------------------------------------------------------------
> validation.xml:
>
> <form-validation>
> <formset>
>
> <form name="loginForm">
> <field property="username"
> depends="required, minlength, maxlength,
> email">
> <arg0 key="forms.login.username"/>
> <arg1 key="${var:minlength}" name="minlength"
> resource="false"/>
> <arg2 key="${var:maxlength}" name="maxlength"
> resource="false"/>
> <var>
> <var-name>maxlength</var-name>
> <var-value>16</var-value>
> </var>
> <var>
> <var-name>minlength</var-name>
> <var-value>3</var-value>
> </var>
> </field>
>
> ...
> </form>
> </formset>
> </form-validation>
>
> ----------------------------------------------------------------
> struts-config.xml:
> <form-beans>
> <form-bean name="loginForm"
> type="org.apache.struts.validator.DynaValidatorFor m">
> <form-property name="username" type="java.lang.String"/>
> <form-property name="password" type="java.lang.String"/>
> </form-bean>
> </form-beans>
> ...
> <action path="/login"
> type="com.blah.actions.LoginAction"
> input="main.loginForm"
> scope="session"
> name="loginForm">
> <forward name="next" path="index" />
> </action>
> ----------------------------------------------------------------
> com.blah.actions.LoginAction:
>
>
> execute(...){
> ...
> if (valid) {
> HttpSession session = request.getSession();
> if (session != null) {
> session.setAttribute(Constants.USER_KEY, user);
> }
> } else {
> errors.add(ActionErrors.GLOBAL_ERROR,
> new ActionError("action.error.login"));
> return(mapping.getInputForward());
> }
> ...
> return(mapping.findForward(Constants.NEXT));


 
Reply With Quote
 
Wendy S
Guest
Posts: n/a
 
      01-13-2004
"Brian O. Bush" <> wrote in message
news: om...
> (2) I don't have a LoginForm (I once did, but retired it when I moved
> to DynaValidatorForms...) so I don't have a validate method to check
> for the first time it was accessed.


Using DynaValidatorForm only gets you out of writing a bunch of get/set
methods. You can still have a Java class that extends DynaValidatorForm and
override the validate method. It looks like this:

public final class ContactForm extends DynaValidatorForm
{ ...

and in struts-config, you use your form as the type rather than the generic
DynaValidatorForm:
<form-beans>
<form-bean name="contactForm"
type="edu.asu.vpia.struts.ContactForm">
...

Struts then knows to instantiate your class rather than DynaValidatorForm.

> The workarounds mentioned are nice, but somehow it feels like this is
> a problem (it happens on all my forms) and is not proper behavior. Has
> the struts developers intentionally designed it so that the form upon
> creation has the validate method called?


HTTP request -> form creation -> form population from request params -> form
validation -> display
The framework doesn't particularly care that the form is "new," it just
goes through the steps. Another option is to turn off validation in
struts-config.xml, and call it manually when you determine it's appropriate.

Hard-coded, my check of whether to validate looks like this:

if ( "Finish".equals( request.getParameter( "userAction" ) ) ) {
log.debug( "User has Finished, validating..." );
errors = super.validate( mapping, request );
}

'userAction' is the param that controls my LookupDispatchAction, and
"Finish" is the value of one of the many buttons on the form. One of these
days I'll get the former value from the ActionMapping and the latter from
the Resource bundle. This app suffers from being my very first Struts app,
and I find the oddest things when I go back and look at the code!

--
Wendy in Chandler, AZ


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calendar gadget shows Monday as the 1st day Carlos Windows 64bit 17 06-17-2009 04:44 PM
peterborough uk shortage of 1st 1st line jobs gerry MCSE 91 03-12-2008 06:03 PM
Session var in page_load shows old value, buttonclick shows new . Whats wrong gce ASP .Net 0 05-07-2005 06:50 AM
querystring doesn't work 1st time on page, only 2nd time Jason Shohet ASP .Net 1 06-17-2004 06:54 PM
In web.config, how to specify a page that can be accessed without login? TaeHo Yoo ASP .Net 2 07-29-2003 02:17 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57