• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1139
  • Last Modified:

struts validation framework example error

I am running following example on Struts Validator Framework example as follows and getting errors.
CustomerDetails.jsp looks like

<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<html:html>
<BODY>
      <html:errors />
      <P>Please fill in the following details</P>
      <%--       <html:form action="customer"> --%>
      <html:form action="dynaCustomer">
            <TABLE border="1">
                  <TBODY>
                        <TR>
                              <TD>FirstName</TD>
                              <TD><html:text property="firstName"></html:text></TD>
                        </TR>
                        <TR>
                              <TD>MiddleName</TD>
                              <TD><html:text property="middleName"></html:text></TD>
                        </TR>
                        <TR>
                              <TD>LastName</TD>
                              <TD><html:text property="lastName"></html:text></TD>
                        </TR>
                        <TR>
                              <TD>Age</TD>
                              <TD><html:text property="age"></html:text></TD>
                        </TR>
                        <TR>
                              <TD>SSN</TD>
                              <TD><html:text property="ssn"></html:text></TD>
                        </TR>
                        <TR>
                              <TD>City</TD>
                              <TD><html:text property="city"></html:text></TD>
                        </TR>
                        <TR>
                              <TD>State</TD>
                              <TD><html:text property="state"></html:text></TD>
                        </TR>
                        <TR>
                              <TD>Country</TD>
                              <TD><html:text property="country"></html:text></TD>
                        </TR>
                        <TR>

                              <TD colspan="2"><html:submit></html:submit></TD>
                        </TR>
                  </TBODY>
            </TABLE>
      </html:form>
</BODY>
</html:html>



Main.jsp looks like




<%@ page session="true"%>
<%
      String name = (String) session.getAttribute("Name");
%>
<h3>
      Hello Mr
      <%=name%>. How are you doing?
</h3>



validation.xml looks like



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

<!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>

    <global>

        <!-- An example global constant
        <constant>
            <constant-name>postalCode</constant-name>
            <constant-value>^\d{5}\d*$</constant-value>
        </constant>
        end example-->

    </global>

    <formset>

        <form name="logonForm">
            <field
                property="username"
                depends="required">
                    <arg key="logonForm.username"/>
            </field>
            <field
                property="password"
                depends="required,mask">
                    <arg key="logonForm.password"/>
                    <var>
                        <var-name>mask</var-name>
                        <var-value>^[0-9a-zA-Z]*$</var-value>
                    </var>
            </field>
        </form>

    </formset>

    <formset language="fr">

        <constant>
            <constant-name>postalCode</constant-name>
            <constant-value>^[0-9a-zA-Z]*$</constant-value>
        </constant>

        <form name="logonForm">
            <field
                property="username"
                depends="required">
                    <arg key="logonForm.username"/>
            </field>
            <field
                property="password"
                depends="required,mask">
                    <arg key="logonForm.password"/>
                    <var>
                        <var-name>mask</var-name>
                        <var-value>^[0-9a-zA-Z]*$</var-value>
                    </var>
            </field>
        </form>

   </formset>
   
   <formset>
            <form name="dynamicCustomerForm">
                  <field property="lastName" depends="required">
                        <arg0 key="customerForm.lastName" />
                  </field>
                  <field property="ssn" depends="required,integer">
                        <arg0 key="customerForm.ssn" />
                  </field>
                  <field property="age" depends="required,range">
                        <arg0 key="customerForm.age" />
                        <arg1 name="range" key="$[var:minLength]" resource="false" />
                        <arg2 name="range" key="$[var:maxLength]" resource="false" />
                        <var>
                              
                              <var-name>minLength</var-name>
                              <var-value>10</var-value>
                        </var>
                        <var>
                              <var-name>maxLength</var-name>
                              <var-value>20</var-value>
                        </var>
                  </field>
            </form>
      </formset>

</form-validation>





validation-rules.xml looks like




