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

struts validation error

I was trying struts validation example. When I try to run jsp like

http://localhost:8080/StrutsReqWithDataNoForm2/jsps/struts/CustomerDetails.jsp


It is giving error as below, attachment.

Any ideas, suggestions, sample code, links, source code highly appreciated. Thanks in advance




HTTP Status 500 - javax.servlet.ServletException: javax.servlet.jsp.JspException: Cannot retrieve definition for form bean: "CustomerForm" on action: "customer"

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

type Exception report

message javax.servlet.ServletException: javax.servlet.jsp.JspException: Cannot retrieve definition for form bean: "CustomerForm" on action: "customer"

description The server encountered an internal error (javax.servlet.ServletException: javax.servlet.jsp.JspException: Cannot retrieve definition for form bean: "CustomerForm" on action: "customer") that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: javax.servlet.ServletException: javax.servlet.jsp.JspException: Cannot retrieve definition for form bean: "CustomerForm" on action: "customer"
      org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:549)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:455)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:722)


root cause

javax.servlet.ServletException: javax.servlet.jsp.JspException: Cannot retrieve definition for form bean: "CustomerForm" on action: "customer"
      org.apache.jasper.runtime.PageContextImpl

Below URL with customer.do  directly going to confirmation.jsp without going through jsp, form bean.
http://localhost:8080/StrutsReqWithDataNoForm2/customer.do
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">
            <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="satte"></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>




Action class looks like


package actions;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import formbeans.CustomerForm;
public class CustomerAction extends Action {
      public ActionForward execute(ActionMapping mapping, ActionForm form,
                  HttpServletRequest request, HttpServletResponse response)
      throws Exception {
            ActionErrors errors = new ActionErrors();
            CustomerForm customerForm = (CustomerForm) form;
            try {
                  insertCustomerRecord(customerForm.getFirstName(),
                              customerForm.getMiddleName(), customerForm.getLastName(),
                              customerForm.getAge(),customerForm.getSsn(), customerForm.getCity(),
                              customerForm.getState(), customerForm.getCountry());
            } catch (Exception e) {
                  // Report the error using the appropriate name and ID.
                  errors.add("name", new ActionError("id"));
            }
            
            return (mapping.findForward("success"));
      }
      private void insertCustomerRecord(String firstName, String middleName,
                  String lastName, String age, String ssn, String city, String state,      
              String country) {
            
      }}






FormBean Looks like


package formbeans;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionError;
public class CustomerForm extends ActionForm {
      private String ssn = null;
      private String age = null;
      private String country = null;
      private String state = null;
      private String middleName = null;
      private String firstName = null;
      private String city = null;
      private String lastName = null;
      public String getAge() {
            return age;
      }
      public void setAge(String age) {
            this.age = age;
      }
      public String getCity() {
            return city;
      }
      public void setCity(String city) {
            this.city = city;
      }
      public String getCountry() {
            return country;
      }
      public void setCountry(String country) {
            this.country = country;
      }
      public String getFirstName() {
            return firstName;
      }
      
      public void setFirstName(String firstName) {
            this.firstName = firstName;
      }
      public String getLastName() {
            return lastName;
      }
      public void setLastName(String lastName) {
            this.lastName = lastName;
      }
      public String getMiddleName() {
            return middleName;
      }
      public void setMiddleName(String middleName) {
            this.middleName = middleName;
      }
      public String getSsn() {
            return ssn;
      }
      public void setSsn(String ssn) {
            this.ssn = ssn;
      }
      public String getState() {
            return state;
      }
      public void setState(String state) {
            this.state = state;
      }
      public ActionErrors validate(ActionMapping mapping, HttpServletRequest
                  request) {
            ActionErrors errors = new ActionErrors();
            
            if ((lastName == null) || (lastName.length() == 0)) {
                  errors.add("lastName", new ActionError("error.lastName.required"));
            }
            if ((ssn == null) || (ssn.length() == 0)) {
                  errors.add("ssn", new ActionError("error.ssn.required"));
            }
            // Validation for valid values for SSN and Age
            try {
                  int ssn1 = Integer.parseInt(ssn);
            } catch (Exception e) {
                  // If it comes here, then the user entered non-numeric data for SSN
                  errors.add("ssn_number", new ActionError("error.ssn.invalid"));
            }
            try {
                  int age1 = Integer.parseInt(age);
            } catch (Exception e) {
                  errors.add("age_number", new ActionError("error.age.invalid"));
            }
            return errors;
      }
}





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-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="/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" />
</struts-config>








0
gudii9
Asked:
gudii9
1 Solution
 
krishna kishore mellacheruvu venkataCommented:
add the following under <form-beans> tag
 <form-bean name="customerForm" type="formbeans.CustomerForm">
            </form-bean>

And change
       <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>
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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