Bah. Struts problem. Validation -- or lack thereof.

S

Steve Sobol

Latest version of Struts, Java 1.4.2_05 (I think), Jetty 5.0rc2

Struts works great. Except for validation. Validation doesn't. :)

If I override validate() and unconditionally add an error message, the form
behaves properly (control is returned to the form no matter what). But the
inherited validate() method is not detecting that my form fields are empty...
see below for more information:

The form:

======================================================================
<%@ page session="false" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<html:xhtml/>
<title>testing</title>
<html:form action="/login">
<html:errors/>
<table align="center" border="0" width="600" cellpadding="5">
<tr>
<td width=300 style="text-align: right">Username or Email address:</td>
<td width=300><html:text property="email"/></td>
</tr>
<tr>
<td width=300 style="text-align: right">Password:</td>
<td width=300><html:password property="password"/><BR/>
</td></tr></table>
<p style="text-align: center"><html:submit styleClass="button" value="Enter
Client Services Site"/></p>
</html:form>
</CENTER>
</BODY></HTML>
======================================================================

The form's bean:
======================================================================
package net.justthe.csweb;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.*;
import org.apache.struts.validator.ValidatorActionForm;

public class LoginForm extends ValidatorActionForm {
private String email = "";
private String password = "";

public String getemail() {
return(email);
}

public void setemail(String value) {
email = value;
}

public String getpassword() {
return(password);
}

public void setpassword(String value) {
password = value;
}

public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {

ActionErrors errors = super.validate(mapping, request);
if (errors == null) errors = new ActionErrors();

// sometimes during testing I will add a call to
// errors.add here, and if I do, I can submit the form and
// have control return back to the form. if I do not, and leave
// the form fields blank, control doesn't return back to the form
// as it should.

return errors;
}
}
======================================================================

My struts-config.xml:
======================================================================
<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">

<struts-config>
<form-beans>
<form-bean name="LoginForm" type="net.justthe.csweb.LoginForm"/>
</form-beans>

<action-mappings>
<action path="/login" type="net.justthe.csweb.LoginAction"
name="LoginForm" scope="request" input="/login.jsp" validate="true">
<forward name="success" path="/loginSuccess.jsp"/>
</action>
</action-mappings>

<message-resources parameter="/WEB-INF/webapp"/>

<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property
property="pathnames"
value="/WEB-INF/validator-rules.xml,/WEB-INF/csweb-validate.xml"/>
</plug-in>
</struts-config>
======================================================================

The validator-rules.xml is the file from the Struts distro; my csweb-validate
xml file is as follows:

======================================================================
<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE form-validation PUBLIC
"-//Apache Software Foundation//DTD Commons Validator Rules
Configuration 1.0//EN"
"http://jakarta.apache.org/commons/dtds/validator_1_0.dtd">

<form-validation>
<formset>
<form name="LoginForm">
<field property="email" depends="required">
<arg0 key="LoginForm.email"/>
</field>
<field property="password" depends="required">
<arg0 key="LoginForm.password"/>
</field>
</form>
</formset>
</form-validation>
======================================================================

FWIW, I'm using Sitemesh to handle site templating. It's installed as a filter.
This shouldn't make a difference, should it??
 
B

Bryce

Latest version of Struts, Java 1.4.2_05 (I think), Jetty 5.0rc2

Struts works great. Except for validation. Validation doesn't. :)

If I override validate() and unconditionally add an error message, the form
behaves properly (control is returned to the form no matter what). But the
inherited validate() method is not detecting that my form fields are empty...
see below for more information:

Works for me, but I don't override the validate() method. Also, I
extend ValidatorForm as opposed to ValidatorActionForm.
 
S

Sudsy

Bryce said:
Works for me, but I don't override the validate() method. Also, I
extend ValidatorForm as opposed to ValidatorActionForm.

