Monday, May 16, 2011

Introduction to Struts 2 configuration on Tomcat

The configuration of Struts 2 is different from the way Struts 1 configuration. This blog demonstrates  the steps required to configure Struts 2 on Tomcat.
These are the steps:
(1)    Install Sun JDK 1.6
(2)    Install Apache Tomcat 6.0.32
(3)    Download from struts web site.
(4)    This contains struts2-blank.war file. This is an empty war file containing struts2 configuration. We will use this to configure the struts2 web application.
(5)    Copy struts2-blank.war file to the webapps folder of the Tomcat server.
(6)    Start the Tomcat server.
(7)    Type the URL on the web browser - http://localhost:8080/struts2-blank/example/HelloWorld.jsp
(8)    You should see the following screenshot of the application, which means that the application is working fine.

Now, let’s look at various configuration elements that goes with configuring struts2 application.

The web.xml file is used to configure the FilterDispatcher class that handles all requests to struts2 framework. FilterDispatcher class is configured as a filter and maps all the application requests to Struts2 framework.

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="" xmlns:xsi="" xsi:schemaLocation="">

    <display-name>Struts Blank</display-name>





The mappings for MVC are declared in the struts.xml file. This file is located in the folder – WEB-INF/src/java

        <action name="HelloWorld" class="example.HelloWorld">

   <package name="default" namespace="/" extends="struts-default">
        <default-action-ref name="index" />
        <action name="index">
            <result type="redirectAction">
                <param name="actionName">HelloWorld</param>
                <param name="namespace">/example</param>

The following code snippet shows the use of struts tag library to create the view.

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
    <title><s:text name="HelloWorld.message"/></title>

<h2><s:property value="message"/></h2>

        <s:url id="url" action="HelloWorld">
            <s:param name="request_locale">en</s:param>
        <s:a href="%{url}">English</s:a>
        <s:url id="url" action="HelloWorld">
            <s:param name="request_locale">es</s:param>
        <s:a href="%{url}">Espanol</s:a>

The execute method is called to process the request. This method generally call the business POJO classes and fetches / updates data to database. Once done, this call the view to be rendered based on the return string from the execute method.

public class HelloWorld extends ExampleSupport {

    public String execute() throws Exception {
        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 view to be rendered is again the same page – HelloWorld.jsp, the same page is called with different message. The following screenshot shows the HelloWorld example.

The above description demonstrates how easy it is to setup Struts 2 MVC web framework. It also demonstrates that it is quite different from configuring Struts 1 framework.

1 comment:

  1. This comment has been removed by a blog administrator.