使用 formatDate 时出现 ClassCastException
我无法弄清楚为什么我的 formatDate 无法正常工作。这是我的代码:
Java
@DateTimeFormat(style = "SS")
@Column(name="my_date")
private Date myDate;
public Date getMyDate() {
return this.myDate;
}
public void setMyDate(Date myDate) {
this.myDate = myDate;
}
JSP - 使用 fmt:formatDate 的代码部分 代码:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<c:forEach var="foo" items="${fooList}" varStatus="i">
<tr>
<td>${i.index+1}</td>
<td>${foo.id}</td>
<td>${foo.name}</td>
<td><fmt:formatDate value="${foo.myDate}" pattern="MM/dd/yyyy"/></td>
</tr>
Web.xml
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<page-encoding>UTF-8</page-encoding>
</jsp-property-group>
</jsp-config>
这是控制器的修改版本,处理日期的所有部分都显示在此处。
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public String list(@PathVariable("id") String id, Model uiModel) {
List<foo> fooList = createList(fooList);
uiModel.addAttribute("fooList", fooList);
return VIEW_OBJECT;
}
运行我的应用程序时,我收到此错误:
java.lang.ClassCastException: org.springframework.web.servlet.support.JstlUtils$SpringLocalizationContext incompatible with java.lang.String
***Error 仅在 formateDate 代码位于其中时显示,否则所有 EL 评估正常。
有人知道为什么会发生这种情况吗?
I'm having trouble trying to figure out why my formatDate is not working correctly. Here is my code:
Java
@DateTimeFormat(style = "SS")
@Column(name="my_date")
private Date myDate;
public Date getMyDate() {
return this.myDate;
}
public void setMyDate(Date myDate) {
this.myDate = myDate;
}
JSP - portion of the code that uses the fmt:formatDate code:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<c:forEach var="foo" items="${fooList}" varStatus="i">
<tr>
<td>${i.index+1}</td>
<td>${foo.id}</td>
<td>${foo.name}</td>
<td><fmt:formatDate value="${foo.myDate}" pattern="MM/dd/yyyy"/></td>
</tr>
Web.xml
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<page-encoding>UTF-8</page-encoding>
</jsp-property-group>
</jsp-config>
Here is a modified version of the controller, all parts dealing with the date are shown here.
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public String list(@PathVariable("id") String id, Model uiModel) {
List<foo> fooList = createList(fooList);
uiModel.addAttribute("fooList", fooList);
return VIEW_OBJECT;
}
When running my application I get this error:
java.lang.ClassCastException: org.springframework.web.servlet.support.JstlUtils$SpringLocalizationContext incompatible with java.lang.String
***Error only shows when formateDate code is in there, otherwise all EL evaluate fine.
Anyone know why this may be happening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
找到了为什么我的 fmt 标签不起作用的解决方案。我需要将我的依赖关系从: 更改
为:
感谢您的建议!
Found the solution as to why my fmt tag was not working. I needed to change my dependency from:
To:
Thanks for the suggestions!
我无法更改依赖项,因此我的解决方案是使用 scriptlet。我没有解决 fmt:formatDate,但它可以用作解决方法:
I could not change the dependencies, so my solution was to use a scriptlet. I does not solve the fmt:formatDate, but it can be used as workarround: