user1141946 user1141946 - 11 days ago 8
Java Question

ClassCastException when using formatDate

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?

Answer

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!