我在我的应用程序中使用jQuery datepicker作为文本字段。我能够以(dd-MM-yy)格式正确插入数据。但问题是将日期以不同的格式(带有时区的日期和时间)返回到验证哪个文本字段会导致异常。
Student.java
public class Student {
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 {
private final static Logger logger=Logger.getLogger(StudentController.class);
@Autowired
private StudentService studentService;
@Autowired
private StudentValidator studentValidator;
@RequestMapping("/form")
public ModelAndView getForm(@ModelAttribute Student student,Model model){
initModelList(model);
logger.info("This is a message");
return new ModelAndView("form");
}
@RequestMapping("/register")
public String addStudent(@ModelAttribute("student") Student student, Model model, BindingResult bindingResult,SessionStatus status){
studentService.addStudent(student);
initModelList(model);
studentValidator.validate(student, bindingResult);
if(bindingResult.hasErrors()){
return "form";
}else{
logger.info("Student added successfully...");
status.setComplete();
return "redirect:list";
}
}
@InitBinder
protected void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yy");
dateFormat.setLenient(true);
binder.registerCustomEditor(Date.class, new CustomDateEditor(
dateFormat, false));
}
private void initModelList(Model model){
List<String> standardList=new ArrayList<String>();
standardList.add("--Select--");
standardList.add("I");
standardList.add("II");
standardList.add("III");
standardList.add("IV");
standardList.add("V");
standardList.add("VI");
standardList.add("VII");
standardList.add("VIII");
standardList.add("IX");
standardList.add("X");
model.addAttribute("standard",standardList);
List<String> mediumList=new ArrayList<String>();
mediumList.add("Telugu");
mediumList.add("English");
model.addAttribute("medium",mediumList);
List<String> sectionList =new ArrayList<String>();
sectionList.add("--Select--");
sectionList.add("A");
sectionList.add("B");
sectionList.add("C");
sectionList.add("D");
sectionList.add("E");
model.addAttribute("section", sectionList);
List<String> cityList=new ArrayList<String>();
cityList.add("--Select--");
cityList.add("Bangalore");
cityList.add("Hyderabad");
cityList.add("Chennai");
cityList.add("Mumbai");
model.addAttribute("city", cityList);
List<String> genderList =new ArrayList<String>();
//genderList.add("--Select--");
genderList.add("Male");
genderList.add("Female");
}
@SuppressWarnings("rawtypes")
@RequestMapping("/list")
public ModelAndView listStudents(){
List studentsList=studentService.listStudents();
return new ModelAndView("/list","studentsList",studentsList);
}
@RequestMapping("/delete")
public ModelAndView deleteStudent(@RequestParam Integer sid){
studentService.deleteStudent(sid);
return new ModelAndView("redirect:list");
}
@RequestMapping("/edit")
public ModelAndView editStudent(@RequestParam Integer sid){
Student studentObject=studentService.getStudent(sid);
return new ModelAndView("edit","studentObject",studentObject);
}
@RequestMapping("/update")
public ModelAndView updateSudent(@ModelAttribute Student student){
studentService.updateStudent(student);
return new ModelAndView("redirect:list");
}
}
<%@ 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>
请解决我的问题,提前致谢..
答案 0 :(得分:1)
我认为您必须在控制器中添加以下initbinder,它将转换为所需的格式。
@InitBinder
public void initBinder(WebDataBinder binder) {
CustomDateEditor editor = new CustomDateEditor(new SimpleDateFormat("dd-MMMM-yy"), true);
binder.registerCustomEditor(Date.class, editor);
}
还在Student class中使用日期字段的注释。
@DateTimeFormat(pattern = "dd-MMMM-yy")
private Date dateOfBirth;
@DateTimeFormat(pattern = "dd-MMMM-yy")
private Date dateOfJoining;
答案 1 :(得分:0)
@DateTimeFormat(pattern = "dd-MMMM-yy")
private Date dateOfBirth;
@DateTimeFormat(pattern = "dd-MMMM-yy")
private Date dateOfJoining;
这些线解决了我的问题......谢谢Mr. RE350