利用SpringMVC3框架验证@DateTimeFormat注解时,遇到一个奇的错,只要表单中的日期字段一输入值则就会如下错:
让人很莫名其妙。查了许久,也试了不少网上说的方法,比如有的说缺少joda-time-2.3.jar包的引用(后来证实不是这个原因)。
JAVA代码如下片段如下:
@Resource public class Employee { private String id; private String lastName; private int gende; private String email; @DateTimeFormat(pattern="yyyy-MM-dd") private Date birth; private Department department;
<form:form action="${pageContext.request.contextPath }/emp" method="POST" modelAttribute="employee"> <c:if test="${empty employee.id }"> LastName:<form:input path="lastName" value="" /> </c:if> <c:if test="${!empty employee.id}"> <form:input type="hidden" path="id"></form:input> <input type="hidden" name="_method" value="PUT"></input> </c:if> <br> <% HashMap<String,String> gendes = new HashMap<String,String>(); gendes.put("0","female"); gendes.put("1","male"); request.setAttribute("gendes", gendes); %> Gender:<br> <form:radiobuttons path="gende" items="${gendes }" delimiter="<br>" /> <br> Email:<form:input path="email" /> <br> Department:<form:select path="department.depId" items="${departments}" itemLabel="depName" itemValue="depId"></form:select> <br> Birth:<form:input path="birth"/> <br> <input type="submit" value="Submit"></input> </form:form>
<mvc:annotation-driven></mvc:annotation-driven>
从以上看过来,确实无法发现有什么问题,后来再写个更简单的测试时发现,原来是JAVA代码中的Date类型Eclipse默认引用了java.sql.Date的类,而不是我们想引入的java.util.Date。更正后马上可以的了。
网上查找此问题时发现有不少同朋友也遇到,有可能也是这个问题,希望对大家有帮助。
Spring MVC "The request sent by the client was syntactically incorrect ()"解决办法 .
原文地址:http://blog.csdn.net/posonrick/article/details/45291269