aspose file tools*
The moose likes Struts and the fly likes Validation framework is not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Validation framework is not working " Watch "Validation framework is not working " New topic
Author

Validation framework is not working

Javed Alam A
Greenhorn

Joined: Aug 28, 2010
Posts: 5


I am working on validation framework in Struts1.1 but I am not able to get result with validation
. Please Help me.......
**************************************************************************************************
index.jsp
**************************************************************************************************
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<%@taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
<%@taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Index</title>
</head>
<body>
<logic:messagesPresent>
<font color="red">
<html:messages id="errors">
<li><%=errors %></li>
</html:messages>
</font>
</logic:messagesPresent>
<html:form action="/login" method="post">
<table>
<tr>
<td><bean:message key="LoginForm.userName" /></td>
<td><html:text property="userName" ></html:text></td>
</tr>
<tr>
<td><bean:message key="LoginForm.password" /></td>
<td><html:password property="password"></html:password></td>
</tr>
<tr>
<td><html:submit property="login"></html:submit> </td>
</tr>


</table>

</html:form>
</body>
</html>

*******************************************************************************************
struts-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="com.user.forms.LoginForm"></form-bean>

</form-beans>

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

<!-- Global Forwards -->
<global-forwards>
<!-- <forward name="home" path="/home.jsp"></forward> -->
</global-forwards>

<!-- Action Mappings -->
<action-mappings>
<action path="/login" type="com.user.actions.LoginAction" name="LoginForm" validate="true" input="/index.jsp" scope="request">
<forward name="success" path="/home.jsp"></forward>
<forward name="failure" path="/index.jsp"></forward>
</action>


</action-mappings>

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

<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>
*************************************************************************************************
validation.xml
*************************************************************************************************
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE form-validation PUBLIC
"-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1.3//EN"
"http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd">

<form-validation>



<formset>

<form name="LoginForm">
<field
property="userName"
depends="required">
<arg key="LoginForm.userName"/>
</field>
<field
property="password"
depends="required,minlength">
<arg0 key="LoginForm.password"/>
<arg1 key="${var:minlength}" name="minlength" resource="false"/>
<var>
<var-name>minlength</var-name>
<var-value>6</var-value>
</var>
</field>
</form>

</form-validation>
**************************************************************************************************
ApplicationResources.properties
**************************************************************************************************
# Optional header and footer for <errors/> tag.
#errors.header=<ul>
#errors.footer=</ul>

LoginForm.userName = User Name
LoginForm.password = Password
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.




Rajagopal Mani
Ranch Hand

Joined: Mar 24, 2011
Posts: 84

The above code is working fine in my working environment. please show your form-bean.
Javed Alam A
Greenhorn

Joined: Aug 28, 2010
Posts: 5

This is my Form Bean
********************************************************************************************************
LoginForm.java
********************************************************************************************************

package com.user.forms;

import org.apache.struts.validator.ValidatorActionForm;

public class LoginForm extends ValidatorActionForm{
/**
*
*/
private static final long serialVersionUID = 1L;
private String userName = null;
private String password = null;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}

}

Rajagopal Mani wrote:The above code is working fine in my working environment. please show your form-bean.
Rajagopal Mani
Ranch Hand

Joined: Mar 24, 2011
Posts: 84

The problem in the usage of extending super class form-bean. If you extend ValidatorForm instead of ValidatorActionForm, the server side validation will perform properly. Moreover, please refer the usage of ValidatorForm and ValidatorActionForm below.

When you use validatorForm or DynaValidatorForm based Form Beans, Validator uses the logical name for the Form Bean from the struts-config.xml file to map the Form Bean to validation definitions in the validation.xml file. This mechanism is great in most cases, but in some scenarios, Form Beans are shared among multiple actions. One action may use all of the Form Bean's fields, and another action may use only a subset of the fields. Because validation definitions are tied to the Form Bean, the action that uses only a subset of the fields has no way of bypassing validations for the unused fields. When the Form Bean is validated, it generates error messages for the unused fields, because Validator has no way of knowing not to validate the unused fields; it simply sees them as missing or invalid.

