Hello World Web App using Spring Annotations Part 6

In part 6, the final part of this series, we’ll create the View to display our message to the end-user.  We’ll also run the whole app under Tomcat and Sysdeo, and even show a bit of Java class hot-swapping.  Lastly, we will create a WAR file from our project.

If you have missed the previous parts of this series, you can find them here: part 1, part 2, part 3, part 4, part 5.  You’ll need to setup your environment from parts 1-4 if you want to follow along with the examples here.

You can download the full source of this project from http://technologicaloddity.com/code/general/HelloWebApp.zip.

Creating the View

In part 5, we created a @Controller that would add our message to the Model and return the View "index".  We need to create that view.  Remember that our dispatch-servlet.xml file says that views are located in /WEB-INF/jsp, and that they end with ".jsp".  Since our view is named "index", go ahead and create the file in /src/main/webapp/WEB-INF/jsp/index.jsp.  (Note: Maven may have placed an index.jsp file in /src/main/webapp/index.jsp.  If it did, you can delete this file as we won’t be using it).

Here is the source of /src/main/webapp/WEB-INF/jsp/index.jsp:

<html>
<body>
My message is ${message}
</body>
</html>

Nothing fancy here.  We’ve used JSP Expression Language to output the value of the "message" attribute to the web page.  The attribute "message", of course, is the same one that we added to the Model object in our @Controller.

That’s all there is to the view.  Now let’s run our app inside Eclipse Galileo 3.5 with Tomcat and Sysdeo.

Running the Application

Make sure you have Sysdeo and Tomcat configured before trying this!  If you started at part 1, you’ve already done this.

Start Tomcat with Sysdeo by clicking on the "Start Tomcat" button. Start-Tomcat-Button

You should see several things in the Tomcat log in the Eclipse console view:

  • Starting DevLoader: Again, this is Sysdeo’s DevLoader setting up the Classpath.
  • Initializing Spring root WebApplicationContext: This is Spring starting up.
  • Mapped URL path [/index.html] onto handler [com.technologicaloddity.hellowebapp.IndexController@1b24442]: This is our Spring reading the @RequestMapping from IndexController.

Open up a web browser and point it to http://localhost:8080/HelloWebApp/index.html.  If all has gone well, you should see:

My message is Hello Web World!

If this is what you see, congratulations!  Your web application is running correctly!  If not, some things to look for:

  • Review the Tomcat settings for the project, including the DevLoader
  • Make sure DevLoader is in $TOMCAT_HOME/server/classes
  • Make sure your Tomcat is running on 8080.  This is the default, but can be changed.
  • Make sure your web application context is set to "HelloWebApp" and the webapp source is set to "/src/main/webapp".

Testing the Hot Swap with Sysdeo

One of the reasons that I use Sysdeo is that is the only (free) method that I know which can hot-swap Java class files.  As long as you 1) don’t add new imports, and 2) don’t change the public interface of the class, you can modify a running JAVA file and have it reflect immediately in the web application.  Unlike other mechanisms, your Java Session information will NOT be reset, and Tomcat will NOT restart (both of these are good things!)

If, for some reason, you modify a file and Sysdeo cannot hot-swap it, it will let you know that you need to restart Tomcat.

Like most solutions, you can modify JSP pages all you want without restart as well.

You can test it out yourself.  Leave Tomcat running, and modify the IndexController to return a different message.  Save IndexController.  Then reload your web page.  You should see the message change to your new message without restart.

Building a WAR File

The final requirement for our web application is to build a WAR file for it.  As it turns out, Maven makes this incredibly easy.

Stop Tomcat if it is running. 

Right-click on your pom.xml file, and select Run As > Maven Package.  Maven will package your application into the WAR format and place it for you in $PROJECT_HOME/target/HelloWebApp.war. 

How did Maven know you wanted a WAR (as opposed to say, a JAR)?  Inside the pom.xml file, we told Maven that the default packaging for this project was WAR  ("<packaging>war</packaging>"), and Maven is smart enough to know how to build the WAR.

Don’t believe it?  Take the WAR file that Maven builds and install in on another server somewhere.  It works!

Conclusion

I hope this walkthrough of a simple Hello World web application was helpful.  Feel free to ask me any questions you may have in the Comments section of the blog, and I’ll try to answer them the best that I can.

You can download the full source of this project from http://technologicaloddity.com/code/general/HelloWebApp.zip.

4 thoughts on “Hello World Web App using Spring Annotations Part 6”

  1. I like the way you explain the tiny details of configuring Spring 3.0 which I have been struggling with for some time. I run it using jetty (not Sysdeco), and that works ok too. My only problem is that when I run it, it prints “My message is ${message}”, so the message part is not interpreted. Any clues?

    1. Some containers ignore Expression Language (EL) by default. You may want to try to put this at the top of your JSP page:

      < %@ page isELIgnored ="false" %>

      Bob

  2. You said “I hope this walkthrough of a simple Hello World web application was helpful.” Yes, it was incredibly helpful. I wrote tons of Java about 8 years ago and it was a different world then. I’m in the process of learning Eclipse, Maven, Spring, etc. all at the same time, and this tutorial was perfect. Thank you very much.

Leave a Reply

Your email address will not be published. Required fields are marked *