Announcement Announcement Module
Collapse
No announcement yet.
Validation and displaying errors in jsp Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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.

    Comment


    • #3
      Thanks

      Thanks katentim, for the quick and helpful reply .

      Comment


      • #4
        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

        Comment

        Working...
        X