我是Spring的新手,试图在Spring中构建一个包含Date作为表单元素的表单示例。但是当点击提交按钮时,它显示错误“HTTP状态400-客户端发送的请求在语法上不正确。”。带Date日期的Spring MVC输入标签引发错误:HTTP状态400 - 客户端发送的请求在语法上不正确
注意:我使用日期元素的sql.Date。
请帮忙。
代码段如下所示
FormExample.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Spring Form Example</title>
</head>
<body>
<h2>Form Example</h2>
<form:form commandName="formExample" action="formExampleDetails" method="post">
<table>
<tr>
<td>
<label>BirthDate:</label>
</td>
<td>
<fmt:formatDate value="${birthDate.date}" pattern="dd-mmm-yyyy" var="birthDate"/>
<form:input path="birthDate" value="${birthDate}"/>
</td>
</tr>
<tr>
<td>
<input type="submit" value="Submit"/>
</td>
<td>
<input type="reset" value="Reset">
</td>
</tr>
</table>
</form:form>
</body>
</html>
Controller类即ApplicationControllerClass是如下
@Controller
public class ApplicationController {
@RequestMapping("/formExample")
public String formExamplePage(ModelMap model){
model.put("formExample", new FormExample());
return "formExamplePage";
}
@RequestMapping(value="/formExampleDetails",method=RequestMethod.POST)
public String formExampleDetails(@ModelAttribute FormExample formExample,ModelMap model){
System.out.println("BirthDate====>"+formExample.getBirthDate());
return "index";
}
@InitBinder
public void initBinder(WebDataBinder binder){
SimpleDateFormat sdf = new SimpleDateFormat("dd-mmm-yyyy");
sdf.setLenient(true);
binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
}
}
POJO类即FormExample.java是如下
package com.spring.pojo;
import java.io.Serializable;
import java.sql.Date;
public class FormExample implements Serializable{
private static final long serialVersionUID = 5527691555730303451L;
private Date birthDate;
public Date getBirthDate() {
return birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
}
尝试将'name'参数添加到输入中,像这样
–@Oleg Kubatov-我试着用你的解决方案。但它仍然在抛出错误。注意 - 我已经从控制器类中删除了“initBinder”方法。我正在使用SQL Date作为生日类型,所以它引发错误。如何克服这一点? – NagenSahu