<!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>

   <global>

      <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.ActionMessages,
                       javax.servlet.http.HttpServletRequest"
                  msg="errors.required"/>

      <validator name="requiredif"
                 classname="org.apache.struts.validator.FieldChecks"
                 method="validateRequiredIf"
                 methodParams="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"
                 msg="errors.required"/>

      <validator name="validwhen"
          msg="errors.required"
                 classname="org.apache.struts.validator.validwhen.ValidWhen"
                 method="validateValidWhen"
                 methodParams="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"/>


      <validator name="minlength"
            classname="org.apache.struts.validator.FieldChecks"
               method="validateMinLength"
         methodParams="java.lang.Object,
                       org.apache.commons.validator.ValidatorAction,
                       org.apache.commons.validator.Field,
                       org.apache.struts.action.ActionMessages,
                       javax.servlet.http.HttpServletRequest"
              depends=""
                  msg="errors.minlength"
           jsFunction="org.apache.commons.validator.javascript.validateMinLength"/>


      <validator name="maxlength"
            classname="org.apache.struts.validator.FieldChecks"
               method="validateMaxLength"
         methodParams="java.lang.Object,
                       org.apache.commons.validator.ValidatorAction,
                       org.apache.commons.validator.Field,
                       org.apache.struts.action.ActionMessages,
                       javax.servlet.http.HttpServletRequest"
              depends=""
                  msg="errors.maxlength"
           jsFunction="org.apache.commons.validator.javascript.validateMaxLength"/>



      <validator name="mask"
            classname="org.apache.struts.validator.FieldChecks"
               method="validateMask"
         methodParams="java.lang.Object,
                       org.apache.commons.validator.ValidatorAction,
                       org.apache.commons.validator.Field,
                       org.apache.struts.action.ActionMessages,
                       javax.servlet.http.HttpServletRequest"
              depends=""
                  msg="errors.invalid"/>


      <validator name="byte"
            classname="org.apache.struts.validator.FieldChecks"
               method="validateByte"
         methodParams="java.lang.Object,
                       org.apache.commons.validator.ValidatorAction,
                       org.apache.commons.validator.Field,
                       org.apache.struts.action.ActionMessages,
                       javax.servlet.http.HttpServletRequest"
              depends=""
                  msg="errors.byte"
       jsFunctionName="ByteValidations"/>


      <validator name="short"
            classname="org.apache.struts.validator.FieldChecks"
               method="validateShort"
         methodParams="java.lang.Object,
                       org.apache.commons.validator.ValidatorAction,
                       org.apache.commons.validator.Field,
                       org.apache.struts.action.ActionMessages,
                       javax.servlet.http.HttpServletRequest"
              depends=""
                  msg="errors.short"
       jsFunctionName="ShortValidations"/>


      <validator name="integer"
            classname="org.apache.struts.validator.FieldChecks"
               method="validateInteger"
         methodParams="java.lang.Object,
                       org.apache.commons.validator.ValidatorAction,
                       org.apache.commons.validator.Field,
                       org.apache.struts.action.ActionMessages,
                       javax.servlet.http.HttpServletRequest"
              depends=""
                  msg="errors.integer"
       jsFunctionName="IntegerValidations"/>



      <validator name="long"
            classname="org.apache.struts.validator.FieldChecks"
               method="validateLong"
         methodParams="java.lang.Object,
                       org.apache.commons.validator.ValidatorAction,
                       org.apache.commons.validator.Field,
                       org.apache.struts.action.ActionMessages,
                       javax.servlet.http.HttpServletRequest"
              depends=""
                  msg="errors.long"/>


      <validator name="float"
            classname="org.apache.struts.validator.FieldChecks"
               method="validateFloat"
         methodParams="java.lang.Object,
                       org.apache.commons.validator.ValidatorAction,
                       org.apache.commons.validator.Field,
                       org.apache.struts.action.ActionMessages,
                       javax.servlet.http.HttpServletRequest"
              depends=""
                  msg="errors.float"
       jsFunctionName="FloatValidations"/>

      <validator name="double"
            classname="org.apache.struts.validator.FieldChecks"
               method="validateDouble"
         methodParams="java.lang.Object,
                       org.apache.commons.validator.ValidatorAction,
                       org.apache.commons.validator.Field,
                       org.apache.struts.action.ActionMessages,
                       javax.servlet.http.HttpServletRequest"
              depends=""
                  msg="errors.double"/>


      <validator name="date"
            classname="org.apache.struts.validator.FieldChecks"
               method="validateDate"
         methodParams="java.lang.Object,
                       org.apache.commons.validator.ValidatorAction,
                       org.apache.commons.validator.Field,
                       org.apache.struts.action.ActionMessages,
                       javax.servlet.http.HttpServletRequest"
              depends=""
                  msg="errors.date"
       jsFunctionName="DateValidations"/>


      <validator name="intRange"
            classname="org.apache.struts.validator.FieldChecks"
               method="validateIntRange"
         methodParams="java.lang.Object,
                       org.apache.commons.validator.ValidatorAction,
                       org.apache.commons.validator.Field,
                       org.apache.struts.action.ActionMessages,
                       javax.servlet.http.HttpServletRequest"
              depends="integer"
                  msg="errors.range"/>


      <validator name="floatRange"
            classname="org.apache.struts.validator.FieldChecks"
               method="validateFloatRange"
         methodParams="java.lang.Object,
                       org.apache.commons.validator.ValidatorAction,
                       org.apache.commons.validator.Field,
                       org.apache.struts.action.ActionMessages,
                       javax.servlet.http.HttpServletRequest"
              depends="float"
                  msg="errors.range"/>


      <validator name="creditCard"
            classname="org.apache.struts.validator.FieldChecks"
               method="validateCreditCard"
         methodParams="java.lang.Object,
                       org.apache.commons.validator.ValidatorAction,
                       org.apache.commons.validator.Field,
                       org.apache.struts.action.ActionMessages,
                       javax.servlet.http.HttpServletRequest"
              depends=""
                  msg="errors.creditcard"/>


      <validator name="email"
            classname="org.apache.struts.validator.FieldChecks"
               method="validateEmail"
         methodParams="java.lang.Object,
                       org.apache.commons.validator.ValidatorAction,
                       org.apache.commons.validator.Field,
                       org.apache.struts.action.ActionMessages,
                       javax.servlet.http.HttpServletRequest"
              depends=""
                  msg="errors.email"/>

      <validator name="url"
            classname="org.apache.struts.validator.FieldChecks"
               method="validateUrl"
         methodParams="java.lang.Object,
                       org.apache.commons.validator.ValidatorAction,
                       org.apache.commons.validator.Field,
                       org.apache.struts.action.ActionMessages,
                       javax.servlet.http.HttpServletRequest"
              depends=""
                  msg="errors.url"/>

     
     <validator name="includeJavaScriptUtilities"
            classname=""
               method=""
         methodParams=""
              depends=""
                  msg=""
           jsFunction="org.apache.commons.validator.javascript.validateUtilities"/>

   </global>

