Hello World program in Struts 2.0
| Hello World Application
Here we will see how to create a simpe Struts 2 Hello World Application. The following files are needed to create a Hello World Application.
|
| The following picture shows the directory structure of the Hello World application.
web.xml web.xml is used to configure the servlet container properties of the hello world appliation. The filter and the filter-mapping elements are used to setup the Struts 2 FilterDispatcher. The filter is mapped to the URL pattern “/*”. This means all the incoming request that targets to the Struts 2 action will be handled by FilterDispatcher class.
The gateway for our hello world application is index.jsp file. The index.jsp file should be mentioned in web.xml as shown above. struts.xml The entry point to the XML declarative architecture is struts.xml file. The struts.xml file contains the following action mapping.
index.jsp The Struts 2 UI tags are simple and powerful. To use the struts tags in the jsp page the following taglib directive should be included.
HelloWorld.java As you see the HelloWorld class is very simple. It contains two properties one for the user name and the other for displaying the message.
In the execute() method of the HelloWorld action we compose the message to be displayed. Note we need not have a seperate form bean like struts 1 to access the form data. We can have a simple java class as action. The action need not extend any class or implement any interface. The only obligation is that you need to have an execute() method which returns a String and has a public scope. success.jsp In the success page we display the “Hello User” message using the property tag.
Extract the downloaded files into the webapps folder of Tomcat. Start the Tomcat server. Type the following url in the browser “http://localhost:8080/Example1/index.jsp“. The index page will be displayed. Enter the user name and submit the form. Hello user name message will be displayed. |



Yep.... I couldn't have said it better myself......