To solve this problem, Validator provides two additional ActionForm subclasses that allow you to tie validations to actions instead of to Form Beans. That way you can specify which validations to apply to the Form Bean based on which action is using the Form Bean. In this case, we can use ValidatorActionForm(concrete form-bean) or DynaValidatorActionForm(dynamic form-bean).
Javed Alam A
Greenhorn

Joined: Aug 28, 2010
Posts: 5

Thank you very much indeed for giving me explanation. But code is not working in my working environment. I have applied both form bean situation.
Rajagopal Mani
Ranch Hand

Joined: Mar 24, 2011
Posts: 84

Please share your project structure
Javed Alam A
Greenhorn

Joined: Aug 28, 2010
Posts: 5

Actually it is required in my Project that's i am using at home using simple demo project. When I extens ValidatorActionForm in my Form Bean then i get no error and don't get Error message on my index.jsp page. But when I extens ValidatorForm in my Form Bean then i get following error message on console:
Jul 4, 2011 8:08:41 PM org.apache.commons.validator.Validator validateFieldForRule
SEVERE: reflection: org.apache.struts.validator.FieldChecks.validateRequired(java.lang.Object, org.apache.commons.validator.ValidatorAction, org.apache.commons.validator.Field, org.apache.struts.action.ActionMessages, org.apache.commons.validator.Validator, javax.servlet.http.HttpServletRequest)
java.lang.NoSuchMethodException: org.apache.struts.validator.FieldChecks.validateRequired(java.lang.Object, org.apache.commons.validator.ValidatorAction, org.apache.commons.validator.Field, org.apache.struts.action.ActionMessages, org.apache.commons.validator.Validator, javax.servlet.http.HttpServletRequest)
at java.lang.Class.getMethod(Unknown Source)
at org.apache.commons.validator.Validator.validateFieldForRule(Validator.java:401)
at org.apache.commons.validator.Validator.validateField(Validator.java:544)
at org.apache.commons.validator.Validator.validate(Validator.java:582)
at org.apache.struts.validator.ValidatorForm.validate(ValidatorForm.java:152)
at org.apache.struts.action.RequestProcessor.processValidate(RequestProcessor.java:942)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:255)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)
Jul 4, 2011 8:08:41 PM org.apache.commons.validator.Validator validateFieldForRule
SEVERE: reflection: org.apache.struts.validator.FieldChecks.validateRequired(java.lang.Object, org.apache.commons.validator.ValidatorAction, org.apache.commons.validator.Field, org.apache.struts.action.ActionMessages, org.apache.commons.validator.Validator, javax.servlet.http.HttpServletRequest)
java.lang.NoSuchMethodException: org.apache.struts.validator.FieldChecks.validateRequired(java.lang.Object, org.apache.commons.validator.ValidatorAction, org.apache.commons.validator.Field, org.apache.struts.action.ActionMessages, org.apache.commons.validator.Validator, javax.servlet.http.HttpServletRequest)
at java.lang.Class.getMethod(Unknown Source)
at org.apache.commons.validator.Validator.validateFieldForRule(Validator.java:401)
at org.apache.commons.validator.Validator.validateField(Validator.java:544)
at org.apache.commons.validator.Validator.validate(Validator.java:582)
at org.apache.struts.validator.ValidatorForm.validate(ValidatorForm.java:152)
at org.apache.struts.action.RequestProcessor.processValidate(RequestProcessor.java:942)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:255)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)



One thing I want to say that Eclipse Galileo , jdk1.6 and Tomcat v6.0
Rajagopal Mani
Ranch Hand

Joined: Mar 24, 2011
Posts: 84

You might have mixed up the versions of validator-rules.xml and struts jar. Either go back to struts jar 1.1 or upgrade the validator-rules.xml. Try to use the all related files(ie. xml files & jars) with same versions. Please refer below link for download.

Struts
 
 
subject: Validation framework is not working