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 6063055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:07:18+00:00 2026-05-23T09:07:18+00:00

I am writing Web app in java using Spring Web MVC framework. Somehow validation

  • 0

I am writing Web app in java using Spring Web MVC framework. Somehow validation does not work.
Below is depicted the controller class:

@Controller
public class UserNameController 
{

    @InitBinder()
    protected void initBinder(WebDataBinder binder) {
        binder.setValidator(new UserNameValidator());
    }

          // 
        @RequestMapping(value="userName.htm", method=RequestMethod.POST)
        public String doForm(@ModelAttribute("user") @Valid User user, BindingResult result, Model model) 
        {         
            System.out.println("------------ "+ user.getuName()+" ---------");
            if (result.hasErrors()) {
                System.out.print("===== errors ======");
            }

            model.addAttribute("user",user);
            return "registration";
        }


  public class UserNameValidator implements Validator 
  {
    public boolean supports(Class clazz) 
    { System.out.println("========== "+User.class.isAssignableFrom(clazz)+" ===================");
       return User.class.isAssignableFrom(clazz);    
    }

     public void validate(Object target, Errors errors)
     {
         System.out.println("=======================");
         User newUser = (User) target;
         ValidationUtils.rejectIfEmptyOrWhitespace(errors, "uName", "field.required", "Required field");
         if(errors.hasFieldErrors("uName"))
         {
          //   if(user.existUser() == true)
            // {

             //}
             System.out.print("===== errors 2 ======");
         }

     }
  } 

}

Method ‘supports’ is invoked what gives true value, though ‘validate’ method stays idle. What is the problem?

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"    
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

       <context:component-scan base-package="forum.web" />      


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

</beans>

User.java source file:

public class User
{

    private String uName;

    public User(){}

    public User(String uName)
    { System.out.println("=== Constructor==== " + uName);
        this.uName= uName;
    }
    public String toString()
    {System.out.println("=====toString()==== " + uName);
        return this.uName;
    }

    public String getuName()
    {
        return uName;
    }
    public void setuName(String uName)
    {
       this.uName = uName;  
    }
}

Main part of jsp page:

<form id="userName" action="userName.htm" method="post" accept="text/plain" accept-charset="UTF-8" enctype="application/x-www-form-urlencoded">

        <td>
        <label for> User Name: 
        </td> 
        <td> </td>
        <td> </td>
        <td>
            <form:errors path="uName"/>
        <input type="text" name="uName" maxlength="20" size="40" onmouseout="submitUName()" value="${user}"/> </p> </p>
        </td>
        <td>*</td>
        </form>       
    </tr> 

This is my updated Controller (new version):

@Controller
public class UserNameController 
{

   private Validator validator;

  public void setValidator(Validator validator)
   {
       this.validator= validator;
   }

    @InitBinder("user")
    protected void initBinder(WebDataBinder binder) {
        binder.setValidator(new UserNameValidator());
        System.out.println("A binder for object: =============== " + binder.getObjectName());
    }

        @RequestMapping(value="userName.htm", method=RequestMethod.POST)
        public String userName(@ModelAttribute("user") User user, BindingResult result, Model model) 
        {         
            this.validator.validate(user, result);
            System.out.println("------------ "+ user.getuName()+" ---------");
            if (result.hasErrors()) {
                System.out.print("===== errors ======");
            }

            model.addAttribute("user",user);
            return "registration";
        }

  public class UserNameValidator implements Validator 
  {
    public boolean supports(Class clazz) 
    { System.out.println("========== "+User.class.isAssignableFrom(clazz)+" ===================");
       return User.class.isAssignableFrom(clazz);    
    }

     public void validate(Object target, Errors errors)
     {
         System.out.println("=======================");
         User newUser = (User) target;
         ValidationUtils.rejectIfEmptyOrWhitespace(errors, "uName", "field.required", "Required field");
         if(errors.hasFieldErrors("uName"))
         {
          //   if(user.existUser() == true)
            // {

             //}
             System.out.print("===== errors 2 ======");
         }

     }
  }

}

Though for some reasons this line this.validator.validate(user, result); causes NullPointerException. Anyway, ‘validate’ method is not invoked as above.
Best regards

  • 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-05-23T09:07:20+00:00Added an answer on May 23, 2026 at 9:07 am

    Try specifying which object (model attribute or request parameter) the binder will be applied to, e.g. @InitBinder(“user”). Note that you can provide an array of names.

    Also, FYI you might be able to learn more about what’s going on by doing this in your initBinder method:
    System.out.println(“A binder for object: ” + binder.getObjectName());

    Another thing to check: that your JSP uses the same names as your controller. What does your tag look like? Does it have the modelAttribute=”user” attribute set correctly?

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

Sidebar

Related Questions

I am writing web app for java learning. Using which users may compile their
I'm writing a Flex app on top of a Java web application using BlazeDS.
I am building a Java web app, using the Play! Framework . I'm hosting
I'm writing a web app (Java) which allows users to select contacts. The contacts
I am writing a web app using TurboGears, and in that app the users
I am writing a simple web app using Linq to Sql as my datalayer
I'm writing a backend for a mobile web-app based in Java and I was
i am writing a web proxy in java on google App engine.I figured out
This is driving me bonkers. I'm writing a web app in Java and all
I'm writing a Java web app in my free time to learn more about

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.