1
votes

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.

2

2 Answers

2
votes

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:

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" >

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:

-application.getMajorVersion(); and
-application.getMinorVersion();

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.

1
votes

My guess is you have a mismatch between the JSP version and JSTL version you are using. Here is an article describing how to use JSTL depending on the setup you have: How to Reference and Use JSTL in your Web Application.

Your <web-app> tag in web.xml says nothing about the version of your application, so this is the first thing I would check. You can write some code to find out your application version (each servlet version should have its default if you don't specify it explicitly in web.xml), or do a quick and dirty test to see if you are using JSP 2 by changing your JSP to this:

<html>
   <head></head>
   <body>
       ${today}
   </body>
</html>

If you see the date value, you are on JSP 2; if you see the string ${today}, you are on JSP 1 (with the wrong JSTL version).