</form-validation>



DynamicCustomerAction  looks like this

package actions;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
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;
public class DynamicCustomerAction extends Action {
      public ActionForward execute(ActionMapping mapping, ActionForm form,
                  HttpServletRequest request, HttpServletResponse response)
      throws Exception {
            String nextPage = "success";
            try {
            
                  DynaValidatorForm bean = (DynaValidatorForm) form;
                  String firstName = (String) bean.get("firstName");
            
                  String middleName = (String) bean.get("middleName");
                  String lastName = (String) bean.get("lastName");
                  String age = (String) bean.get("age");
                  String ssn = (String) bean.get("ssn");
                  String city = (String) bean.get("city");
                  String state = (String) bean.get("state");
                  String country = (String) bean.get("country");
            
                  int iage = 0;
                  int issn = 0;
                  iage = Integer.parseInt(age);
                  issn = Integer.parseInt(ssn);
                  request.getSession(true).setAttribute("Name", firstName + " " +
                              lastName);
                  
                  /*
                   * int status = insertCustomer(firstName,middleName,lastName,
                   * iage,issn,city,state,country);
                   */
            } catch (Exception e) {
            }
            // Finish with
            return mapping.findForward(nextPage);
      }
      private int insertCustomer(String firstName, String middleName,
                  String lastName, int age, int ssn, String city, String state,
                  String country) {
            Connection conn = null;
            try {
                  Class.forName("com.mysql.jdbc.Driver");
                  conn =
                        DriverManager.getConnection("jdbc:mysql://localhost:3306/MyDB");
            } catch (Exception e) {
                  e.printStackTrace();
            }
            int result = -1;
            // Create the DDL
            String sql = "INSERT INTO CUSTOMERS VALUES(?,?,?,?,?,?,?,?);";
            PreparedStatement ps = null;
            try {
                  if (conn != null) {
                        // Create statement from connection
                        ps = conn.prepareStatement(sql);
                        // Populate the data
                        
                        ps.setString(1, firstName);
                        ps.setString(2, middleName);
                        ps.setString(3, lastName);
                        ps.setInt(4, age);
                        ps.setInt(5, ssn);
                        ps.setString(6, city);
                        ps.setString(7, state);
                        ps.setString(8, country);
                        // Execute the prepared statement
                        result = ps.executeUpdate();
                  }
            } catch (Exception e) {
                  result = -1;
            }
            return result;
      }
}


