Monday, 28 February 2011

A Fix to Our Spring-MVC Course

Just a quick blog post to alert that there is an error in one of the config files in the Spring-MVC course. I'm uploading a fixed version now, so if you're a user of the course, please download the new "Practicals and Code" folder, or follow these instructions:

If you start your project from the "Starting Code" folder, our web.xml contains the following:

   <!--  we've already configured Spring-MVC for you - nothing to do here! -->         
    <servlet>
       <servlet-name>FrontController</servlet-name>
       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
       <load-on-startup>1</load-on-startup>
    </servlet>
  
    <servlet-mapping>
       <servlet-name>FrontController</servlet-name>
       <url-pattern>*.do</url-pattern>
    </servlet-mapping>

However, our Spring XML config file is called "Dispatcher-servlet.xml".

By default, Spring finds it's config file in a web app by taking the servlet name and appending "-servlet.xml" to the end - it then looks for a matching file in /WEB-INF.

If you downloaded the course before the date of this post, please rename the servlet name to Dispatcher (it appears twice in web.xml).

So, your web.xml file should be (in full):

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5">
         
    <!--  we've already configured Spring-MVC for you - nothing to do here! -->         
    <servlet>
       <servlet-name>Dispatcher</servlet-name>
       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
       <load-on-startup>1</load-on-startup>
    </servlet>
  
    <servlet-mapping>
       <servlet-name>Dispatcher</servlet-name>
       <url-pattern>*.do</url-pattern>
    </servlet-mapping>

</web-app>


Many apologies for the inconvenience this has caused - it was entirely my fault and happened because I changed my naming convention between chapters.

Thanks to David Wubshet and Benjamin Quarless amongst others for flagging up this problem.

Thursday, 17 February 2011

Integrating Spring MVC and a "Real" Database

On the Spring MVC and WebFlow video training course, we build a system from scratch based around a book store. To keep things focussed on the web tier, we use a mock implementation of a service class.

Therefore, we can run the system up without worrying about setting up a database, getting the tables set up etc.

However, I've had a few requests from people who are stuck on "upgrading" their Spring MVC project that uses the Mock. They want to talk to a back end database but for some reason it isn't working.

The best way to work is to start from your Spring-MVC project, and make sure that is working. As on the course, you'll probably have a Dispatcher-servlet.xml file with the web-tier beans, and a separate application.xml containing the "middle-tier" beans.

Now write a production version of your service class, in the same style as on the fundamentals course (for example, service class calls a dao which uses a Spring template to read/write to the db).

As all of the above is covered in both courses, it's just a case of integrating your knowledge.

There are a couple of steps you might need to follow however:

1: Add in missing Jar files

Add in any JAR files that you need for your data access strategy. I used Hibernate 3 (the old XML mapping style, not annotations), and I added:

antlr-2.7.6.jar
dom4j-1.6.1.jar
hibernate3.jar
hsqldb.jar
javassist.jar
jta.jar

If you're using Hibernate with Annotations, or MyBatis or whatever, your dependencies will be different. I simply borrowed these JARs from the project I created on the Spring Fundamentals course.

Step 2: Ensure your mapping files are copied across (Hibernate XML only)

Now for an annoying complication: I'm using the old fashioned but still usable XML mapping approach to Hibernate. I have a file called Book.hbm.xml in my package "com.virtualpairprogrammers.domain".

However, this file is not being picked up by the build script we used on the Spring MVC course. When Eclipse compiles, it copies any non-Java files from the /src folder structure, across to the corresponding folder under /bin.

The ant compile task does NOT do this, however. So I need to manually add this step into my build.xml.

<!-- ensure hibernate XML files are also moved in the compilation -->
<copy todir="${bin}">
   <fileset dir="${src}">
      <include name="**/*.hbm.xml"/>
   </fileset>
</copy>
Note: this is added inside the compile target.

I've uploaded a full sample project to http://vpp-misc-files.s3.amazonaws.com/MVC%20Project.zip. These sample files will only be available for a short period, so grab them while you can.

Tuesday, 15 February 2011

JavaEE 6 with Glassfish is Nearly There!

It's good to report that we're approaching the final stages of recording the JavaEE course. Just two more scripts to write and a few more recording sessions and we're there.

The running order right now looks like this (and it shouldn't change too much in post production):

1: Getting Started with Glassfish
2: Writing an EJB Session Bean
3: JNDI and Calling Remote Session Beans
4: More on Session Beans
5: The Java Persistence API: Mapping Classes
6: JPA2: Updates and Deletes
7: JPA: Relationships
8: JPA2: Queries using JPQL and The JPA2 Criteria API
9: JPA in a Managed Environment: the PersistenceContext
10: Declarative Transactions
11: JSF fundamentals
12: JSF Component Libraries (including Prime Faces)
13: Security in JavaEE
14: Webservices and REST
15: Messaging and Message Driven Beans

This is probably going to turn out to be our biggest course yet.

I decided to cover just one application server, Glassfish, as to cover more than that would confuse and overload the course. But everything on the course should work just fine on other app servers. We may release variants for other servers if there's a demand.

The estimated release time will be early March. The only block in the way is that writing the Webservices and REST chapter is proving very difficult as Webservices is a big topic that is orthogonal to Java. As always, I'll be aiming to make the material understandable to all, so I am having to do a lot of working covering the basics of SOAP, WSDLs, REST and the like.

But it will be worth it! My next post will probably be announcing the course release, so watch this space!