Question

Hi i am new to spring mvc validation framework.I am trying to implement a simple custom validator for the login form.

I did this way , but result.hasErrors() is always returning null and my control is moving to success page.

Login.jsp

<form:form method="POST" action="/SampleWeb/logins" commandName="USER">

<table align="center" border="5" bordercolor="orange">
<tr>
    <td style="color: white" align="center"><label>User Name</label></td>
    <td><input name="userName" /></td>
</tr>
<tr>
    <td style="color: white" align="center"><label>Password</label></td>
    <td><input name="password" type="password"/></td>
</tr>
<tr>
    <td colspan="2" style="color: white" align="center">        
         <input type="submit" value="Submit"/>
    </td>
</tr>
</table>
<h3 align="center" style="color: red">${error}</h3>     
</form:form>

Sample-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
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:annotation-config />   
    <mvc:annotation-driven />

   <context:component-scan base-package="com.sample.web.controller" />
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/jsp/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>

    </bean> 

    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
      <property name="basename" value="/WEB-INF/messages"/>
    </bean> 

</beans>

UserValidator(my custom validator)

import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

@Component
public class UsersValidator implements Validator {

  @Override
  public boolean supports(Class<?> clazz) {
    return clazz.isAssignableFrom(users.class);
  }
  @Override
  public void validate(Object obj, Errors errors) {
    users user = (users) obj;
    String userName = user.getUserName();
    validateuserName(userName, errors);
  }
  private void validateuserName(String userName, Errors errors) {

    if (isValidString(userName) && isNotBirminghamuserName(userName)) {
      errors.rejectValue("userName", "UsersValidator.userName.notBirmingham",
          "Not a b group user ");
    }
  }
  private boolean isValidString(String str) {
    return isNotNull(str) && (str.length() > 0);
  }
  private boolean isNotNull(String userName) {
    return userName != null;
  }
  /** The first character of the Birmingham post code is 'B' */
  private boolean isNotBirminghamuserName(String userName) {
    char val = userName.charAt(0);
    return val != 'B';
  }
}

Controller

import javax.validation.Valid;

@Controller
public class LoginController {
    @Autowired
    private UsersValidator usersValidator;

   final RequestHandler  requestHandler = new RequestHandler();
   @RequestMapping(value = "/login", method = RequestMethod.GET)
   public ModelAndView login() {
      return new ModelAndView("login", "users", new users());
   }

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

   @RequestMapping(value = "/logins",method = RequestMethod.POST)
   public ModelAndView validateUser(@Valid users user, BindingResult result,@ModelAttribute("USER")users users,RedirectAttributes redirectAttributes,ModelMap model) {

       if (result.hasErrors()) {
              model.addAttribute("error",
                  "No Errors This Time for postal code: " + user.getUserName());
            }
            return new ModelAndView("sample", model);
       }
}

Users.java ( my pojo)

public class users implements Serializable {
    private String userName;
    private String password;        
    //getter setters    
}

I mean validations in validator class are skipped and the control is directly going to success page. Can some one help me in this issue?

Était-ce utile?

La solution

Try refactoring your validateUser method so that it's the modelAttribute that gets validated.

@RequestMapping(value = "/logins",method = RequestMethod.POST)
public ModelAndView validateUser(@ModelAttribute("USER") @Valid users users, BindingResult result, RedirectAttributes redirectAttributes, ModelMap model) {

   if (result.hasErrors()) {
      model.addAttribute("error",
          "No Errors This Time for postal code: " + user.getUserName());
    }
    return new ModelAndView("sample", model);
   }
}

Autres conseils

In addition to Will Keeling's answer of cleaning up your arguments in the controller, try using modelAttribute="users" in your form:form tag instead of commandName="USER".

Source: http://codetutr.com/2013/05/28/spring-mvc-form-validation/

I'd also suggest naming your POJO starting with uppercase and in the singular form: User instead of users, since each instance of the class will be an individual user.

Other than that, it all looks good to me.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top