Struts2 Introduction

What is Struts2?

The struts 2 framework is an elegant and extensible framework for creating MVC-based web application initially created by Craig McClanahan and donated to Apache Foundation in May, 2000 and Struts 1.0 was released in June 2001.

The current stable release of Struts is Struts 2.3.24 was released in 7 May 2015.
Struts 2 is the combination of webwork framework of opensymphony and struts 1.
struts2 = webwork + struts1

The Struts 2 provides supports to POJO based actions, Validation Support, AJAX Support, Integration support to various frameworks such as Hibernate, Spring, Tiles etc, support to various result types such as Freemarker, Velocity, JSP etc.

Struts 2 provides many features that were not in struts 1. The important features of struts 2 framework are as follows:

  • Configurable MVC components
  • POJO based actions
  • AJAX support
  • Integration support
  • Various Result Types
  • Various Tag support
  • Theme and Template support

Steps to create First Struts 2 Application using Netbeans/Eclipse

  1. Add required jar files
  2. Provide filter tag in (web.xml) file
  3. Create the action class (Product.java)
  4. Create input page (index.jsp)
  5. Create view components (welcome.jsp)
  6. Map the request with the action in (struts.xml)
  7. Run the project

1. Add required jar files
Download the latest jar files from here, extract the zip folder and you will find the required jars from the lib folder. Add the following jars to the project’s class path.

  1. commons-fileupload-1.3.1.jar
  2. commons-io-2.2.jar
  3. commons-lang3-3.2.jar
  4. commons-logging-1.1.3.jar
  5. freemarker-2.3.22.jar
  6. javassist-3.11.0.GA.jar
  7. ognl-3.0.6.jar
  8. struts2-core-2.3.24.1.jar
  9. xwork-core-2.3.24.1.jar

2. Provide filter tag in (web.xml) file

<web-app>
<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.jsp</welcome-file>
</welcome-file-list>
</web-app>

3. Create the action class (Product.java)
Place Product.java file in project’s com.javaknowledge package.

package com.javaknowledge;

public class Product {
   private int id;
   private String name;
   private float price;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
        this.price = price;
    }
   
    public String execute(){
    return "success";
    }
}

4. Create input page (index.jsp)
Place index.jsp file in project’s web folder.

<%@ taglib uri="/struts-tags" prefix="s" %>  
<s:form action="product">  
    <s:textfield name="id" label="Product Id"></s:textfield>  
    <s:textfield name="name" label="Product Name"></s:textfield>  
    <s:textfield name="price" label="Product Price"></s:textfield>  
    <s:submit value="save"></s:submit> 
</s:form>

5. Create view components (welcome.jsp)
Place welcome.jsp file in project’s web folder.

<%@ taglib uri="/struts-tags" prefix="s" %>
Product Id:<s:property value="id"/><br/>
Product Name:<s:property value="name"/><br/>
Product Price:<s:property value="price"/>

6. Map the request with the action in (struts.xml)
Place struts.xml file in project’s default package.

<?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" extends="struts-default">
        <action name="product" class="com.javaknowledge.Product">
            <result name="success">/welcome.jsp</result>
        </action>
    </package>
</struts>

7. Run the project
Now compile and run the project and you will see the index.jsp page.

struts.xml in Details

1) package element
The package element specifies a module. You can have one or more packages in the struts.xml file.
Attributes of package element
1. name  attribute is must for defining any package and should be unique if there are multiple packages.
2. extends attribute mostly extends the struts-default package where interceptors and result types are defined. If you extend struts-default, all the actions of this package can use the interceptors and result-types defined in the struts-default.xml file.
3. namespace is an optional attribute of package. If namespace is not present, / is assumed as the default namespace. In such case, to invoke the action class, you need this URI:

/actionName.action 

 
If you specify any namespace, you need this URI:

/namespacename/actionName.action

2) action element
The action is the subelement of package and represents an action.
Attributes of action element
1. name  attribute is must for defining any action.
2. class attribute is the optional for action. If you omit the class attribute, ActionSupport will be considered as the default action. A simple action may be as:

<action name="product">

3. method is an optional attribute. If you don’t specify method attribute, execute method will be considered as the method of action class. So this code:

<action name="product" class="com.javaknowledge.Product">

  
will be same as:

<action name="product" class=" com.javaknowledge.Product" method="execute">

  
If you want to invoke a particular method of the action, you need to use method attribute.

3) result element
It is the sub element of action that specifies where to forward the request for this action.
Attributes of result element
1. name is the optional attribute. If you omit the name attribute, success is assumed as the default result name.
2. type is another optional attribute. If you omit the type attribute, dispatcher is assumed as the default result type.

 

admin

Hello! I am Md. Abdul Bari; founder and admin of javaknowledge; currently providing training as a J2EE faculty in The Computers Ltd. Dhaka, Bangladesh under an IT Scholarship project provided by IDB-BISEW. I am a self learner and passionate about training and writing. I am always trying my best to share my knowledge through my blog.

More Posts - Website

Follow Me:
TwitterFacebookGoogle Plus

2,895` Total Views

Leave a Reply

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


four + = 6