我在我的應用程序中使用jQuery datepicker作爲文本字段。我能夠以(dd-MM-yy)格式正確插入數據。但問題是獲取日期以不同的格式(帶時區的日期和時間)返回到文本字段,同時驗證哪些會導致異常。從不同的格式獲取日期,而從春天的日期驗證MVC
Student.java
public class Student {
\t private final static Logger logger=Logger.getLogger(Student.class);
private Integer sid;
private String firstName;
private String middleName;
private String lastName;
private String fatherName;
private String motherName;
private Date dateOfBirth;
private Integer admissionNo;
private Date dateOfJoining;
private String standard;
private String section;
private String medium;
private Address address;
StudentController.java
@Controller
@RequestMapping("/student")
public class StudentController {
\t
\t private final static Logger logger=Logger.getLogger(StudentController.class);
\t
\t @Autowired
\t private StudentService studentService;
\t
\t @Autowired
\t private StudentValidator studentValidator;
\t
\t @RequestMapping("/form")
\t public ModelAndView getForm(@ModelAttribute Student student,Model model){
\t \t initModelList(model);
\t \t
\t \t logger.info("This is a message");
\t return new ModelAndView("form");
\t }
\t
\t @RequestMapping("/register")
\t public String addStudent(@ModelAttribute("student") Student student, Model model, BindingResult bindingResult,SessionStatus status){
\t \t
\t \t studentService.addStudent(student);
\t \t initModelList(model);
\t \t studentValidator.validate(student, bindingResult);
\t \t if(bindingResult.hasErrors()){
\t \t \t return "form";
\t \t }else{
\t \t logger.info("Student added successfully...");
\t \t status.setComplete();
\t \t return "redirect:list";
\t \t }
\t }
\t
\t @InitBinder
\t protected void initBinder(WebDataBinder binder) {
\t SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yy");
\t dateFormat.setLenient(true);
\t binder.registerCustomEditor(Date.class, new CustomDateEditor(
\t dateFormat, false));
\t }
\t
\t private void initModelList(Model model){
List<String> standardList=new ArrayList<String>();
\t standardList.add("--Select--");
\t \t standardList.add("I");
\t \t standardList.add("II");
\t \t standardList.add("III");
\t \t standardList.add("IV");
\t \t standardList.add("V");
\t \t standardList.add("VI");
\t \t standardList.add("VII");
\t \t standardList.add("VIII");
\t \t standardList.add("IX");
\t \t standardList.add("X");
\t \t model.addAttribute("standard",standardList);
\t \t
\t \t List<String> mediumList=new ArrayList<String>();
\t \t
\t \t mediumList.add("Telugu");
\t \t mediumList.add("English");
\t \t model.addAttribute("medium",mediumList);
\t \t
\t \t List<String> sectionList =new ArrayList<String>();
\t \t sectionList.add("--Select--");
\t \t sectionList.add("A");
\t \t sectionList.add("B");
\t \t sectionList.add("C");
\t \t sectionList.add("D");
\t \t sectionList.add("E");
\t \t model.addAttribute("section", sectionList);
\t \t
\t \t List<String> cityList=new ArrayList<String>();
\t \t cityList.add("--Select--");
\t \t cityList.add("Bangalore");
\t \t cityList.add("Hyderabad");
\t \t cityList.add("Chennai");
\t \t cityList.add("Mumbai");
\t \t model.addAttribute("city", cityList);
\t \t
\t \t List<String> genderList =new ArrayList<String>();
\t \t //genderList.add("--Select--");
\t \t genderList.add("Male");
\t \t genderList.add("Female");
\t }
\t
\t @SuppressWarnings("rawtypes")
\t @RequestMapping("/list")
\t public ModelAndView listStudents(){
\t \t List studentsList=studentService.listStudents();
\t \t return new ModelAndView("/list","studentsList",studentsList);
\t }
\t
\t @RequestMapping("/delete")
\t public ModelAndView deleteStudent(@RequestParam Integer sid){
\t \t studentService.deleteStudent(sid);
\t \t return new ModelAndView("redirect:list");
\t }
\t
\t @RequestMapping("/edit")
\t public ModelAndView editStudent(@RequestParam Integer sid){
\t \t Student studentObject=studentService.getStudent(sid);
\t \t return new ModelAndView("edit","studentObject",studentObject);
\t }
\t
\t @RequestMapping("/update")
\t public ModelAndView updateSudent(@ModelAttribute Student student){
\t \t studentService.updateStudent(student);
\t \t return new ModelAndView("redirect:list");
\t }
\t
}
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title>School Management System</title>
<script type="text/javascript" src="${pageContext.servletContext.contextPath}/resources/js/hello.js"></script>
<script type="text/javascript" src="${pageContext.servletContext.contextPath}/resources/js/sms_validation.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/themes/smoothness/jquery-ui.css" />
<link rel="stylesheet" type="text/css" href="${pageContext.servletContext.contextPath}/resources/css/styles.css"/>
<script>
$(function() {
$(".datepicker").datepicker({
changeMonth: true,
changeYear: true,
dateFormat: "dd-MM-yy"
});
});
</script>
</head>
<body>
<div id="container">
<div id="header">
</div>
<div id="content">
<div class="content_heading">Student Registration Form</div>
<p>This is a paragraph</p>
<button type="button" onclick="myFunction()">Date</button>
<p id="demo"></p>
<form:form id="registerForm" modelAttribute="student" method="post" action="register" onsubmit="return validateForm()">
<fieldset>
<legend>Student Details:</legend>
<table class="form_table">
<tr><td><form:label path="sid">Studet Id</form:label></td>
<td><form:input path="sid" placeholder="Enter Student Id"/></td>
<td><form:errors path="sid" cssClass="error"/></td>
</tr>
<tr>
<td><form:label path="firstName">First Name</form:label></td>
<td><form:input path="firstName"/></td>
<td><form:errors path="firstName" cssClass="error"/></td>
</tr>
<tr><td><form:label path="lastName">Last Name</form:label></td>
<td><form:input path="lastName"/></td>
<td><form:errors path="lastName" cssClass="error"/></td>
</tr>
<tr>
<td><form:label path="middleName">Middle Name</form:label></td>
<td><form:input path="middleName"/></td>
<td><form:errors path="middleName" cssClass="error"/></td>
</tr>
<tr><td><form:label path="fatherName">Father Name</form:label></td>
<td><form:input path="fatherName"/></td>
<td><form:errors path="fatherName" cssClass="error"/></td>
</tr>
<tr>
<td><form:label path="motherName">Mother Name</form:label></td>
<td><form:input path="motherName"/></td>
<td><form:errors path="motherName" cssClass="error"/></td>
</tr>
<tr><td><form:label path="dateOfBirth">Date Of birth</form:label></td>
<td><form:input path="dateOfBirth" class="datepicker"/></td>
<td><form:errors path="dateOfBirth" cssClass="error"/></td>
</tr>
<tr>
<td><form:label path="admissionNo">Admission No</form:label></td>
<td><form:input path="admissionNo"/></td>
<td><form:errors path="admissionNo" cssClass="error"/></td>
</tr>
<tr><td><form:label path="dateOfJoining">DateOfJoining</form:label></td>
<td><form:input path="dateOfJoining" class="datepicker"/></td>
<td><form:errors path="dateOfJoining" cssClass="error"/></td>
</tr>
<tr>
<td><form:label path="standard">Standard</form:label></td>
<td><form:select path="standard" >
<form:options items="${standard}"/>
</form:select></td>
<td><form:errors path="standard" cssClass="error"/></td>
</tr>
<tr><td><form:label path="section">Section</form:label></td>
<td><form:select path="section" items="${section}"/></td>
<td><form:errors path="section" cssClass="error"/></td>
</tr>
<tr>
<td><form:label path="medium">Medium</form:label></td>
<td><form:radiobuttons path="medium" items="${medium}"/></td>
<td><form:errors path="medium" cssClass="error"/></td>
</tr>
</table>
</fieldset>
<fieldset>
<legend>Addres Details:</legend>
<table class="form_table">
<tr><td><form:label path="address.doorNo">DoorNo</form:label></td>
<td><form:input path="address.doorNo"/></td>
<td><form:errors path="address.doorNo" cssClass="error"/></td>
</tr>
<tr>
<td><form:label path="address.streetName">StreetName</form:label></td>
<td><form:input path="address.streetName"/></td>
<td><form:errors path="address.streetName" cssClass="error"/></td>
</tr>
<tr><td><form:label path="address.addressLine">AddressLine</form:label></td>
<td><form:input path="address.addressLine"/></td>
<td><form:errors path="address.addressLine" cssClass="error"/></td>
</tr>
<tr>
<td><form:label path="address.city">City</form:label></td>
<td><form:select path="address.city" items="${city}"/></td>
<td><form:errors path="address.city" cssClass="error"/></td>
</tr>
<tr><td><form:label path="address.district">District</form:label></td>
<td><form:input path="address.district"/></td>
<td><form:errors path="address.district" cssClass="error"/></td>
</tr>
<tr>
<td><form:label path="address.state">State</form:label></td>
<td><form:input path="address.state"/></td>
<td><form:errors path="address.state" cssClass="error"/></td>
</tr>
<tr><td><form:label path="address.pin">Pin</form:label></td>
<td><form:input path="address.pin"/></td>
<td><form:errors path="address.pin" cssClass="error"/></td>
</tr>
<tr>
<td><form:label path="address.phone">Phone</form:label></td>
<td><form:input path="address.phone"/></td>
<td><form:errors path="address.phone" cssClass="error"/></td>
</tr>
<tr><td><form:label path="address.email">Email</form:label></td>
<td><form:input path="address.email"/></td>
<td><form:errors path="address.email" cssClass="error"/></td>
</tr>
<tr>
<td><form:label path="address.country">Country</form:label></td>
<td><form:input path="address.country"/></td>
<td><form:errors path="address.country" cssClass="error"/></td>
</tr>
<tr><td></td>
<td><input type="submit" value="Save"/></td><td></td>
</tr>
</table>
</fieldset>
</form:form>
</div>
<div id="footer"></div>
</div>
</body>
</html>
請解決我的問題,在此先感謝..
但問題是diiferent格式(日期和時間與時區)獲得追溯到在驗證哪一個會導致異常的文本字段,我不明白你的意思,你能告訴我們你在Spring層或數據庫中接收到什麼值。還有一件事,我想知道的是異常進入你的JSP頁面? – RE350 2014-11-15 08:01:38
感謝您的回覆。我的輸入格式爲04-nov-2014。提交表單後。當顯示驗證消息時,返回到日期字段的數據格式爲2014年11月4日星期二00:00:00 IST 2014.如果我沒有得到任何數據,我能夠以04-nov-2014的格式將數據插入到數據庫中驗證消息..這裏的問題是,日期格式正在更改,只要我得到驗證messages.If我提交表單與更改格式日期..我得到的休耕異常 – 2014-11-15 15:54:44
BindException:org.springframework.validation.BeanPropertyBindingResult:2錯誤字段'dateOfBirth'的對象'學生'中的字段錯誤:被拒絕的值[Tue Nov 04 00:00:00 IST 2014];代碼[typeMismatch.student.dateOfBirth,typeMismatch.dateOfBirth,typeMismatch.java.util.Date,typeMismatch];參數[org.springframework.context.support.DefaultMessageSourceResolvable:codes [student.dateOfBirth,dateOfBirth];參數[];預設信息[dateOfBirth]];默認消息[無法將屬性'java.lang.String'的屬性值轉換爲屬性'dateOfBirth'所需的類型'java.util.Date'; – 2014-11-15 15:59:33