Struts2 Tutorial Part 3 – Actions 1

In this tutorial session we will see the important key aspects of Struts2 Actions. If you want to get clear idea over struts 2 from the beginning please refer the below posts.

Struts2 Introduction
Struts2 HelloWorld Application

In nutshell, Struts2 actions are string returning java methods. Those methods are organized in a effective way for Struts2 Framework. See a sample action below.

public String welcome() {
    return "success";
}

The above method is a valid struts2 action. So in short, any java method returns a string can be a struts2 action.

Actions mainly do three thinks.

  1. Action Encapsulates the unit of work. (i.e) The real code logic for the application will be here.
  2. Action returns control string to decide the result page. (i.e) Based on the result string from the action only the framework will decide which page to load (will explain later)
  3. Action is the place where data transfer is happening. (will explain later)

So now we want to know how the framework executes the correct java method when request arrives. Here we take tomcat apache as our example server where our application is running. If suppose the user types the url http://localhost:8080/Struts2Action  in the browser it will come to the application server (i.e) here tomcat.  Then the application server look for the application Struts2Action in its webapps folder.

Once it identifies it will look for web.xml file inside the application WEB_INF folder.Once it finds the web.xml it will read and execute based on that file.

As I explained in the HelloWorld Application the web.xml will redirect all the requests it receives to struts filter (For Struts2 only; not all). Because we added a filter to all the requests. See a sample web.xml below.

<?xml version="1.0" encoding="UTF-8" ?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
    <display-name>Struts2Action</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>
</web-app>

So here we redirected all the requests to org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter filter class. So the control will go to the org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter class. That class takes struts.xml as it’s configuration. So it will look for struts.xml in the class folder path.

See a sample struts.xml file below.

<?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>
    <package name="default" namespace="/" extends="struts-default">
        <action name="">
            <result>home.jsp</result>
        </action>
        <action name="welcome" class="com.rajesh.struts2.Welcome" method="welcome">
            <result name="success">welcome.jsp</result>
        </action>
    </package>
</struts>

Once the struts.xml is found based on that configuration the flow continuous. Clear? As per our example once the user entered the URL http://localhost:8080/Struts2Action  the control will open this struts.xml file and tries to find corresponding action. In our example we never mentioned any name after the application Context Struts2Action.

But in struts.xml we mentioned a action with empty name as below.

<action name="">
     <result>home.jsp</result>
</action>

So it will get match with our request http://localhost:8080/Struts2Action . If suppose we type the url http://localhost:8080/Struts2Action/welcome then the welcome action mentioned in the struts.xml will get executed. See that below.

<action name="welcome" class="com.rajesh.struts2.Welcome" method="welcome">
    <result name="success">welcome.jsp</result>
</action>

Now we will see how to declare an action in the struts.xml. The syntax to declare an action is

<action name={actionName} [class={className}]>
<result [name={resultValue1}]>{resultFileA}</result>
<result [name={resultValue2}]>{resultFileB}</result>
 ...
<result [name={resultValuen}]>{resultFileZ}</result>
</action>

where

actionName is

  • The name of the action
  • Refered by the url
  • Compulsary

className is

  • A Java Class to be executed while calling the respective action
  • Optional

resultName is

  • A string which comes as return type from the action method (ex “success”,”input”,”error”)
  • Optional
  • If not mentioned will take success by default

resultFile is

  • A File to render if resultName matches
  • Complulsory

With this we  finish this session for action. We will continue with a simple example in the net session.

Add a Comment

Your email address will not be published. Required fields are marked *