Struts 2.1.8 Hello World Example

Learn how to develop Hello World application in Struts 2.

Struts 2.1.8 Hello World Example

Struts 2.1.8 Hello World Example

     

In this section we will learn how to develop our first Hello World example using the latest Struts 2.8.1. We will use the properties files to internalize the application.

After completing this example you will be able to develop simple Hello World example in Struts 2.8.1. You will also learn how to configure struts environment in web.xml file. We have provided the necessary library files in the example download. You can download the example and run it on any Servlet container. But we highly recommend you to use Tomcat 6 to run the example. The Ant build tool is also needed to compile the java source code. You can use any text editor to write java and jsp code.

So, let's get started with the Hello World tutorial using Struts 2.1.8:

The web configuration file web.xml:

In the web.xml file we are required to configure the Struts filter. It is done by adding the following code to web.xml file:

<filter>
  <filter-name>struts2</filter-name>
   <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

and then configure the filter mapping for struts2 filter:

 <filter-mapping>
  <filter-name>struts2</filter-name>
   <url-pattern>/*</url-pattern>
</filter-mapping>

The complete code of web.xml file is:

<?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>RoseIndia.net Struts 2.8.1 Tutorials</display-name>

<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</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>

Writing the action class:

The action class is developed by the programmer and business processing logic is written in the Action class. The Struts framework executes the default (or specified) method in the action class.

Here is the code of our HelloWorld.java file:



package net.roseindia;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorld extends ActionSupport {

  public String execute() throws Exception {
  setMessage(getText(MESSAGE));
  return SUCCESS;
  }

  /**
 * Provide default valuie for Message property.
 */
  public static final String MESSAGE = "HelloWorld.message";

  /**
 * Field for Message property.
 */
  private String message;

  /**
 * Return Message property.
 *
 @return Message property
 */
  public String getMessage() {
  return message;
  }

  /**
 * Set Message property.
 *
 @param message Text to display on HelloWorld page.
 */
  public void setMessage(String message) {
  this.message = message;
  }
}

The above class extends com.opensymphony.xwork2.ActionSupport class. It provides a method getText(), which retrieves the message for the current local from the message resource file. The message resource file(s)  must present in the same package. In this application two message resource files package.properties and package_es.properties are present in the net.roseindia package.

The Struts framework executes the following default method :

  public String execute() throws Exception {
  setMessage(getText(MESSAGE));
  return SUCCESS;
  }

This method is just setting the message and the returning the SUCCESS value, which is the result of the action. It is then matched in the action declaration in the struts.xml file and appropriate view is generated and displayed to the user. In this Hello World example HelloWorld.jsp is rendered and displayed to the user. Here is configuration of the HelloWorld action defined in struts.xml file:

<action name="HelloWorld" class="net.roseindia.HelloWorld">
   <result>/example/HelloWorld.jsp</result>
</action>

Here is the complete struts.xml file:

<?xml version="1.0" encoding="UTF-8" ?>
<!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="false" />

<package name="example" namespace="/roseindia" extends="struts-default">

<action name="HelloWorld" class="net.roseindia.HelloWorld">
  <result>/example/HelloWorld.jsp</result>
</action>


<!-- Add actions here -->
</package>

<!-- Add packages here -->

</struts>

The view of the application:

The HelloWorld.jsp file is view in the Hello World example. Here is the code of HelloWorld.jsp file:

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
  <title><s:text name="HelloWorld.message"/></title>
</head>
<body>
   <h2><s:property value="message"/></h2>
<br>
<a href="/struts2/">Back to Index page</a>
</body>
</html>

In the jsp file <s:property value="message"/> is used to display the message from the action class and <s:text name="HelloWorld.message"/>  is used to display the message from the properties file.

Compiling the example:

You can download the complete example from here and then extract and place the web application on tomcat. To compile the application go to command prompt and type ant as shown below:


C:\TutorialsWriting\Struts2\apache-tomcat-6.0.29\webapps\struts2\WEB-INF\src>ant

Buildfile: build.xml

clean:
[delete] Deleting directory C:\TutorialsWriting\Struts2\apache-tomcat-6.0.29\
webapps\struts2\WEB-INF\src\classes
[mkdir] Created dir: C:\TutorialsWriting\Struts2\apache-tomcat-6.0.29\webapp
s\struts2\WEB-INF\src\classes

prepare:
[copy] Copying 1 file to C:\TutorialsWriting\Struts2\apache-tomcat-6.0.29\w
ebapps\struts2\WEB-INF\src\classes

resources:
[copy] Copying 2 files to C:\TutorialsWriting\Struts2\apache-tomcat-6.0.29\
webapps\struts2\WEB-INF\src\classes

compile:
[javac] Compiling 1 source file to C:\TutorialsWriting\Struts2\apache-tomcat
-6.0.29\webapps\struts2\WEB-INF\src\classes
[jar] Building jar: C:\TutorialsWriting\Struts2\apache-tomcat-6.0.29\webap
ps\struts2\WEB-INF\lib\struts2tutorial.jar

project:

all:

BUILD SUCCESSFUL
Total time: 1 second
C:\TutorialsWriting\Struts2\apache-tomcat-6.0.29\webapps\struts2\WEB-INF\src>

Running example:

Start tomcat and type following urls to run the example:

http://localhost:8080/struts2/roseindia/HelloWorld.action?request_locale=en 0

http://localhost:8080/struts2/roseindia/HelloWorld.action?request_locale=es

In this section we have studied how to make Hello World application in Struts 2.1.8 framework.

Download code tutorial code. 1