日期的Spring表单标记出现奇怪的错误

时间:2014-08-05 10:19:04

标签: spring forms jsp

我有

public class FormInputValidationTest {

private String email;
private Date dob;
private String doj;
private String desig;

和相关的getter / setter

和我的HomeController(只有一个控制器)是

@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>

我已经注释掉了表单:输入路径=&#34; dob&#34; 因为当我在我的页面中将其作为输入并以适当的值提交时 我总是得到400状态回复

     type Status report

     message

     description The request sent by the client was syntactically incorrect.

没有&#34; dob&#34;它可以正常工作作为输入 我一直在试图查看请求标题和正文但我仍然无法理解为什么这会给我一些问题。 所以我添加了doj(期望的日期),但声明为String进行测试,看起来很好

有人可以对此有所了解吗? 特别是因为这段代码(我没试过)似乎能够处理其关联模型中Date对象的输入。

PS:即使我使用modelAttribute =&#34; fivt&#34;它也是一样的在表单标签

1 个答案:

答案 0 :(得分:1)

每当提交表单时,其值都是String类型。现在当你写&#34; Date dob&#34; spring尝试做的是将String值赋给Date变量,这是不可能的。正确的方法是在日期变量上使用@DateTimeFormat注释告诉spring将传入的String值转换为相应的Date值。

参考this帖子