【发布时间】:2017-05-08 01:28:16
【问题描述】:
我正在尝试使用 @Valid 和 @Size 注释来验证我的 JSP 页面字段长度,但它们不起作用。我知道我错过了什么吗?
请在下面找到我的代码。
Student.java(pojo 类)
package com.vijay.controllerCAMVC;
import java.util.ArrayList;
import java.util.Date;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import jdk.nashorn.internal.objects.annotations.SpecializedFunction;
public class Student {
@Size(min=2, max=20)
private String firstname;
// @Size(min=2, max=20, message='Size.student1.lastname')
**@Size(min=2, max=20)**
// , message="Please enter lastname length between {min} and {max}")
private String lastname;
private Date dateofbirth;
private ArrayList<String> courses;
private Address address;
public Date getDateofbirth() {
return dateofbirth;
}
public void setDateofbirth(Date string) {
this.dateofbirth = string;
}
public ArrayList<String> getCourses() {
return courses;
}
public void setCourses(ArrayList<String> courses) {
this.courses = courses;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
控制器类代码:
package com.vijay.controllerCAMVC;
import java.beans.PropertyEditor;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
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.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HelloWorld2 {
// avoiding binding of problem fields @ModelAttribute auto binding case, always in 1st position in controller
@InitBinder
public void initbinder( WebDataBinder webdatabinder){
webdatabinder.setDisallowedFields( new String[] {"courses"} );
// property editors
SimpleDateFormat df = new SimpleDateFormat("mm-dd-yyyy");
webdatabinder.registerCustomEditor(Date.class, "dateofbirth", new CustomDateEditor(df, false) );
// String editor for custom property editors defined by user specific i.e StringPropertyEditor.java
webdatabinder.registerCustomEditor(String.class, "firstname", new StringPropertyEditor() );
}
@SuppressWarnings({ "deprecation", "deprecation" })
@RequestMapping(value = "/Submissionform.html", params = "DELETE", method = RequestMethod.POST)
public ModelAndView modelview3(
/*
* @RequestParam(name = "firstname", required = false, defaultValue
* = "ENter Name") String firstname,
*
* @RequestParam(name = "lastname", required = false, defaultValue =
* "Default2") String lastname
*/
// @RequestParam Map<String, String> reqparams) {
// @Valid Before do data binding from JSP to model please apply validations using annotations
**@Valid** @ModelAttribute("student1") Student student1, BindingResult result) {
// similar to move corresponding using name
if (result.hasErrors()) {
ModelAndView modelandview = new ModelAndView("ApplicationForm");
return modelandview;
}
ModelAndView modelandview = new ModelAndView("DeleteForm");
// modelandview.addObject("msg", "Congratulation on Applicaiton
// Submission for");
// Student student1 = new Student();
// student1.setFirstname(reqparams.get("firstname"));
// student1.setLastname(reqparams.get("lastname"));
//
// DateFormat df = new SimpleDateFormat("dd/mm/yyy");
// Date dateofbirth = null;
//
// try {
// dateofbirth = (Date) df.parse(reqparams.get("dateofbirth"));
// } catch (ParseException e) {
// e.printStackTrace();
// }
// student1.setDateofbirth(dateofbirth);
// modelandview.addObject("student1", student1);
//
return modelandview;
}
// common attributes which gets auto added to all methods in controller &
// executes 1st
@ModelAttribute
public void addcommonmodelattributes(Model model1) {
model1.addAttribute("msg", "Congratulation on Applicaiton Submission for");
}
}
我的 JSP 文件:强文本
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%-- page language="java" contentType="text/html; charset=ISO-8859-1" --%>
<%-- pageEncoding="ISO-8859-1"%> --%>
<!-- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> -->
<html>
<body>
<h1>Student Application Form</h1>
<form:errors path="student1.*"/>
<form action="/SimpleSpringMVC2/Submissionform.html" method="post">
<table>
<tr>
<td> First Name : </td> <td> <input name="firstname" type="text" /> </td>
</tr>
<tr>
<td> Last Name : </td> <td> <input name="lastname" type="text" /> </td>
</tr>
<tr>
<td> Date of birth : </td> <td> <input name="dateofbirth" type="text" class="date" />
' </td>
</tr>
<tr>
<td> Courses : </td> <td> <select name="courses" multiple>
<option value="C">C</option>
<option value="C++">C++</option>
<option value="Java">Java</option>
</select> </td>
</tr>
</table>
<table>
<tr>
<td>City : </td> <td><input type="text" name="address.city"></td>
<td>State : </td> <td><input type="text" name="address.state"></td>
<td>PinCode : </td> <td><input type="text" name="address.picode"></td>
</tr>
</table>
<input type="submit" name="CREATE" value="CREATE" />
<input type="submit" name="MODIFY" value="MODIFY" />
<input type="submit" name="DELETE" value="DELETE" />
</form>
</body>
</html>
【问题讨论】:
-
它是怎么回事?
-
您实际上是在自己做这件事。您正在执行
new ModelAndView("ApplicationForm");,它基本上会破坏所有有关错误等的信息。该信息也必须是ModelAndView的一部分。而是使用new ModelAndView("ApplicationForm", result.getModel());。
标签: spring validation model-view-controller