1 Reply Latest reply: Nov 21, 2006 4:21 AM by 843844 RSS

    Not able to run validation using validation.xml & validator-rules.xml

    843844
      Hello Friends,
      I am not able to run validation using validation.xml & validator-rules.xml.
      Entire code in running prefectly but no error messages are prompted.

      Following is my code:



      File Name : struts-config.xml


      <struts-config>

      <!-- Form Beans Configuration -->
      <form-beans>
      <form-bean name="searchForm"
      type="com.solversa.SearchForm"/>
      </form-beans>

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

      <!-- Action Mappings Configuration -->
      <action-mappings>
      <action path="/search"
      type="com.solversa.SearchAction"
      name="searchForm"
      scope="request"
      validate="true"
      input="/search.jsp">
      </action>
      </action-mappings>

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

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



      <br> File Name : <b> validation.xml </b>


      <form-validation>
      <formset>
      <form name="searchForm">
      <field property="name" depends="minlength">
      <arg key="label.search.name" position = "0"/>
      <arg1 name="minlength" key="${var:minlength}" resource="false"/>
      <var>
      <var-name>minlength</var-name>
      <var-value>5</var-value>
      </var>
      </field>

      <field property="ssNum" depends="mask">
      <arg0 key="label.search.ssNum"/>
      <var>
      <var-name>mask</var-name>
      <var-value>^\d{3}-\d{2}-\d{4}$</var-value>
      </var>
      </field>

      </form>
      </formset>
      </form-validation>


      <br> File Name : <b> SearchForm.java </b>

      package com.jamesholmes.minihr;

      import java.util.List;
      import org.apache.struts.validator.ValidatorForm;

      public class SearchForm extends ValidatorForm
      {
      private String name = null;
      private String ssNum = null;
      private List results = null;

      public void setName(String name) {
      this.name = name;
      }

      public String getName() {
      return name;
      }

      public void setSsNum(String ssNum) {
      this.ssNum = ssNum;
      }

      public String getSsNum() {
      return ssNum;
      }

      public void setResults(List results) {
      this.results = results;
      }

      public List getResults() {
      return results;
      }
      }


      <br> File Name : <b> SearchAction.java </b>

      package com.jamesholmes.minihr;

      import java.util.ArrayList;

      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;

      public final class SearchAction extends Action
      {
      public ActionForward execute(ActionMapping mapping,
      ActionForm form,
      HttpServletRequest request,
      HttpServletResponse response)
      throws Exception
      {       
      EmployeeSearchService service = new EmployeeSearchService();
      ArrayList results;

      SearchForm searchForm = (SearchForm) form;

      // Perform employee search based on what criteria was entered.
      String name = searchForm.getName();
      if (name != null && name.trim().length() > 0) {
      results = service.searchByName(name);
      } else {
      results = service.searchBySsNum(searchForm.getSsNum().trim());
      }

      // Place search results in SearchForm for access by JSP.
      searchForm.setResults(results);

      // Forward control to this Action's input page.
      return mapping.getInputForward();
      }
      }

      <br> File Name : <b> EmployeeSearchService.java </b>

      package com.jamesholmes.minihr;

      import java.util.ArrayList;

      public class EmployeeSearchService
      {
      /* Hard-coded sample data. Normally this would come from a real data
      source such as a database. */
      private static Employee[] employees =
      {
      new Employee("Bob Davidson", "123-45-6789"),
      new Employee("Mary Williams", "987-65-4321"),
      new Employee("Jim Smith", "111-11-1111"),
      new Employee("Beverly Harris", "222-22-2222"),
      new Employee("Thomas Frank", "333-33-3333"),
      new Employee("Jim Davidson", "444-44-4444")
      };

      // Search for employees by name.
      public ArrayList searchByName(String name) {
      ArrayList resultList = new ArrayList();

      for (int i = 0; i < employees.length; i++) {
      if (employees.getName().toUpperCase().indexOf(name.toUpperCase()) != -1) {
      resultList.add(employees[i]);
      }
      }

      return resultList;
      }

      // Search for employee by social security number.
      public ArrayList searchBySsNum(String ssNum) {
      ArrayList resultList = new ArrayList();

      for (int i = 0; i < employees.length; i++) {
      if (employees[i].getSsNum().equals(ssNum)) {
      resultList.add(employees[i]);
      }
      }

      return resultList;
      }
      }

      <br> File Name : <b> Employee.java </b>

      package com.solversa;
      public class Employee
      {
           private String name;
           private String ssNum;

           public Employee(String name, String ssNum) {
           this.name = name;
           this.ssNum = ssNum;
           }

           public void setName(String name) {
           this.name = name;
           }

           public String getName() {
           return name;
           }

           public void setSsNum(String ssNum) {
           this.ssNum = ssNum;
           }

           public String getSsNum() {
           return ssNum;
           }
      }


      Pls help me out.
      Not able to prompt errors.
        • 1. Re: Not able to run validation using validation.xml & validator-rules.xml
          843844
          Hello Friends,
          I am not able to run validation using
          validation.xml & validator-rules.xml.
          Entire code in running prefectly but no error
          messages are prompted.

          Following is my code:



          File Name : struts-config.xml


          <struts-config>

          <!-- Form Beans Configuration -->
          <form-beans>
          <form-bean name="searchForm"
          type="com.solversa.SearchForm"/>
          ans>

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

          <!-- Action Mappings Configuration -->
          <action-mappings>
          <action path="/search"
          type="com.solversa.SearchAction"
          name="searchForm"
          scope="request"
          validate="true"
          input="/search.jsp">
          tion>
          </action-mappings>
          <!-- Message Resources Configuration -->
          <message-resources
          parameter="ApplicationResources"/>
          <!-- Validator Configuration -->
          <plug-in
          className="org.apache.struts.validator.ValidatorPlugI
          ">
          <set-property property="pathnames"
          value="/WEB-INF/validator-rules.xml,
          /WEB-INF/validation.xml"/>
          >

          </struts-config>



          <br> File Name : <b> validation.xml </b>


          <form-validation>
          <formset>
          <form name="searchForm">
          <field property="name" depends="minlength">
          <arg key="label.search.name" position = "0"/>
          <arg1 name="minlength" key="${var:minlength}"
          resource="false"/>
          <var>
          <var-name>minlength</var-name>
          <var-value>5</var-value>
          </var>
          </field>
          <field property="ssNum" depends="mask">
          <arg0 key="label.search.ssNum"/>
          <var>
          <var-name>mask</var-name>
          <var-value>^\d{3}-\d{2}-\d{4}$</var-value>
          </var>
          </field>

          /form>
          </formset>
          form-validation>


          <br> File Name : <b> SearchForm.java </b>

          package com.jamesholmes.minihr;

          import java.util.List;
          import org.apache.struts.validator.ValidatorForm;

          public class SearchForm extends ValidatorForm
          {
          private String name = null;
          private String ssNum = null;
          private List results = null;
          public void setName(String name) {
          this.name = name;
          }

          public String getName() {
          return name;
          }

          public void setSsNum(String ssNum) {
          this.ssNum = ssNum;
          }

          public String getSsNum() {
          return ssNum;
          }

          public void setResults(List results) {
          this.results = results;
          }

          public List getResults() {
          return results;
          }
          }


          <br> File Name : <b> SearchAction.java </b>

          package com.jamesholmes.minihr;

          import java.util.ArrayList;

          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;

          public final class SearchAction extends Action
          {
          public ActionForward execute(ActionMapping
          mapping,
          ActionForm form,
          HttpServletRequest request,
          HttpServletResponse response)
          throws Exception
          {       
          EmployeeSearchService service = new
          EmployeeSearchService();
          ArrayList results;
          SearchForm searchForm = (SearchForm) form;
          // Perform employee search based on what criteria
          was entered.
          String name = searchForm.getName();
          if (name != null && name.trim().length() > 0) {
          results = service.searchByName(name);
          else {
          results =
          service.searchBySsNum(searchForm.getSsNum().trim());
          }
          // Place search results in SearchForm for access
          by JSP.
          searchForm.setResults(results);
          // Forward control to this Action's input page.
          return mapping.getInputForward();
          }


          <br> File Name : <b> EmployeeSearchService.java </b>

          package com.jamesholmes.minihr;

          import java.util.ArrayList;

          public class EmployeeSearchService
          {
          /* Hard-coded sample data. Normally this would come
          from a real data
          source such as a database. */
          ivate static Employee[] employees =
          {
          new Employee("Bob Davidson", "123-45-6789"),
          new Employee("Mary Williams", "987-65-4321"),
          new Employee("Jim Smith", "111-11-1111"),
          new Employee("Beverly Harris", "222-22-2222"),
          new Employee("Thomas Frank", "333-33-3333"),
          new Employee("Jim Davidson", "444-44-4444")
          };

          // Search for employees by name.
          public ArrayList searchByName(String name) {
          ArrayList resultList = new ArrayList();
          for (int i = 0; i < employees.length; i++) {
          if
          (employees.getName().toUpperCase().indexOf(name.toU
          pperCase()) != -1) {
          resultList.add(employees[i]);

          }
          return resultList;
          }

          // Search for employee by social security number.
          public ArrayList searchBySsNum(String ssNum) {
          ArrayList resultList = new ArrayList();
          for (int i = 0; i < employees.length; i++) {
          if (employees[i].getSsNum().equals(ssNum)) {
          resultList.add(employees[i]);
          }
          }
          return resultList;

          }

          <br> File Name : <b> Employee.java </b>

          package com.solversa;
          public class Employee
          {
               private String name;
               private String ssNum;

               public Employee(String name, String ssNum) {
               this.name = name;
               this.ssNum = ssNum;
               }

               public void setName(String name) {
               this.name = name;
               }

               public String getName() {
               return name;
               }

               public void setSsNum(String ssNum) {
               this.ssNum = ssNum;
               }

               public String getSsNum() {
               return ssNum;
               }
          }


          Pls help me out.
          Not able to prompt errors.

          Hi,
          Your error message are not displaying because u does not made Message-Resoucrce property file (Resource Bundle) when you make it .

          give it entry in
          struts-config.xml
          <message-resources parameter="ApplicationResources" />
          and
          define key and corresponding error message to key in this ApplicationResources i.e
          #Error Resources
          label.search.ssNum=Plz Enter correct ssNum