1

index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<body>
<div align="center" style="margin-top:100px;">
    <font face="verdana" size="2">
        ${welcomeMessage} <BR><BR>
        ${result}
        <form:form action="${pageContext.request.contextPath}/login.html" method="POST" modelAttribute="loginForm">
            <table>
                <tr>
                    <td colspan="2" align="center">Spring MVC Form Demo - Login</td>
                </tr>
                <tr>
                    <td>User Name</td>
                    <td><form:input path="username" /> <form:errors path="username"></form:errors></td>                 
                </tr>
                <tr>
                    <td>Password</td>
                    <td><form:password path="password" /> <form:errors path="password"></form:errors></td>
                </tr>
                <tr>
                    <td colspan="2" align="center"><input type="submit" value="Login" style="background-color:white;" /></td>
                </tr>                                   
            </table>                                        
        </form:form>
        <a href="${pageContext.request.contextPath}/welcome">Register if not already registered</a>
    </font>
</div> 
</body>
</html>

HelloController.java

package java4s;


import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.context.annotation.Scope;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import java4s.model.Login;

@Controller
public class LoginSuccessController {

    @Autowired
    EmployeeService emp_service;

    @RequestMapping(value = "/login", method=RequestMethod.POST)
    public ModelAndView loginvalidateForm(ModelMap model, @ModelAttribute("loginForm") Login login, BindingResult result, HttpSession session) {

        if(result.hasErrors()){
            model.addAttribute("result", "All Fields are neccessary");
            return new ModelAndView("index",model);
        }
        if(emp_service.validateLogin(login.getUsername(), login.getPassword()))
        {
            List<Employee> user_info = emp_service.getUserinfo(login.getUsername());
            session.setAttribute("session_username", login.getUsername()); //Add value to session variable
            model.addAttribute("result", "Login Success");
            model.addAttribute("user_info", user_info);
            return new ModelAndView("LoginSuccess",model);
        }
        else
        {
            model.addAttribute("result", "Login Failure");
            return new ModelAndView("index",model);     
        }
    }

}

Login.java

package java4s.model;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

public class Login {

    @NotNull
    @Size(min = 3, max = 20)
    private String username;

    @NotNull
    @Size(min = 3, max = 20)
    private String password;

    public String getUsername()
    {
        return username;
    }
    public void setUsername(String username)
    {
        this.username = username;
    }
    public String getPassword()
    {
        return password;
    }
    public void setPassword(String password)
    {
        this.password = password;
    }
}

I am trying to put validation on the login fields when they are empty, but errors are not showing on the index page the the login fields are empty. What is the problem in the code?

2 Answers 2

2

You have to add the annotation @Valid ( see the spring doc for more details) :

public ModelAndView loginvalidateForm(ModelMap model, @Valid @ModelAttribute("loginForm") Login login, BindingResult result, HttpSession session) {
....
}
Sign up to request clarification or add additional context in comments.

2 Comments

Not getting the validation error. Is there anything else I have to put in the code?
The problem is @ModelAttribute("loginForm"), should be replaced by @ModelAttribute("login")
1

Don't forget to enable “mvc:annotation-driven” to make Spring MVC supports @Valid annotation. Add the following tag to your application context XML file.

<mvc:annotation-driven />

3 Comments

It was missing, but when I added it, I am getting the error could not initialize class org.hibernate.validator.engine.configurationimpl. I am using hibernate-validator-4.1.0.Final.jar
And do you have a SLF4J jar in classpath? Check also if the hibernate-validator-4.1.0.Final.jar is deployed in the WEB-INF/lib directory of your web application on the server. Try to see reference guide.
SLF4J jar was not present in the lib dir. I added it & it is working now!! Thanks!!

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.