Bryce,
That was the part which bothered me. I seem to recall that the
name resolution is different for Action forms. The OP might gain
some insights from my article at
<http://www.sudsy.net/technology/validation.html>
I just don't have the time or inclination to cut-and-paste the
code into my test environment. :-(
 
S

Steve Sobol

Bryce said:
Works for me, but I don't override the validate() method. Also, I
extend ValidatorForm as opposed to ValidatorActionForm.

Heh. I've tried both; neither works.
 
S

Steve Sobol

Steve said:
Heh. I've tried both; neither works.

I think I found the problem. I initialized email and password to null instead
of an empty string, and that fixed things. (Looks like it, anyhow; I'll report
back here if it didn't.)
 
S

Steve Sobol

Steve Sobol wrote:

I think I found the problem. I initialized email and password to null
instead of an empty string, and that fixed things. (Looks like it,
anyhow; I'll report back here if it didn't.)

It seems to work fine. Now, my last ordeal is getting <html:errors/> to
actually display errors. :)
 
S

Sudsy

Steve Sobol wrote:
I think I found the problem. I initialized email and password to null
instead of an empty string, and that fixed things. (Looks like it,
anyhow; I'll report back here if it didn't.)

Doh! An empty string satisfies 'required'; a null string doesn't.
You might also want to institute some sanity checking so that a
user can't specify a string of spaces, for example.
 
S

Steve Sobol

Sudsy said:
Steve Sobol wrote:


Doh! An empty string satisfies 'required'; a null string doesn't.
You might also want to institute some sanity checking so that a
user can't specify a string of spaces, for example.

In this case, not necessary for a login page, since a string of blanks won't
match any username or password on my system anyhow. :)

OK, one more quick question. I figured out why my errors aren't printing out -
they are listed in the message resource file as error.(whatever) or
PropertyName.(whatever) but Struts is prepending locale information so that it
looks for a resource named, for example, en_US.error.required instead of
error.required. If I don't want to use i18n right now, is there any way to
prevent the locale name from being prepended?

Thank you for your help. (Brice too)

**SJS
 
S

Sudsy

Steve Sobol wrote:
OK, one more quick question. I figured out why my errors aren't printing
out - they are listed in the message resource file as error.(whatever)
or PropertyName.(whatever) but Struts is prepending locale information
so that it looks for a resource named, for example, en_US.error.required
instead of error.required. If I don't want to use i18n right now, is
there any way to prevent the locale name from being prepended?

That's a fall-back position, where the locale on the server doesn't
match the browser. I've played around with I18N and never had any real
problems. I hate to do this to you again, but offer another link to
an article in my Struts series:
<http://www.sudsy.net/technology/struts-messageresources.html>
If the default locale on the server side matches what is presented by
the browser then nothing will be prepended.
Let us know how things work out!
 
S

Sudsy

Sudsy said:
Steve Sobol wrote:



That's a fall-back position, where the locale on the server doesn't
match the browser. I've played around with I18N and never had any real
problems. I hate to do this to you again, but offer another link to
an article in my Struts series:
<http://www.sudsy.net/technology/struts-messageresources.html>
If the default locale on the server side matches what is presented by
the browser then nothing will be prepended.
Let us know how things work out!

Postscript: I got to thinking about this after my reply as there was a
nagging memory about LC_ environment variables. If you're running some
variant of *NIX then there's probably a man page for setlocale(3) which
discusses some of these issues.
If everything is "in sync" then you shouldn't encounter problems... ;-)
 
S

Steve Sobol

Sudsy said:
Postscript: I got to thinking about this after my reply as there was a
nagging memory about LC_ environment variables. If you're running some
variant of *NIX then there's probably a man page for setlocale(3) which
discusses some of these issues.
If everything is "in sync" then you shouldn't encounter problems... ;-)

Redhat can bite me. They suck big hairy wet donkey nads.

I can't get it not to tag the encoding onto the end of the locale name.

And yes, I've tried the often-recommended 'edit /etc/sysconfig/i18n' and I even
just rebooted...

**making mental note to build my next server on SuSe**
 

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,012
Latest member
RoxanneDzm

Latest Threads

Top