Reputation: 577
Hi i am trying to put validation in my spring project but it is not working for me i also add all the required validator jar in my lib folder.
My Controller Class is looking like
package qm;
import java.sql.Date;
import java.text.SimpleDateFormat;
import javax.validation.Valid;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
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.servlet.ModelAndView;
@Controller
public class StudentAdmissionController {
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.setDisallowedFields(new String[] {"studentSkill","studentDOB"});
SimpleDateFormat dateFormat=new SimpleDateFormat("dd-mm-yyyy");
binder.registerCustomEditor(Date.class,"studentDOB", new CustomDateEditor(dateFormat, false));
}
@RequestMapping(value="/registration.html",method=RequestMethod.GET)
public ModelAndView getStudentAdmissionForm() {
ModelAndView model=new ModelAndView("AdmissionForm");
return model;
}
@ModelAttribute
public void addCommonObject(Model model) {
model.addAttribute("headerMessage","Q-Manager.com");
}
@RequestMapping(value="/submit-detail.html",method=RequestMethod.POST)
public ModelAndView studentDetail(@Valid @ModelAttribute("student1") Student student,BindingResult result) {
if(result.hasErrors()) {
ModelAndView model=new ModelAndView("AdmissionForm");
return model;
}
ModelAndView model=new ModelAndView("AdmissionSuccess");
return model;
}
}
my Student Class is
package qm;
import java.util.ArrayList;
import java.util.Date;
import javax.validation.constraints.Size;
public class Student {
@Size(min=2,max=30)
private String studentName;
private String studentHobby;
private Long studentMobile;
private Date studentDOB;
private ArrayList<String> studentSkill;
private Address studentAddress;
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getStudentHobby() {
return studentHobby;
}
public void setStudentHobby(String studentHobby) {
this.studentHobby = studentHobby;
}
public Long getStudentMobile() {
return studentMobile;
}
public void setStudentMobile(Long studentMobile) {
this.studentMobile = studentMobile;
}
public Date getStudentDOB() {
return studentDOB;
}
public void setStudentDOB(Date studentDOB) {
this.studentDOB = studentDOB;
}
public ArrayList<String> getStudentSkill() {
return studentSkill;
}
public void setStudentSkill(ArrayList<String> studentSkill) {
this.studentSkill = studentSkill;
}
public Address getStudentAddress() {
return studentAddress;
}
public void setStudentAddress(Address studentAddress) {
this.studentAddress = studentAddress;
}
}
my Address Class is
package qm;
public class Address {
private String country,city,street,pincode;
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getPincode() {
return pincode;
}
public void setPincode(String pincode) {
this.pincode = pincode;
}
}
and Here is my AdmissionForm.jsp
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<html>
<head>
<title>Student Form</title>
</head>
<body>
<center><h4>${headerMessage}</h4></center>
<center><h4>Student Admission Form</h4></center>
<center>
<form:errors path="student1.*"/>
</center>
<form action="submit-detail.html" method="post">
<table>
<tr>
<td>Student Name</td>
<td><input type="text" name="studentName"/></td>
</tr>
<tr>
<td>Hobby</td>
<td><input type="text" name="studentHobby"/></td>
</tr>
<tr>
<td>Student Mobile</td>
<td><input type="text" name="studentMobile"/></td>
</tr>
<tr>
<td>DOB</td>
<td><input type="text" name="studentDOB" value="May 04 09:51:52 CDT 2009"/></td>
</tr>
<tr>
<td>Skills</td>
<td>
<select name="studentSkill" multiple>
<option value="Core java">Core java</option>
<option value="Spring Core">Spring Core</option>
<option value="Spring MVC">Spring MVC</option>
</select>
</td>
</tr>
</table>
<table>
<tr><td>Student Address</td></tr>
<tr><td>Country<input type="text" name="studentAddress.country"/></td></tr>
<tr><td>City<input type="text" name="studentAddress.city"/></td></tr>
<tr><td>Street<input type="text" name="studentAddress.street"/></td></tr>
<tr><td>Pin Code<input type="text" name="studentAddress.pincode"/></td></tr>
<tr>
<td><input type="submit" value="Submit"/></td>
</tr>
</table>
</form>
</body>
</html>
anyone suggest me the where i am wrong so i will correct my code actually in my previous research i found that most of the people suggest regarding the jars ie need so i define all the required jars for validation in spring.
I used Spring 4.3.* jars for this project
Upvotes: 2
Views: 4631
Reputation: 11
Try by adding below dependency in your pom.xml
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.12.Final</version>
</dependency>
Upvotes: 1
Reputation: 1
you need to add mvc annotation tag in spring dispatcher.
<mvc:annotation-driven/>
Upvotes: 0