
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.10\bin). 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.
If you would like to make a comment, please fill out the form below.
[...] Five minutes guide for creating your first Struts2 project using Maven2 [...]
Nice article. Have a question to ask. Which one’s better, Maven or Ant? And why?
@Nischal, good question. I prefer Maven2 over Ant as it is more sophisticated. I have been using Ant from past 3+ years. Though it’s easy to write build scripts for small projects using Ant, creating and managing the ant build files is bit complex for enterprise scale projects with lot of dependencies. This is where Maven2 comes in handy. Maven2 provides many features like dependency management, scope management & plug-in based approach, which eases the development, testing & packaging of the applications. And also, projects created using Maven2 will be having a common directory structure, which eases the life of developers working on multiple projects. In spite of these advantages, there are people who still prefer using Ant mainly because of their familiarity & expertise with the tool. More details about Maven2 can be found on the official Maven2 web page.
Thanks for the explanation. I’ve never tried Maven but I guess I’ll give it a try soon