This demo will show you how create Struts 2 Hello World eclipse.To create first Hello World application using struts 2 framework below are needed:
Eclipse 3.2 or above (Download eclipse from http://www.eclipse.org/downloads/ site)
JDK 1.6 or above (Download from here: http://www.oracle.com/technetwork/java/javase/downloads/index.html)
Tomcat 6 or above (Please follow link to install and configure tomcat in eclipse:Configure and Run Tomcat server in eclipse
Struts 2 jars which is already included in project for download in the bottom
Please follow below steps:
Create dynamic web project in eclipse name: Struts2HelloWorld (Please use this link if you are not familiar how to create dynamic project in eclipse: Create Dynamic Web Project Eclipse)
Copy below web.xml content to your project web.xml file
<?xml version="1.0" encoding="UTF-8"?><web-appid="WebApp_9"version="2.4"xmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"><display-name>Struts 2 Hello World Application</display-name><filter><filter-name>struts2</filter-name><filter-class>
As we know for any web application first framework read web.xml file and if we use any framework we do mapping in web.xml file. Above we have filter mapping for struts 2 and corresponding mapping url pattern is /*.
We have welcome page as well HelloWorld.jsp which you will see when you deploy the application.
Next create struts.xml file. Basically whenever you deploy and run application struts.xml file should be there in classpath. It’s good practice if you create resource folder as source folder and create struts.xml file inside it then you don’t need to include in xml file in classpath and it will be available at runtime.
To create resource folder –Right click Project –>Click New –> Click Source folder as below:
Create source folder name resources –> Click Finish
Now create struts.xml file inside resource folder and copy paste below content in it.
Now create ApplicationResources.properties inside resource folder as well and copy paste below content in it. This file we use to keep as message resource mapping and later you could use as internationalization support to your application
label.helloWorld= Struts 2 Hello World!!!
label.username= User Name
error.login= Invalid Username/Password. Please try again.
Now we have to create action class. Create package com.javahorn.action inside src folder and copy paste below LoginAction.java inside it
Below are the jars needed to run the application. You could download project source code in the bottom and copy paste below jars inside lib folder. Below is full project structure and jars files screen shot:
That’s it. Now we are ready to run this project.To run this project right click project –> Click Run As –> Run on Server as below:
Finally you will see below Hello world. We are done.