CustomerDetails.jsp looks like



<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<html:html>
<BODY>
      <html:errors />
      <P>Please fill in the following details</P>
      <%--       <html:form action="customer"> --%>
      <html:form action="dynaCustomer">
            <TABLE border="1">
                  <TBODY>
                        <TR>
                              <TD>FirstName</TD>
                              <TD><html:text property="firstName"></html:text></TD>
                        </TR>
                        <TR>
                              <TD>MiddleName</TD>
                              <TD><html:text property="middleName"></html:text></TD>
                        </TR>
                        <TR>
                              <TD>LastName</TD>
                              <TD><html:text property="lastName"></html:text></TD>
                        </TR>
                        <TR>
                              <TD>Age</TD>
                              <TD><html:text property="age"></html:text></TD>
                        </TR>
                        <TR>
                              <TD>SSN</TD>
                              <TD><html:text property="ssn"></html:text></TD>
                        </TR>
                        <TR>
                              <TD>City</TD>
                              <TD><html:text property="city"></html:text></TD>
                        </TR>
                        <TR>
                              <TD>State</TD>
                              <TD><html:text property="state"></html:text></TD>
                        </TR>
                        <TR>
                              <TD>Country</TD>
                              <TD><html:text property="country"></html:text></TD>
                        </TR>
                        <TR>

                              <TD colspan="2"><html:submit></html:submit></TD>
                        </TR>
                  </TBODY>
            </TABLE>
      </html:form>
</BODY>
</html:html>



Struts-config.xml looks like


<?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="formbeans.LoginForm">
            </form-bean>
            <form-bean name="customerForm" type="formbeans.CustomerForm">
            </form-bean>



            <form-bean name="dynamicCustomerForm"
                  type="org.apache.struts.validator.DynaValidatorForm">
                  <form-property name="firstName" type="java.lang.String" />
                  <form-property name="middleName" type="java.lang.String" />
                  <form-property name="lastName" type="java.lang.String" />
                  <form-property name="age" type="java.lang.String" />
                  <form-property name="ssn" type="java.lang.String" />
                  <form-property name="city" type="java.lang.String" />
                  <form-property name="state" type="java.lang.String" />
                  <form-property name="country" 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="/customer" name="customerForm" validate="true"
                  input="/jsps/struts/CustomerDetails.jsp" scope="request"
                  type="actions.CustomerAction">
                  <forward name="success" path="/jsps/struts/Confirmation.jsp" />
            </action>
            <action path="/dynaCustomer" name="dynamicCustomerForm"
            validate="true" type="actions.DynamicCustomerAction ">
                  <forward name="success" path="/jsps/struts/Main.jsp" />
            </action>
            <action path="/welcome" forward="/jsps/struts/Welcome.jsp" />
            <action path="/maintenance" type="actions.MaintenanceAction">
                  <forward name="sales" path="/jsps/struts/Sales.jsp" />
                  <forward name="inventory" path="/jsps/struts/Inventory.jsp" />
            </action>
            <action path="/login" name="loginForm" type="actions.LoginAction">
                  <forward name="success" path="/jsps/struts/Main.jsp" />
            </action>
            <action path="/customTags" type="actions.BeanAction">
                  <forward name="bean" path="/jsps/struts/BeanTagsDemo.jsp" />
            </action>
      </action-mappings>
      

      <!-- Message Resources -->
      <message-resources parameter="myprops.MyStruts" />
      
      
      
      <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>


