?
Solved

struts validation error

Posted on 2012-08-16
1
Medium Priority
?
1,228 Views
Last Modified: 2012-08-17
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
Comment
Question by:gudii9
1 Comment
 
LVL 10

Accepted Solution

by:
krishna kishore mellacheruvu venkata earned 2000 total points
ID: 38301454
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month16 days, 3 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question