11/27/2009

AppFuse Struts2 Tutorial with Eclipse / SpringSource Tools Suite

Excerpt from AppFuse's website:

AppFuse is an open source project and application that uses open source tools built on the Java platform to help you develop Web applications quickly and efficiently. It was originally developed to eliminate the ramp-up time found when building new web applications for customers. At its core, AppFuse is a project skeleton, similar to the one that's created by your IDE when you click through a wizard to create a new web project.

AppFuse builds on Maven to accomplish its goal. AppFuse + Maven can be used command line, creating our application with just a single line; but here we will be using Eclipse.

In this tutorial we'll go through the creation of a simple Struts2 web-application, which we'll extend later. We'll use SpringSource Tools Suite 2.2.1 (STS) which is a free tool based on Eclipse 3.5.1 with a pre-configured set of plugins useful for web-development. STS has the Maven2 plugin which we'll use to configure AppFuse for us.

So, grab a copy of SpringSource Tools from http://www.springsource.com/products/sts. After the installation, select File->New->Project... Type maven, and click Next:



Leave "Use default Workspace location" checked and click Next.



Enter struts in the filter, and select struts2-archetype-starter. This is a so called "Maven Archetype", a ready-to-be-used project template built around the Project Object Model (POM), which will create a starter application for Apache Struts 2.0. Click Next.



Now enter the following info:
  • Group Id: org.techmissive
  • Artifact Id: testdrive-struts
  • Version: 1.0.0
  • Package: org.techmissive.testdrive.struts
And click "Finish":



We have created our template project. Now Eclipse (I mean STS of course) will start downloading all our dependencies, and build the project for us. On OSX, we can see directly the Maven-log; on Windows we have to open the Console view (Alt+Shift+Q then C), and click the down-arrow next to the Open Console button on its toolbar (the window-like button left of the green lamp) and select Maven console to see what Maven is doing.



Included with this template is Jetty, a lightweight servlet container, which can be used to test our application locally without installing a full blown J2EE Server. Let's see how we can start it from STS.

Select Run -> Run Configurations... from the Menu, here we'll specify what happens when we start our application. Select 'Maven Build' from the list, and click on the New icon on the top of the window to create a new configuration.



To set the Base directory, click on Browse Workspace. Now click on testdrive-struts and OK. This sets ${workspace_loc:/testdrive-struts} in the Base directory field, which is a relative resolution from our workspace location.

To run Jetty, we have to run a given goal, so enter jetty:run-war in the goals field.

Replace the New_configuration default name with something more meaningful, like Run Jetty.



Click Apply to save, and Run to start the configuration we just created.

Now Eclipse is working again, starts to download all kind of stuff. But in the end proudly confirms the successful starting of Jetty:



Let's test if it works. Jetty is listening by default on the port 8080, so let's start a browser and check what is there. Start Safari and enter http://localhost:8080/testdrive-struts in the address field:



We have a running Web-application and we didn't have to write a single line of code. This is the main purpose of AppFuse: to provide us a skeleton application we can start working on without hours of hunting for the libraries, configuring them, etc. I just wish all the profiles would work the first time like this one...

No comments:

Post a Comment