When I run
http://localhost:8080/StrutsReqWithDataNoForm2/jsps/struts/CustomerDetails.jsp leading to

http://localhost:8080/StrutsReqWithDataNoForm2/dynaCustomer.do
giving error as follows

HTTP Status 500 - java.lang.ClassNotFoundException: actions.DynamicCustomerAction

--------------------------------------------------------------------------------

type Exception report

message java.lang.ClassNotFoundException: actions.DynamicCustomerAction

description The server encountered an internal error (java.lang.ClassNotFoundException: actions.DynamicCustomerAction ) that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: java.lang.ClassNotFoundException: actions.DynamicCustomerAction
      org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:286)
      org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
      org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:722)


root cause

java.lang.ClassNotFoundException: actions.DynamicCustomerAction
      org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1711)
      org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1556)
      org.apache.struts.chain.commands.util.ClassUtils.getApplicationClass(ClassUtils.java:54)
      org.apache.struts.chain.commands.util.ClassUtils.getApplicationInstance(ClassUtils.java:71)
      org.apache.struts.chain.commands.servlet.CreateAction.createAction(CreateAction.java:98)
      org.apache.struts.chain.commands.servlet.CreateAction.getAction(CreateAction.java:68)
      org.apache.struts.chain.commands.AbstractCreateAction.execute(AbstractCreateAction.java:91)
      org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
      org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
      org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:305)
      org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
      org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
      org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
      org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:722)


note The full stack trace of the root cause is available in the Apache Tomcat/7.0.29 logs.


--------------------------------------------------------------------------------





Any ideas, suggestions, sample code, links, source code highly appreciated. Thanks in advance
StrutsValidationFw.JPG
0
gudii9
Asked:
gudii9
  • 8
1 Solution
 
chaitu chaituCommented:
check in WEB-INF/classes folder whether below class exists in the desired path or not.

 WEB-INF/classes/actions/DynamicCustomerAction.class
0
 
gudii9Author Commented:
I cannot see under WEB-INF/CLASSES/ACTIONS in eclipse but under src --->actions it is there for sure. Please advise
0
 
gudii9Author Commented:
Am I am missing anything. Please advise.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
gudii9Author Commented:
i have all required entries...not sure why action file not found
struts-config has entry


<action path="/dynaCustomer" name="dynamicCustomerForm"

                  type="actions.DynamicCustomerActions ">
                  <forward name="success" path="/jsps/struts/Confirmation2.jsp" />
            </action>

and

      <form-bean name="dynamicCustomerForm"
                  type="org.apache.struts.validator.DynaValidatorForm">
                  <form-property name="firstName" type="java.lang.String" />
                  <form-property name="middleName" type="java.lang.String" />
                  <form-property name="lastName" type="java.lang.String" />
                  <form-property name="age" type="java.lang.String" />
                  <form-property name="ssn" type="java.lang.String" />
                  <form-property name="city" type="java.lang.String" />
                  <form-property name="state" type="java.lang.String" />
                  <form-property name="country" type="java.lang.String" />
            </form-bean>


CustomerDetails.jsp has entry

<html:form action="dynaCustomer">

0
 
gudii9Author Commented:
I see following 3 entries missing compared to previous action

validate="true"
                  input="/jsps/struts/CustomerDetails.jsp" scope="request"


Do I need to mention them as well in struts.config. Please advise
0
 
gudii9Author Commented:
Hi Chaituu,

Did this program worked for you on your local machine. Please advise on I was doing wrong.
0
 
gudii9Author Commented:
please advise
0
 
gudii9Author Commented:
i created table as well in mysql database

CREATE TABLE CUSTOMERS

 (firstname VARCHAR(20),
middlename VARCHAR(35),
lastname VARCHAR(35),
iage VARCHAR(20),
issn VARCHAR(35),
city VARCHAR(35),
state VARCHAR(35),
country VARCHAR(35)


);
0
 
gudii9Author Commented:
Please advise
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now