• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Validation is not working in Annotaion based controller

 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a validator class----

public class UserValidator implements Validator{
@Override
public boolean supports(Class<?> clazz) {
return User.class.equals(clazz);
}
@Override
public void validate(Object target, Errors errors) {
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "firstName", "firstName.required","give proper value");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "lastName", "lastName.required","give proper value");
}
}



A form in index.jsp---

<form:form method="POST" commandName="user">
<h3> First Name</h3>
<form:errors path="firstName" />
<form:input path="firstName" />
<h3>Last Name </h3>
<form:errors path="lastName" />
<form:input path="lastName" />
<input type="submit" value="+" />
</form:form>



A controller class---

@Controller
@RequestMapping("/userRegistration.htm")
public class UserController {
UserValidator userValidator;
@Autowired
UserController(UserValidator userValidator){
this.userValidator=userValidator;
}
@RequestMapping(method = RequestMethod.GET)
public String showUserForm(ModelMap model)
{
User user = new User();
model.addAttribute(user);
return "index";
}
@RequestMapping(method = RequestMethod.POST)
public String onSubmit(User user) {
return "result";
}
}



And dispatcher-servlet.xnl---

<context:component-scan base-package="controller" />
<bean id="userValidator" class="validator.UserValidator" />
<bean id="viewResolver"
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 class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />


All classes are placed in proper packages
The application is running but it is not validating for firstName and lastName in index.jsp.If I left places blank it is not showing any error

What changes I will have to make ?
please help
 
Abhra Kar
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please give some suggestion
i need help for this
 
Abhra Kar
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Helloooooooo

Still waiting for the response
Provide any suggestion , any help
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're passing the validator into the controller, but you're not telling it what to do with it.

Specifically, two parts you are missing are setting the validator in initBinder() and telling the form that you want to validate the User object by using the @Valid annotation in onSubmit().



P.S. - Patience is a virtue.
 
Abhra Kar
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot .
As you has written I have done this.TO compile the controller class properly I had to import

import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.WebDataBinder;
and
import javax.validation.Valid;
for validation.Valid i had to load one extra jar ---MAVEN2 http://mirrors.ibiblio.org/pub/mirrors/maven2/org/apache/geronimo/specs/geronimo-validation_1.0_spec/1.0-CR5/

geronimo-validation_1.0_spec-1.0-CR5.jar



But still the problem persists.I think i will have to change the logic in --
@RequestMapping(method = RequestMethod.POST)
public String onSubmit(@Valid User user) {
return "result";
}


please give some suggestion

and to understand the proper flow in annotation based validation provide some links or book name
thanks again
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you have <mvc:annotation-driven/> in your spring configuration? This is needed for the @Valid annotation to actually do anything.

The Spring Docs - Ch. 5.7 Validation is a good reference.
 
Abhra Kar
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No I didn't included <mvc:annotation-driven/>
But now i did that , but no effect .I am giving you the files ,please help me to find my mistake

dispatcher-servlet.xml ----

<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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
">
<!-- Enable annotation driven controllers, validation etc... -->
<mvc:annotation-driven />
<context:component-scan base-package="controller" />
<bean id="userValidator" class="validator.UserValidator" />
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="messages"/>
</bean>
<bean id="viewResolver"
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 class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
</beans>



bean class--

package model;
public class User{
private String firstName;
private String lastName;
public void setFirstName(String firstName){
this.firstName=firstName;
}
public String getFirstName(){
return firstName;
}
public void setLastName(String lastName){
this.lastName=lastName;
}
public String getLastName(){
return lastName;
}
}



Controller class---

package controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
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.bind.annotation.InitBinder;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.SessionAttributes;
import javax.validation.Valid;
import validator.UserValidator;
import model.User;
@Controller
@RequestMapping("/userRegistration.htm")
public class UserController {
@Autowired
private UserValidator userValidator;
@InitBinder
protected void initBinder(WebDataBinder binder) {
binder.setValidator(userValidator);
}
@RequestMapping(method = RequestMethod.GET)
public String showUserForm(ModelMap model)
{
User user = new User();
model.addAttribute(user);
return "index";
}
@RequestMapping(method = RequestMethod.POST)
public String onSubmit(@Valid User user) {
return "result";
}
}



validator class-----

package validator;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
import model.User;
public class UserValidator implements Validator{
@Override
public boolean supports(Class<?> clazz) {
return User.class.isAssignableFrom(clazz);
}
@Override
public void validate(Object target, Errors errors) {
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "firstName", "firstName.required","give proper value");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "lastName", "lastName.required","give proper value");
}
}



messages.properties in WEB-INF\classes ---

firstName.required = First Name is required
lastName.required = Last Name is required



jars set in classpath----

set classpath=.;D:\sjars\org.springframework.aop_3.0.5.RELEASE.jar;D:\sjars\org.springframework.asm_3.0.5.RELEASE.jar;D:\sjars\org.springframework.aspects_3.0.5.RELEASE.jar;D:\sjars\org.springframework.beans_3.0.5.RELEASE.jar;D:\sjars\org.springframework.context.support_3.0.5.RELEASE.jar;D:\sjars\org.springframework.context_3.0.5.RELEASE.jar;D:\sjars\org.springframework.core_3.0.5.RELEASE.jar;D:\sjars\org.springframework.expression_3.0.5.RELEASE.jar;D:\sjars\org.springframework.instrument.tomcat_3.0.5.RELEASE.jar;D:\sjars\org.springframework.instrument_3.0.5.RELEASE.jar;D:\sjars\org.springframework.jdbc_3.0.5.RELEASE.jar;D:\sjars\org.springframework.jms_3.0.5.RELEASE.jar;D:\sjars\org.springframework.orm_3.0.5.RELEASE.jar;D:\sjars\org.springframework.oxm_3.0.5.RELEASE.jar;D:\sjars\org.springframework.test_3.0.5.RELEASE.jar;D:\sjars\org.springframework.transaction_3.0.5.RELEASE.jar;D:\sjars\org.springframework.web.portlet_3.0.5.RELEASE.jar;D:\sjars\org.springframework.web.servlet_3.0.5.RELEASE.jar;D:\sjars\org.springframework.web.struts_3.0.5.RELEASE.jar;D:\sjars\org.springframework.web_3.0.5.RELEASE.jar;D:\sjars\commons-logging-api-1.1.1.jar;D:\sjars\servlet-api.jar;D:\sjars\jsp-api.jar






And link provide by you help a lot ,I have better idea now .
PLease help me here
THanks
 
Abhra Kar
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi please provide any suggestion
 
CAUTION! Do not touch the blades on your neck propeller while they are active. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic