使用 formatDate 时出现 ClassCastException

发布于 2024-12-25 22:33:53 字数 1878 浏览 1 评论 0原文

我无法弄清楚为什么我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只是一片海 2025-01-01 22:33:53

找到了为什么我的 fmt 标签不起作用的解决方案。我需要将我的依赖关系从: 更改

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.1.2</version>
</dependency> 

为:

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.1.2</version>
    <scope>provided</scope>
</dependency>

感谢您的建议!

Found the solution as to why my fmt tag was not working. I needed to change my dependency from:

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.1.2</version>
</dependency> 

To:

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.1.2</version>
    <scope>provided</scope>
</dependency>

Thanks for the suggestions!

海之角 2025-01-01 22:33:53

我无法更改依赖项,因此我的解决方案是使用 scriptlet。我没有解决 fmt:formatDate,但它可以用作解决方法:

<c:forEach var="foo" items="${fooList}" varStatus="i">
<tr>
<td>${i.index+1}</td>
<td>${foo.id}</td>
<td>${foo.name}</td>

<%--Scriptlet alternative: %>
<c:set var="fooMyDate" value="${foo.myDate}" scope="request"/>
<%
    Object myDate = request.getAttribute("fooMyDate");
    java.text.DateFormat df = new java.text.SimpleDateFormat("MM/dd/yyyy");
%>
<td><%= df.format(myDate)%></td>

</tr>

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:

<c:forEach var="foo" items="${fooList}" varStatus="i">
<tr>
<td>${i.index+1}</td>
<td>${foo.id}</td>
<td>${foo.name}</td>

<%--Scriptlet alternative: %>
<c:set var="fooMyDate" value="${foo.myDate}" scope="request"/>
<%
    Object myDate = request.getAttribute("fooMyDate");
    java.text.DateFormat df = new java.text.SimpleDateFormat("MM/dd/yyyy");
%>
<td><%= df.format(myDate)%></td>

</tr>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文