mercredi 11 janvier 2012

Changes on "Developing a Spring Framework MVC application step-by-step" using spring 3 and tomcat 7.x

I've seen a lot of questions over different forums of people following this spring tutorial.

This is a really nice tutorial if it's your first time using Spring MVC but  it was written for using Tomcat 5.x or 6.x and Spring 2.5

If you are using tomcat 7.x and Spring 3 you can still follow the tutorial but you need to do some small changes on the Basic Application and Environment Setup part:


  • In step 1.3 "Deploy the application to Tomcat"
    1. In the file springapp/build.xml, change org.apache.catalina.ant.InstallTask (now deprecated) by org.apache.catalina.ant.DeployTask and
    2. In the fileset where you include catalina-ant.jar include  include as well tomcat-coyote.jar and tomcat-util.jar like this:

      <fileset dir="${appserver.lib}">
               <include name="catalina-ant.jar"/>
               <include name="tomcat-coyote.jar"/>
               <include name="tomcat-util.jar"/>
      </fileset>
      
      
    3. In the file springapp/build.properties set your appserver home as
      appserver.home=${user.home}/apache-tomcat-7.0.23   
      and
    4. set the url of tomcat manager as
      tomcat.manager.url=http://localhost:8080/manager/text        
    5. In the tomcat-users.xml you need to use the roles provided in tomcat 7.x, you need at least the manager-script role, you can use the following xml:
      <tomcat-users>
          <role rolename="manager-script"/>
          <user username="tomcat" password="s3cret" roles="manager-script"/>
      </tomcat-users>
      

  • In step 1.7 "Copy libraries to 'WEB-INF/lib'",  just take into account that the  spring.jar is not longer provided by Spring 3. Instead of one single jar now spring has many of them, you can either choose to add just the ones you will use or just add all the jars to the  'war/WEB-INF/lib' directory. You will probably need to download the  commons-logging.jar from  here and add it to the lib directory as well. If you want, you can download this zip that contains the needed jars to follow the tutorial (therefore you don't need to download the jars described in the next two steps).
  • In step 1.9 "Write a test for the Controller" you  will probably need to download the  junit-4.10.jar from  here and add it to the lib directory.
  • In step 2.1. "Configure JSTL and add JSP header file" instead of  adding to your project the standard.jar and the  jstl.jar  download the jars that are here: http://jstl.java.net/download.html
  • In the step 4.5 "Adding a form" just take into account that both spring.tld and spring-form.tld can be found in the spring-webmvc-3.0.x.RELEASE.jar file.  To use them the JAR file must be in the classpath. Just add the following lines to the JSP files:
    <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
    
By doing this little config changes you should be able to follow the rest of the tutorial without any problems.