1

I am new to Spring. I am trying to show error messages on jsp for the wrong user and password by using BindingResult. But the error messages are not showing.

Please suggest me what I is wrong in the below code.

JSP

<script type="text/javascript">      
    function loginUser() {              
        $('#loginForm').submit();    
    }

</script>

</head>

<body>
    <form:form action="login.test" id="loginForm" commandName="loginForm" method="POST">
    <div class="brand_area"></div>
        <div class="content_area">          
            <table style="top: 360px; position: relative; margin-left: 333px;">
                <tr id="uNameID">
                    <td class="label">User Name:</td>
                    <td><form:input id="userNameID" path="userName" class="textInput" /></td>
                    <td><form:errors path="userName" class="error"/></td>
                </tr>
                <tr id="pID">
                    <td class="label">Password:</td>
                    <td><form:password id="passwordID" path="password" class="textInput" /></td>
                    <td><form:errors path="password" class="error"/></td>
                </tr>
                <tr>
                    <td></td>
                    <td><span id="saveButton" class="loginButton"
                        onclick="loginUser()"> <span>Login</span>
                    </span></td>
                </tr>
            </table>
        </div>
    </form:form>

Controller

@RequestMapping(value = "login.test", method = RequestMethod.POST)
public String processForm( LoginForm loginForm, BindingResult result, ModelMap model, HttpSession session) throws SQLException {

    String resultedPage;

    model.addAttribute("l_nodes", reportService.getAllLiveNodes());
    model.addAttribute("s_nodes", reportService.getAllStaticReportNodes());

    User user = userService.getUserByName( loginForm.getUserName() );

    if( user != null ){
        session.setAttribute("userID", user.getUserID());
        if( loginForm.getPassword().equals( user.getPassword() ) ){


            resultedPage = "home/userHome";

        }else{
            result.rejectValue( "password", "login.passwordNotValid");
            resultedPage = "redirect:login.test";
        }
    }else{
        result.rejectValue( "userName", "login.userNotValid");
        resultedPage = "redirect:login.test";
    }

    return resultedPage;
}

Thanks

2 Answers 2

2

In case anyone else research the same...
Add the hasBindErrors tag to your JSP:

        <spring:hasBindErrors name="loginForm">
                <c:forEach var="error" items="${errors.allErrors}">
                <b><spring:message message="${error}" /></b>
                <br/>
                </c:forEach>
         </spring:hasBindErrors>
Sign up to request clarification or add additional context in comments.

Comments

1

Well, i generally send back the values using the Model object.

May be this answer might help you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.