Announcement Announcement Module
Collapse
No announcement yet.
Validator - Error messages not getting displayed in JSP Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Validator - Error messages not getting displayed in JSP

    Hi,

    I am new to Spring framework and trying to implement a login screen. How ever, when I am using Validator the errors messages are not getting displayed in JSP page. When the jsp page loads, Its not parsing ${status.errorMessage} and it is displaying as it is.

    /////////////////////////////JSP Page

    <%@ include file="include.jsp" %>
    <%@ taglib uri="/spring" prefix="spring" %>
    <html>
    <head><title><fmt:message key="title"/></title></head>
    <body>
    <h1><fmt:message key="heading"/></h1>
    <spring:bind path="user">
    <FONT color="red">
    <B><c:out value="${status.errorMessage}"/></B>
    </FONT>
    </spring:bind>
    <form method="post">
    <table width="95%" bgcolor="f8f8ff" border="0" cellspacing="0" cellpadding="5">
    <tr>
    <td align="right" width="20%">UserId:</td>
    <spring:bind path="user.userId">
    <td width="20%">
    <input type="text" name="userId" />
    </td>
    <td width="60%">
    <font color="red"></font>
    </td>
    </spring:bind>
    </tr>
    <tr>
    <td align="right" width="20%">Password:</td>
    <spring:bind path="user.password">
    <td width="20%">
    <input type="password" name="password" />
    </td>
    <td width="60%">
    <font color="red"></font>
    </td>
    </spring:bind>
    </tr>
    </table>
    <br>
    <input type="submit" value="Login">
    </form>
    </body>
    </html>

    /////////////////////////////Validator class
    public void validate(Object arg0, Errors errors) {
    log.info("^^^^I am in Validator^^^^");
    User user = (User)arg0;
    if(user==null){
    errors.reject("error.nullpointer", "Null data received");
    }else{
    log.info("**********Login form is null*******");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "userId", "required","userId.not-entered");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password","required","password.not-entered");
    }
    List err = errors.getFieldErrors();
    log.info("Field Errors: "+err.size());
    for(int i=0;i<err.size();i++){
    log.info(err.get(i));
    }
    }

    Thanks in advance!

  • #2
    Hi there,

    I'd guess that you need to declare the use of the <c:xxx> tag library, by adding something like this to the top of the file...

    Code:
    <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
    Hope this helps.

    Comment


    • #3
      Oh yes - you might want this as well...

      Code:
      <%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>

      Comment


      • #4
        My include.jsp has those declarations. Even then, my JSP does not display the error messages.

        As per spring, if we bind the text fields to properties of command class and then if we use ${status.errorMessage} - it will automatically display the error messages for that field. Did I miss any thing here?

        Thanks

        Comment


        • #5
          Try adding this code in to confirm that you have errors in the page.

          Code:
          <spring:hasBindErrors name="<commandBean>">
          		<c:out value="${errors.errorCount}" />
          </spring:hasBindErrors>
          where <commandBean> is the backing object with the errors.
          Last edited by Pierre Javason; May 21st, 2008, 07:06 PM. Reason: corrected statement

          Comment


          • #6
            regarding display of error messages

            Hope <spring bind> tag is bit old. just try <form:input > similar tags for binding values.. And then use ValidationUtils functionalities to display error messages . the following are the working copies of code .


            THIS IS THE CODE FOR VALIDATOR

            import org.apache.commons.logging.Log;
            import org.apache.commons.logging.LogFactory;
            import org.springframework.validation.Errors;
            import org.springframework.validation.Validator;
            import org.springframework.validation.ValidationUtils;
            import org.apache.commons.lang.StringUtils;
            import com.web2labs.GHDesk.dto.User;

            public class LogOnValidator implements Validator
            {



            public boolean supports(Class clazz)
            {
            return User.class.isAssignableFrom(clazz);
            }

            public void validate(Object obj,Errors errors)
            {
            System.out.println("inside validator");
            User user = (User) obj;
            ValidationUtils.rejectIfEmptyOrWhitespace(errors, "uname", null, "required");
            ValidationUtils.rejectIfEmptyOrWhitespace(errors," password" , null, "required");

            System.out.println(user.getUname()+" "+user.getPassword());

            System.out.println(user.getUname().equals("guest") );



            if((!user.getUname().equals("guest")))
            {
            errors.rejectValue("uname",null,"enter the username as guest");
            }
            //if(!(StringUtils.equals(user.getUname(), "guest")))
            //{
            // errors.rejectValue("uname",null,"enterthe username as guest ");
            // }
            if(!(StringUtils.equals(user.getPassword(), "guest")))
            {
            errors.rejectValue("password",null,"enter the password as guest");
            }



            }
            }

            THIS IS THE CORRESPONDING JSP CODE


            <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
            <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>


            <html>

            <head>
            <title></title>
            <style>
            .error {
            color: red;
            }
            </style>
            </head>

            <body>
            <h1></h1>

            <form:form method="post">



            Name <form:errors path="uname" cssClass="error"/><br />
            <form:input path="uname"/><br /><br />


            Password <form:errors path="password" cssClass="error"/><br />
            <formassword path="password"/><br /><br />


            <input type="submit" value="Submit">

            </form:form>
            </body>
            </html>

            THIS IS THE SERVLET CONFIGURATION


            <beans>
            <bean id="logonvalidator" class="com.web2labs.GHDesk.controller.LogOnValidat or"/>
            <bean id="logon" class="com.web2labs.GHDesk.controller.LogonControl ler">
            <property name="sessionForm"><value>true</value></property>

            <property name="commandClass"><value>com.web2labs.GHDesk.dto .User</value></property>
            <property name="validator"> <ref bean ="logonvalidator"/></property>
            <property name="formView"><value>logon</value></property>
            <property name="successView"><value>home.htm</value></property>
            </bean>


            HOPE U WOULD BENEFIT FROM THIS

            Comment

            Working...
            X