|Home > Tutorials > Bootstrap > Hello World|
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 message. After setting up an empty "tutorial" web application (see Ready, Set, Go!), to create a "Hello World" example, you need to do three things:
First, we need a server page to present the message.
Second, we need an Action class to create the message.
Third, we need a mapping to tie it all together.
struts.xml file to add the
Go ahead and try it now! Deploy the application and open http://localhost:8080/tutorial/HelloWorld.action and see what happens! You should see a page with the title "Hello World!" and the message "Struts is up and running!".
Your browser sends to the web server a request for the URL http://localhost:8080/tutorial/HelloWorld.action.
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
*.actionrequests. The FilterDispatcher is the entry point into the framework.
executemethod sets the message and returns
SUCCESS. The framework checks the action mapping to see what page to load if
SUCCESSis returned. The framework tells the container to render as the response to the request, the resource
HelloWorld.jspis being processed, the
<s:property value="message" />tag calls the getter
HelloWorldAction, and the tag merges into the response the value of the message.
For detailed information on Struts 2 architecture see Big Picture.
Testing an Action is easy. Here's a test for our Hello World Action.
The framework uses Actions to process HTML forms and other requests. The
Action class returns a result-name such as
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 (image, 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.