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