Struts2 Hello World Application

How application works?

Here is the brief description on how Struts 2 Hello World Application works:
Your browser sends a request to the web server for the URL http://localhost:8080/JavaVillage/HelloWorld.action.
1. When you click on the "Run Struts 2 Hello World Application" link, the browser sends a request for the url http://localhost:8080/JavaVillage/HelloWorld.action. The container requests for the resource "HelloWorld.action". By default web.xml file of struts blank application is configured to route all the request for *.action through org.apache.struts2.dispatcher.FilterDispatcher.
Here is the configuration from web.xml file:
<filter>
      <filter-name>struts2</filter-name>
      <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>

<filter-mapping>
      <filter-name>struts2</filter-name>
      <url-pattern>/*</url-pattern>
</filter-mapping>
2. Then the framework looks for the mapping for the action "HelloWorld" and then framework instantiates the appropriate class and calls the execute method. In this case action class is Struts2HelloWorld. Here is the configuration file from struts.xml which defines the action mapping:

<!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
	<constant name="struts.enable.DynamicMethodInvocation" value="false" />
	<constant name="struts.devMode" value="true" />
	<package name="test" extends="struts-default">
		<action name="HelloWorld" class="net.javaVillage.Struts2HelloWorld">
		<result name="success">/resources/HelloWorld.jsp</result>
		</action>
	</package>
</struts>

3. Then the execute method sets the message and returns SUCCESS.
Then framework determines which page is to be loaded if SUCCESS is returned. In our case framework tells the container to load HelloWorld.jsp and render the output.

In the struts 2 framework Actions are used to process the form and user request. The execute method of the action returns SUCCESS, ERROR, or INPUT value. Then based on these values framework tells the container to load and render the appropriate result.
 Public class Struts2HelloWorld{
      private String message;      //setters and getters for message

      public String execute() throws Exception {
                setMessage("Hello World JavaVillage");
                     return "success";

      }
}

4. Container process the HelloWorld.jsp and generates the output.
<%@ taglib prefix="s" uri="/struts-tags" %>

<s:property value="message" />
5. Then the output in the HTML format is sent to the browser.

Struts2 example output page

6. To download the source code click here Struts2HelloWorld.zip

Struts2 sample example