Struts2 is one of the most popular Java web application frameworks for building modern web 2.0 applications. In spite of facing stiff competitions from other sophisticated web frameworks like Ruby on Rails, Struts2 has been successful in retaining the top position mostly because of the Java factor associated with it.
This post is tailored for Java developers who are already having a basic theoretical understanding of Struts2 & who are looking forward for developing their first Struts2 application. Creating a Struts2 application from scratch can be a daunting task as it is time consuming & error prone. So, to avoid this, I will be making use of Apache Maven2 for creating a standard Struts2 sample project. Compared to its predecessor Ant, Maven2 eases packaging & deployment of the application by providing sophisticated dependency management features.
Steps for creating & running your first Struts 2 project
1) Install Maven2: Installing Maven2 is pretty straight forward. Maven2 can be downloaded from the official Apache project website. After downloading, to complete the installation you just need to modify the ‘PATH’ environment variable to include the Maven2 bin directory (e.g., D:apache-maven-2.0.10bin). You can verify the installation by giving the command ‘mvn -v’ from the command line. You should get the Maven2 version as a response if the installation was successful.
2) Generate a sample project: Maven2 provides an arch-type feature which can be used to create empty directory structures, configuration files & sample project files for a specific type of a project. Following is the command to create a sample Struts2 project,
‘mvn archetype:create
-DgroupId=com.ts.sample.s2 -DartifactId=starter
-DarchetypeGroupId=org.apache.struts
-DarchetypeArtifactId=struts2-archetype-starter
-DarchetypeVersion=2.0.9-SNAPSHOT
-DremoteRepositories=http://people.apache.org/maven-snapshot-repository‘
where,
groupId, is the package name for the generated java source files.
artifactId, is the name of the sample project to be created.
archetypeArtifactId, is the type of the project
3) Deploy the application: The pom.xml file of the above generated project contains configurations for the Jetty servlet container. Starting the container & deployment of the application can be done by using a single command, ‘mvn jetty: run‘. After executing this command, you can point your browser to the URL ‘http://localhost:8080/starter‘ to access the home page of the sample application.
4) Import the project to your favorite IDE & start enhancement: Once you have the reference application in hand, you can keep that as a base & start modifying it to meet your requirements by importing the source code to your favorite IDE’s like Eclipse. As the above generated sample application is a Maven2 project, the IDE needs to have support for Maven2 in order to successfully import the project. For Eclipse, you can easily add the Maven2 support by using a plugin called M2Eclipse.
If you face difficulties with any of the above steps or if you know any other better ways for kick starting a Struts2 project, please feel free to express your thoughts by making use of the below comment box.