日期的Spring表单标记出现奇怪错误
我有 和相关的getter/setter 我的家庭控制器(只有一个控制器)是 最后,我的观点是日期的Spring表单标记出现奇怪错误,spring,forms,jsp,Spring,Forms,Jsp,我有 和相关的getter/setter 我的家庭控制器(只有一个控制器)是 最后,我的观点是 @RequestMapping(value = "/validation", method = RequestMethod.GET) public String new_validation(Locale locale, Model model) { FormInputValidationTest fivt = new FormInputValidationTest(); logger
@RequestMapping(value = "/validation", method = RequestMethod.GET)
public String new_validation(Locale locale, Model model) {
FormInputValidationTest fivt = new FormInputValidationTest();
logger.info("in validation for GET request");
model.addAttribute("fivt",fivt);
return "validation";
}
@RequestMapping(value = "/validation", method = RequestMethod.POST)
public String new_validation(@ModelAttribute("fivt") FormInputValidationTest fivt, Locale locale, Model model, HttpServletRequest req) {
logger.info("in validation for POST request");
logger.info("email received from user input is : {}",fivt.getEmail());
// logger.info("dob received from user input is : {}",fivt.getDob().toString());
logger.info("desig received from user input is : {}",fivt.getDesig());
logger.info("doj received from user input is : {}",fivt.getDoj());
model.addAttribute("message","successfully added");
return new_validation(locale, model);
}
没有“dob”作为输入,它工作正常
我一直在尝试查看请求头和请求体,但我仍然无法理解为什么这会给我带来一些问题。
所以我添加了doj(预期日期),但声明为要测试的字符串,这似乎很好
有人能解释一下吗?
特别是因为这段代码(我还没有尝试过)似乎能够处理关联模型中日期对象的输入
PS:即使我在表单标签中使用modeldattribute=“fivt”也一样,只要提交表单,它的值都是String类型的。现在,当您编写“Date dob”时,spring试图做的是将字符串值赋给日期变量,这是不可能的。正确的方法是使用@DateTimeFormat annotation over date变量告诉spring将传入的字符串值转换为相应的日期值
请参阅postPrabhat。。谢谢你的回复。。我已经能够在过去的工作中确保日期暂时是yyyy/mm/dd格式。。
@RequestMapping(value = "/validation", method = RequestMethod.GET)
public String new_validation(Locale locale, Model model) {
FormInputValidationTest fivt = new FormInputValidationTest();
logger.info("in validation for GET request");
model.addAttribute("fivt",fivt);
return "validation";
}
@RequestMapping(value = "/validation", method = RequestMethod.POST)
public String new_validation(@ModelAttribute("fivt") FormInputValidationTest fivt, Locale locale, Model model, HttpServletRequest req) {
logger.info("in validation for POST request");
logger.info("email received from user input is : {}",fivt.getEmail());
// logger.info("dob received from user input is : {}",fivt.getDob().toString());
logger.info("desig received from user input is : {}",fivt.getDesig());
logger.info("doj received from user input is : {}",fivt.getDoj());
model.addAttribute("message","successfully added");
return new_validation(locale, model);
}
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page session="false"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Search/View</title>
</head>
<body>
<h1></h1>
<!-- modelAttribute="fivt" -->
<form:form method="post" action="validation" commandName="fivt" accept-charset="utf-8">
<table>
<tr>
<td><form:label path="email">Email</form:label></td>
<td><form:input path="email" /></td>
</tr>
<!-- <tr> -->
<%-- <td><form:label path="dob">DB</form:label></td> --%>
<%-- <td><form:input path="dob" /></td> --%>
<!-- </tr> -->
<tr>
<td><form:label path="doj">DOJ</form:label></td>
<td><form:input path="doj" /></td>
</tr>
<tr>
<td><form:label path="desig">Desig</form:label></td>
<td><form:input path="desig" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Submit" /></td>
</tr>
</table>
</form:form>
</body>
</html>
type Status report
message
description The request sent by the client was syntactically incorrect.