Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8540615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:38:22+00:00 2026-06-11T11:38:22+00:00

I was learning how to make simple form validation using this tutorial: http://www.raistudies.com/spring/spring-mvc/form-validation-spring-mvc-3-hibernate-validator-jsr-303/ The

  • 0

I was learning how to make simple form validation using this tutorial:
http://www.raistudies.com/spring/spring-mvc/form-validation-spring-mvc-3-hibernate-validator-jsr-303/

The problem is, when I’m entering invalid data (blank fields), there is no errors for user anywhere. result.hasErrors() always produce false.

I’ve produced following code:

IndexController.java:

package pl.aadamczyk.springtest.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import pl.aadamczyk.springtest.beans.LoginBean;

@Controller
@RequestMapping("/index")
public class IndexController
{   
    @RequestMapping(method = RequestMethod.GET)
    public String getIndex(ModelMap model)
    {
        LoginBean loginBean = new LoginBean();
        model.addAttribute("loginBean", loginBean);

        return "index";
    }
}

+
LoginController.java:

package pl.aadamczyk.springtest.controllers;

import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import pl.aadamczyk.springtest.beans.LoginBean;
import pl.aadamczyk.springtest.validators.LoginValidator;

@Controller
@RequestMapping("/login")
public class LoginController
{
    private LoginValidator loginValidator;

    private LoginValidator getValidator()
    {
        return loginValidator;
    }

    @Autowired
    public void setValidator(LoginValidator loginValidator)
    {
        this.loginValidator = loginValidator;
    }

    @RequestMapping(method= RequestMethod.POST)
    public String login(@Valid @ModelAttribute(value="loginBean") LoginBean loginBean,
                        ModelMap model,
                        BindingResult result)
    {
        System.out.println(result.hasErrors()); // always false

        loginValidator.validate(loginBean, result);

        return "redirect:index.html";

    }
}

+
LoginValidator.java

package pl.aadamczyk.springtest.validators;

import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import pl.aadamczyk.springtest.beans.LoginBean;

@Component
public class LoginValidator implements Validator
{
    @Override
    public boolean supports(Class<?> type)
    {
        return LoginBean.class.isAssignableFrom(type);
    }

    @Override
    public void validate(Object o, Errors errors)
    {

    }
}

LoginBean.java:

package pl.aadamczyk.springtest.beans;

import org.hibernate.validator.Length;
import org.hibernate.validator.NotEmpty;

public class LoginBean
{
    @NotEmpty(message="Username is mandatory.")
    @Length(min=3,max=25,message="Username must be between 3 and 25 characters length")
    private String username;

    @NotEmpty(message="Password field is mandatory.")
    @Length(min=3,max=25,message="Password must be between 5 and 25 characters length")
    private String password;

    // getters+setters skipped
}

Part of index.jsp:

<form:form method="post" action="login.html" modelAttribute="loginBean">
    <table>
        <tr>
            <td>Login:</td>
            <td><form:input path="username"/></td>
            <td><form:errors path="username"/></td>
        </tr>
        <tr>
            <td>Password</td>
            <td><form:input path="password"/></td>
            <td><form:errors path="password"/></td>
        </tr>
        <tr>
            <td colspan="3"><button type="submit">Login</button></td>
        </tr>
    </table>
</form:form>

Important content of dispatcher-servlet:

<context:annotation-config /> 
<context:component-scan base-package="pl.aadamczyk.springtest"/>
<mvc:annotation-driven/>

<bean id="viewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver"
      p:prefix="/WEB-INF/jsp/"
      p:suffix=".jsp" />

pom.xml dependencies:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>3.1.1.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-asm</artifactId>
    <version>3.1.1.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
    <version>3.1.1.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>3.1.1.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>3.1.1.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>3.1.1.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>3.1.1.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-expression</artifactId>
    <version>3.1.1.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-instrument</artifactId>
    <version>3.1.1.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-instrument-tomcat</artifactId>
    <version>3.1.1.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>3.1.1.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jms</artifactId>
    <version>3.1.1.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-orm</artifactId>
    <version>3.1.1.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-oxm</artifactId>
    <version>3.1.1.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>3.1.1.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>3.1.1.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>3.1.1.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc-portlet</artifactId>
    <version>3.1.1.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>3.1.1.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-struts</artifactId>
    <version>3.1.1.RELEASE</version>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.1.2</version>
</dependency>
<dependency>
    <groupId>taglibs</groupId>
    <artifactId>standard</artifactId>
    <version>1.1.2</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>3.0.0.ga</version>
    <type>jar</type>
</dependency>
<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-web-api</artifactId>
    <version>6.0</version>
    <scope>provided</scope>
</dependency>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T11:38:23+00:00Added an answer on June 11, 2026 at 11:38 am

    To get validation working, make sure you have a recent version of hibernate-validator in your classpath.

    To print the errors in A JSP page, use something like the following

    <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
    ...
    
     <spring:hasBindErrors name="person">
        <div class="error">
            <c:forEach var="error" items="${errors.allErrors}">
                <p>Errors ${error.defaultMessage}</p>
            </c:forEach>
        </div>
    </spring:hasBindErrors>
    

    You also need the jstl and spring-webmvc libraries in your classpath.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm just learning sencha touch 2, MVC. I would to make a simple form
I'm still writing my simple MVC for learning and eventually make a framework for
i want make a simple delete using Ajax but this dont works, i wantmake
I am trying to make simple notepad application for learning android development. So, to
I'm learning Objective-C and trying to make a very simple command line calculator. 'S'
I have a quick question. I am learning how to make a registration form
This question is for learning purposes. Suppose I am writing a simple SQL admin
I'm learning smalltalk right now and am trying to make a very simple program
I am learning some myqli and would like to make a simple check. Basically,
I'm working on learning to use boost threads. I'm trying to make a simple

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.