As part of struts project we need to implement several Action classes to take care of various tasks.

Action class is a sub class of org.apache.struts.action.Action

Example:

  1. Configure Struts in web.xml class.
  2. Create a ActionOne class that extends Actison class.
  3. Configure ActionOne class in struts-config.xml
  4. Deploy & Run the application.

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <servlet>
        <servlet-name>action</servlet-name>
        <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
        <init-param>
            <param-name>config</param-name>
            <param-value>/WEB-INF/struts-config.xml</param-value>
        </init-param>
        <init-param>
            <param-name>debug</param-name>
            <param-value>2</param-value>
        </init-param>
        <init-param>
            <param-name>detail</param-name>
            <param-value>2</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
        </servlet>
    <servlet-mapping>
        <servlet-name>action</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
    </web-app>

ActionOne.java

package com.java2learn.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class ActionOne extends Action {
	
	public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response)throws Exception{
		System.out.println("*****************ActionOne::execute() method************************");
		return null;
	}
	
	public ActionOne(){
		System.out.println("ActionOne is created.");
	}

}

struts-config.xml

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts-config PUBLIC
	  "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
	  "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>

	<action-mappings>
		<action  path="/HelloWorld" type="com.java2learn.action.ActionOne">
            
		</action>
       
	</action-mappings>
</struts-config>

ActionOne class is mapped to the path /HelloWorld

Package Structure

package structure

Output:

Output

Download Project(With jars): StrutsSample

Analysis:

ActionOne

The following steps will be carried out in the server when it receives the request with HelloWorld.do

  1. WC starts the execution of ActionServlet code (struts code).
  2. Struts code checks for the Action class mapped to the path HelloWorld.do (ActionOne)
  3. If required ActionOne object will be created by struts code.
  4. Struts code calls the execute method.
  5. As the execute method returns null, struts will stop processing the request.