File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes Struts: DynaValidatorForm not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Frameworks » Struts
Reply locked New topic
Author

Struts: DynaValidatorForm not working

Nina Anderson
Ranch Hand

Joined: Jul 18, 2006
Posts: 148
Hey guys

My Login page does not seem to be going through the form validation. When I enter an invalid login (eg. leave password blank) the page no longer displays error messages.

Previously, when I was using the ActionForm class, I explicitly added the error keys to the ActionError() object to be displayed in the jsp <html:errors>. But, now that I using DynaValidatorForm, the form is not being validated.

I'll appreciate your insight.

Here are my files:

==================================================================
LOGIN.JSP


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<html:html>
<head>

<%@ page
language="java"
contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"
%>
<META name="GENERATOR" content="IBM Software Development Platform">
<meta http-equiv="Content-Type" content="text/html;">
<title>Untitled</title>


<style>
TD{
font-family:verdana,arial;
color:black;
font-size:8pt;

}
.news{
font-family:arial;
}
.bot{
color:black;
text-decoration:none;
font-size:7pt;
font-family:Verdana;
font-weight:bold;

}
.copy{
font-size:7pt;
color:black;
}
</style>
</head>

<body>

<html:errors/>

<html:form action="/submitLogin" >

<table border="0" cellpadding="0" cellspacing="0" width="159">


<tr>
<td valign="top" class="news"><b>UserName: </b></td>
<td>   <html:text property="userName" size="11" />


</td>
</tr>

<tr>


<td valign="top" class="news"><b>PassWord: </b></td>
<td>   <html:text property="passWord" size="11" />
</td>

</tr>

<tr>

<td valign="top"class="news"><INPUT type="submit" value="Logon"></td>

</tr>

</table>
</html:form>

</BODY>
</html:html>

====================================
STUTS_CONFIG.xml

<?xml version="1.0" encoding="UTF-8"?>
<!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>

<!-- Data Sources -->
<data-sources>
</data-sources>

<!-- Form Beans -->
<form-beans>
<form-bean name="LoginForm"

type="org.apache.struts.validator.DynaValidatorForm">

<!-- Dynamic properties of the Login Form -->
<form-property name="userName" type="java.lang.String" />
<form-property name="passWord" type="java.lang.String" />

</form-bean>
</form-beans>

<!-- Global Exceptions -->
<global-exceptions>
</global-exceptions>

<!-- Global Forwards -->
<global-forwards>
</global-forwards>

<!-- Action Mappings -->
<action-mappings>
<action path="/myPath" forward="/main.jsp" />
<action path="/submitLogin" scope="request"

type="com.poshWebApp.actions.LoginAction" name="LoginForm" validate="false"

input="/web/pages/login/login.jsp">
<forward name="success" path="/main.jsp" />
</action>
</action-mappings>

<!-- Message Resources -->
<message-resources parameter="pbwebapp.resources.ApplicationResources"/>

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

/WEB-INF/validation.xml" />
</plug-in>

</struts-config>

===============================================================
LOGINACTION.java


package com.poshWebApp.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.DynaValidatorForm;

/**
* Form bean for a Struts application.
* Users may access 2 fields on this form:
* <ul>
* <li>passWord - [your comment here]
* <li>userName - [your comment here]
* </ul>
* @version 1.0
* @author
*/
public class LoginAction extends Action {

public ActionForward execute(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {

DynaValidatorForm dynaform = (DynaValidatorForm) form;

request.setAttribute(mapping.getAttribute(), dynaform);

System.out.println("Forwarding - SUCCESS page");

return mapping.findForward("success");
}


}

=======================================================
Validation.xml

<formset>
<form name="LoginForm">
<field property="userName" depends="required">
<arg0 key="prompt.username" />
</field>
<field property="passWord" depends="required, minlength,

maxlength">
<arg0 key="prompt.password" />
<arg1 key="${var:minlength}" name="minlength"

resource="false" />
<arg2 key="${var:maxlength}" name="maxlength"

resource="false" />


<var>
<var-name>minlength</var-name>
<var-value>6</var-value>
</var>
<var>
<var-name>maxlength</var-name>
<var-value>12</var-value>
</var>
</field>
</form>
</formset>

==================================================================

ApplicationResource.properties

# Optional header and footer for <errors/> tag.
errors.header=<ul><hr><h3>Errors</h3><ul>
errors.footer=</ul><hr>
errors.username.required=<li>valid username required
errors.password.required=<li>valid password required

# Struts Validator Error Messages
errors.required={0} is required.
errors.minlength={0} can not be less than {1} characters.
errors.maxlength={0} can not be greater than {1} characters.
errors.invalid={0} is invalid.

======================================================

Validation-rules.xml

<validator name="required"
classname="org.apache.struts.validator.FieldChecks"
method="validateRequired"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionErrors,
javax.servlet.http.HttpServletRequest"
msg="errors.required">

Thanks!!!
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Nina,

Please do not post the same question more than once. It confuses things and makes it harder to keep track of what has been answered and what hasn't. It's fine if you want to start a new thread, but don't ask the same question twice.

If anyone wishes to respond to this question, please do so in this thread.

Merrill


Merrill
Consultant, Sima Solutions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Struts: DynaValidatorForm not working