As with many a new person to Spring MVC I am having difficulty getting over that first hurdle. In my case the current hurdle appears to be related to JSTL fmt taglib not formatting my dates properly or it is being passed a string literal by the value attribute of the fmt tag . I have a simple Maven project which I am using to work out the kinks in my knowledge. The following code snippets are from my WelcomeController, web-servlet.xml, web.xml and welcome.jsp.
WelcomeController.java
@Controller
@RequestMapping(value="/")
class WelcomeController {
@RequestMapping(method=RequestMethod.GET)
public String welcome(Model model) {
Date today = new Date();
System.out.println("Controller being called");
model.addAttribute("today", today);
System.out.println(model.containsAttribute("today"));
return "welcome";
}
}
web-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans> <!-- bean namespaces ommitted for space sake -->
<context:component-scan base-package="org.opel.eros.web"/>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<context:property-placeholder location="classpath:META-INF/properties/web-config.properties"/>
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="${config.prefix}"/>
<property name="suffix" value="${config.suffix}"></property>
</bean>
web.xml
<web-app>
<servlet>
<servlet-name>eros</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>eros</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
And finally welcome.jsp
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<html>
<head></head>
<body>
<fmt:formatDate value="${today}" pattern="yyyy-MM-dd"/>
</body>
</html>
Doesn’t get much simpler I know, but an exception is thrown when I enter the url http://localhost:9990/XYZ/, this is the exception: PWC6338: Cannot convert “${today}” for the attribute value of the bean java.util.Date
That seems to me to be an error where the formateDate is being passed the String literal “${today}”, which will obviously throw an exception. The example I am using to help (from Spring Reciepes: A problem Solution Approach) states that to access model attributes you use the syntax specified in the fmt tag above.
Basically I am looking for a solution and a reason why this is happening ( I do realise it is probably something really simple and stupid on my part =]). Thanks for any help in advance, much appreciated.
The problem seems to be inside the web.xml. The web.xml and corresponding project structure were auto generated by Spring Source tools using the maven-archtype-webapp. The auto generated web.xml doesn’t specify any namespace defenitions; Instead specifying a DOCTYPE similar to this:
I made the mistake of assuming that the auto generated code would use the version 3.0 definitions. A change to version 3 fixed the issues. One thing to note is that while the system was obviously using version 2.3 the output from calls to:
Produced a Servlet version of 3.0, which lead to some confusion on my part and may catch other people out. Big thanks to Bogdan for his help.