Results 1 to 4 of 4

Thread: Validation and displaying errors in jsp

  1. #1

    Default Validation and displaying errors in jsp

    Hi, i'd appreciate any help with my questions pertaining to validation.
    First off, i have a jsp form with a controller which extends the SimpleFormController setup. Everything works perfectly.
    I introduced the validator bit and added this line in the servlet bean xml file
    Code:
            <property name="validator">
                <bean class="com.suno.gt.web.handler.TradeEntryValidator"/>
            </property>
    The validator right now just has one rejectifempty method -
    Code:
    public class TradeEntryValidator implements Validator &#123;
        static Logger logger = Logger.getLogger&#40;TradeEntryValidator.class.getName&#40;&#41;&#41;; // logger
    
    
        public boolean supports&#40;Class aClass&#41; &#123;
            return aClass.equals&#40;SmtmTradeMessage.class&#41;;
        &#125;
    
        public void validate&#40;Object obj, Errors errors&#41; &#123;
            SmtmTradeMessage trade = &#40;SmtmTradeMessage&#41;obj;
            logger.info&#40;"Validating trade object"&#41;;
            ValidationUtils.rejectIfEmpty&#40;errors, "quantity", "required.quantity", "Quantity is required"&#41;;
        &#125;
    
    
    &#125;

    My question is how does the validaotr return errors (if any ) to the jsp page. Do i need to return anything and point it to the "formview" jsp page.
    If so, how do i get and display the errors on the jsp page?
    I read in a few places the use of the <spring:bind> tag. Is it possible for me to use the validator without using this tag to display errors on the jsp?
    Thanks in advance for any help. I'd also appreciate it if you coud point me to examples or tutorials.
    Thank you,
    ...D

  2. #2
    Join Date
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,104

    Default

    My question is how does the validaotr return errors (if any ) to the jsp page
    Effectively this just calls...
    Code:
    errors.rejectValue&#40;field, errorCode, errorArgs, defaultMessage&#41;;
    if the quantity field is empty. The errors object is passed to the JSP.

    If so, how do i get and display the errors on the jsp page?
    From the petclinic sample:
    Code:
      <spring&#58;bind path="command.name">
        <FONT color="red">
          <B><c&#58;out value="$&#123;status.errorMessage&#125;"/></B>
        </FONT>
        <BR><INPUT type="text" maxlength="30" size="30" name="name" value="<c&#58;out value="$&#123;status.value&#125;"/>" >
      </spring&#58;bind>
    The <c:out value="${status.errorMessage}"/> will display the error.

    Is it possible for me to use the validator without using this tag to display errors on the jsp?
    Yes. Add you're error:
    Code:
    errors.reject&#40;code&#41;;
    Then display:
    Code:
       <spring&#58;hasBindErrors name="command">
          <c&#58;forEach items="$&#123;errors.globalErrors&#125;" var="error">
            <div class="error">
            	<spring&#58;message code="$&#123;error.code&#125;" />
            </div>
          </c&#58;forEach>
       </spring&#58;hasBindErrors>
    I'd also appreciate it if you coud point me to examples or tutorials.
    The samples in the Spring distribution are a great place to start.

  3. #3

    Default Thanks

    Thanks katentim, for the quick and helpful reply .

  4. #4
    Join Date
    Jun 2008
    Posts
    14

    Default Not able to display Validation errors in JSP

    I have a Student bean with name, email and phone number. I am using Java 1.4 with RAD 6.0. There are no compilation or run time errors that I see. I have written a validator for Student and it is being accessed and error messages are displayed in the console saying that name is empty if I don't enter the name.

    However, I am not able to display this in JSP although I have used <spring:bind>

    //In validator
    public void validate(Object command,Errors errors)
    {
    Student student = (Student)command;
    ValidationUtils.rejectIfEmpty(errors,"name","requi red.name","Name is required");
    ValidationUtils.rejectIfEmpty(errors,"email","requ ired.email","Email is required");
    ValidationUtils.rejectIfEmpty(errors,"phoneNumber" ,"required.phoneNumber","Phone Number is required");

    }

    In JSP,
    <spring:bind path="student.name">
    <input type="text" name="name" value="${status.value}" />
    </spring:bind>

    <spring:hasBindErrors name="student">
    <font color="red">${status.errorMessage}</font>
    </spring:hasBindErrors>

    In configuration file for Spring
    <bean id="getSubjectsController"
    class="com.assurant.GetSubjectsController">
    <property name="formView">
    <value>InputName</value>
    </property>
    <property name="successView">
    <value>OutputSubjects</value>
    </property>
    <property name="commandName"><value>student</value></property>
    <property name="commandClass"><value>com.assurant.Student</value></property>
    <property name="validator">
    <bean class="com.assurant.StudentValidator" />
    </property>
    </bean>

    I see no errors and Validation is being accessed correctly when I don't enter anything for "name". But why the error message which I see on console is not visible in JSP?

    I had followed the link posted on the forum..but not working

Similar Threads

  1. Validation Errors will not display
    By thus in forum Web
    Replies: 1
    Last Post: Oct 18th, 2005, 04:40 PM
  2. ideas on displaying validation errors
    By lindbird in forum Web
    Replies: 1
    Last Post: Aug 26th, 2005, 01:03 PM
  3. Validation only supports Errors
    By Paul Taylor in forum Web
    Replies: 3
    Last Post: Apr 19th, 2005, 03:47 AM
  4. Displaying server-side validation messages
    By Jurijus Jarmakas in forum Swing
    Replies: 2
    Last Post: Feb 15th, 2005, 08:57 AM
  5. Replies: 3
    Last Post: Oct 19th, 2004, 09:20 AM

Posting Permissions

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