When you submit a HTML form to the framework, the input is not sent to another server page, but to a Java class that you provide. These classes are called Actions. After the Action fires, a Result selects a resource to render the response. The resource is generally a server page, but it can also be a PDF file, an Excel spreadsheet, or a Java applet window.
Suppose you want to create a simple "Hello World" example that displays a welcome messagestruts2(1)_休闲. to create a "Hello World" example, you need to do three things:
  1. Create a server page to present the messages
  2. Create an Action class to create the message
  3. Create a mapping to couple the action and page
    struts2(1)_休闲_02 By creating these components, we are separating the workflow into three well-known concerns: the View, the Model, and the Controller. Separating concerns makes it easier to manage applications as they become more complex.

How the Code Works

Your browser sends to the web server a request for the URL http://localhost:8080/tutorial/HelloWorld.actionstruts2(1)_休闲.
  1. The container receives from the web server a request for the resource HelloWorld.action. According to the settings loaded from the web.xml, the container finds that all requests are being routed to org.apache.struts2.dispatcher.FilterDispatcher, including the *.action requests. The FilterDispatcher is the entry point into the framework.
  2. The framework looks for an action mapping named "HelloWorld", and it finds that this mapping corresponds to the class "HelloWorld". The framework instantiates the Action and calls the Action's execute method.
  3. The execute method sets the message and returns SUCCESS. The framework checks the action mapping to see what page to load if SUCCESS is returned. The framework tells the container to render as the response to the request, the resource HelloWorld.jsp.
  4. As the page HelloWorld.jsp is being processed, the <s:property value="message" /> tag calls the getter getMessage of the HelloWorld Action, and the tag merges into the response the value of the message.
  5. A pure HMTL response is sent back to the browser.
For detailed information on Struts 2 architecture see Big Picture.

Testing Actions

Testing an Action is easy. Here's a test for our Hello World Action.
HelloWorldTest.java
package tutorial;
import junit.framework.TestCase;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorldTest extends TestCase {
  public void testHelloWorld() throws Exception {
    HelloWorld hello_world = new HelloWorld();
    String result = hello_world.execute();
    assertTrue("Expected a success result!",
      ActionSupport.SUCCESS.equals(result));
    assertTrue("Expected the default message!",
      HelloWorld.MESSAGE.equals(hello_world.getMessage()));
    }
}

What to Remember

The framework uses Actions to process HTML forms and other requests. The Action class returns a result-name such as SUCCESS, ERROR, or INPUT. Based on the mappings loaded from the struts.xml, a given result-name may select a page (as in this example), another action, or some other web resource (p_w_picpath, PDF).
When a server page is rendered, most often it will include dynamic data provided by the Action. To make it easy to display dynamic data, the framework provides a set of tags that can be used along with HTML markup to create a server page.