Results 1 to 8 of 8

Thread: basic jsp validation using spring mvc

  1. #1
    Join Date
    Dec 2008
    Posts
    26

    Default basic jsp validation using spring mvc

    hello,
    i am new to spring mvc. i have a form to be validated after which i need to show the error messages beside the field. how do i do that. and also i am not able to run the onsubmit() extended form the simpleformcontroller when i am using the validator class. can u plz send me the code how to do the validation stuff?

  2. #2
    Join Date
    Sep 2007
    Location
    Santiago,Chile
    Posts
    10

  3. #3
    Join Date
    Dec 2008
    Posts
    26

    Default getting jsptagexception

    yes, i did the same thing as mentioned in the site but its not able to recognize the spring tags. i am getting jsptagexception while using the spring:bind tag. can u pls help me with this? also if i use <%@ taglib prefix="spring" uri="/spring" %> it says it cannot find /spring in the jsp

  4. #4

    Default

    I'm new to Spring MVC myself, so sorry is any of the explanations is wrong

    First, what version of Spring do you use? I think the spring:bind tag has been deprecated ... Anyway, my example will be based on Spring 2.5 and I will use the form tag to bind the fields values.

    We have the following Employee object:

    Code:
    public class Employee {
      private String employeeNo;
      private String firstName;
      private String lastName;
      
      // Getters and setters for those 3 fields
    }
    We'll ensure that those 3 fields aren't empty when the form is submitted. We then need to write a class that implements the Validator interface - there are other ways to validate the form, but implementing the Validator interface is the easiest.

    Code:
    public class EmployeeValidator implements Validator {
      @Override
      public boolean supports(Class aClass) {
        return Employee.class.isAssignableFrom(aClass);
      }
    
      @Override
      public void validate(Object target, Errors errors) {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "employeeNo", "Employee no can't be blank");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "firstName", "First name can't be blank");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "lastName", "Last name can't be blank");
      }
    }
    You have to overrides two method: supports and validate. Override the supports method to ensure the object you're validating is the employee object (or assignable from it). The validation itself is done on the validate method. It takes two parameters: 'Object target' which is the object we're validating and 'Errors errors' which is an error object to which we'll bound any field error.

    Spring provides ValidationUtils class to do basic validations. In this instance, I'm using the 'rejectIfEmptyOrWhitespace()' method to check that the field is not empty. The one i'm using takes 3 parameters: the error object, the field to be validated, the error message to be shown.

    If there is any error, then a field error is created and bounded to the field.

    Finally, the jsp code:

    Code:
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
    <html>
      <head>
        <title>Employee Form</title>
        <style>
          .error {
            color: #ff0000;
            font-weight: bold;
          }
        </style>
      </head>
      <body>
        <form:form method="POST" commandName="employee">
          <form:errors path="*" cssClass="error" />
          <table>
            <tr>
              <td>Employee No</td>
              <td><form:input path="employeeNo" /></td>
              <td><form:errors path="employeeNo" cssClass="error" /></td>
            </tr>
            <tr>
              <td>First Name</td>
              <td><form:input path="firstName" /></td>
              <td><form:errors path="firstName" cssClass="error" /></td>
            </tr>
            <tr>
              <td>Last Name</td>
              <td><form:input path="lastName" /></td>
              <td><form:errors path="lastName" cssClass="error" /></td>
            </tr>
            <tr>
              <td><input type="submit" value="SAVE" /></td>
            </tr>
          </table>
        </form:form>
      </body>
    </html>
    I put the error information in two places: at the top of the form which shows all errors thrown.
    Code:
    <form:errors path="*" cssClass="error" />
    Also, below each field by including this code after each field declaration:
    Code:
    <td>Last Name</td>
    <td><form:input path="lastName" /></td>
    <td><form:errors path="lastName" cssClass="error" /></td>
    Hope it helps

  5. #5
    Join Date
    Dec 2008
    Posts
    26

    Default

    hey thnks i am doing this and i got an error saying javax.servlet.jsp.JspTagException: Neither BindingResult nor plain target object for bean name 'command' available as request attribute


    can u polz help me with this

  6. #6

    Default

    I found a thread discussed about similar error.
    It has not yet been solved, but it may be a good reference for you.

  7. #7

  8. #8
    Join Date
    Dec 2008
    Posts
    26

    Default display errors using the onsubmit method

    hello, has any1 worked with the onsubmit method for displaying the errrors in the JSP?

    it has onSubmit(request, response, object, bindException). from this method how do i use the bindException for dispalying errors?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •