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");
}
}
<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>
@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";
}
}
<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" />
-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
geronimo-validation_1.0_spec-1.0-CR5.jar
-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
<?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>
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;
}
}
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";
}
}
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");
}
}
firstName.required = First Name is required
lastName.required = Last Name is required
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
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
|