Deploying Struts and testing struts 2 hello world application

I am assuming that you have already installed ant build tool on your machine

Deploying Struts and testing struts 2 hello world application

Deploying Struts and testing struts 2 hello world application

     

I am assuming that you have already installed ant build tool on your machine. Since we are using the ant built tool to build the application. To build the application open command prompt and go to "struts2helloworld\WEB-INF\src" directory of the web application and issue the "ant" command. The ant build tool will compile the java file and create jar file "struts2tutorial.jar" into the lib directory of your web application. Here is the output of ant build tool:

C:\sourcecontrol\code\roseindiacodes\struts2\struts2helloworld\WEB-INF\src>ant
Buildfile: build.xml

clean:
[delete] Deleting directory C:\sourcecontrol\code\roseindiacodes\struts2\struts2helloworld\WEB-INF\classes
[mkdir] Created dir: C:\sourcecontrol\code\roseindiacodes\struts2\struts2helloworld\WEB-INF\classes

prepare:

resources:

compile:
[javac] Compiling 1 source file to C:\sourcecontrol\code\roseindiacodes\struts2\struts2helloworld\WEB-INF\src\classes
[jar] Building jar: C:\sourcecontrol\code\roseindiacodes\struts2\struts2helloworld\WEB-INF\lib\struts2tutorial.jar

project:

all:

BUILD SUCCESSFUL
Total time: 19 seconds
C:\sourcecontrol\code\roseindiacodes\struts2\struts2helloworld\WEB-INF\src>

Testing Struts 2 Hello World Application

In the above section we have compiled our application and now finally we will test our application. To test the application start the tomcat server and type http://localhost:8080/struts2HelloWorldApplication/ and then select "Run Struts 2 Hello World Application" from the list. 

Here is the output of struts 2 Hello World Application:

When run this application you get:

 

After clicking on the "Run Struts 2 Hello World Application" Then you get the message "Struts 2 Hello World Application" on the screen.

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/struts2HelloWorldApplication/roseindia/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/struts2HelloWorldApplication/roseindia/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:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app id="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 Blank</display-name>

      <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>

      <welcome-file-list>
      <welcome-file>index.html</welcome-file>
      </welcome-file-list>

    </web-app>

  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:
    <action name="HelloWorld" class="net.roseindia.Struts2HelloWorld">
    <result>/pages/HelloWorld.jsp</result>
    </action>

        
  3. Then the execute method sets the message and returns SUCCESS. 
    public String execute() throws Exception {
      setMessage(MESSAGE);
      return 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.
     
  4. Container process the HelloWorld.jsp and generates the output.
     
  5. Then the output in the HTML format is sent to the browser.

Download "Hello World" Application