Different Types of Struts2 Tags

Different Types of Struts2 Tags

Struts2 provides a rich set of tags that can be used in JSP pages to create dynamic web applications. These tags are used to interact with the Struts2 framework, manage form data, control flow, and display data. They simplify the development process by providing powerful abstractions and reducing the need for explicit Java code in JSP pages.

Different Types of Struts2 Tags

Different Types of Struts2 Tags

  • 1.  Data Tags : Used to manipulate and display data.
    • bean: Instantiates and initializes a bean.
    • property: Accesses and displays a property of a bean.
    • set: Assigns a value to a variable.
  • 2.  Control Tags : Used to control the flow of the application.
    • if: Conditionally includes content.
    • else: Includes content when the condition in if is false.
    • iterator: Iterates over a collection.
  • 3.  Form Tags : Used to create and manage HTML forms.
    • form: Creates a form.
    • textfield: Creates a text field.
    • submit: Creates a submit button.
  • 4.  UI Tags : Used to create user interface components.
    • div: Creates a div element.
    • a: Creates a link.
    • date: Formats and displays a date.

Java Example

Let’s create a simple Struts2 application using various tags.

Step 1 : Create Struts2 Action
java
package com.example.action;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {

    private String name;
    private String message;

    @Override
    public String execute() {
        if (name == null || name.isEmpty()) {
            addActionError("Name cannot be empty");
            return INPUT;
        }
        message = "Hello, " + name + "!";
        return SUCCESS;
    }

    public String getName() {
        return name;
    }

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

    public String getMessage() {
        return message;
    }
}

Step 2 : Configure Struts2
xml
<!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="user" class="com.example.action.UserAction">
            <result name="success">/welcome.jsp</result>
            <result name="input">/userForm.jsp</result>
        </action>
    </package>
</struts>

Step 3 : Create JSP Pages Using Struts2 Tags
jsp
<%@ taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
    <title>User Form</title>
</head>
<body>
    <s:form action="user" method="post">
        <s:textfield name="name" label="Name" />
        <s:submit value="Submit" />
    </s:form>

    <s:if test="hasActionErrors()">
        <s:iterator value="actionErrors">
            <div style="color:red;"><s:property /></div>
        </s:iterator>
    </s:if>
</body>
</html>


 welcome.jsp 

jsp
<%@ taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
    <title>Welcome</title>
</head>
<body>
    <h1><s:property value="message" /></h1>
</body>
</html>

Explanation of the Example

  • 1.  Struts2 Action : UserAction.java handles form submissions. It checks if the name field is empty and sets a message accordingly.
  • 2.  Struts Configuration : struts.xml maps the user action to the UserAction class and specifies the result pages.
  • 3.  JSP Pages :
    • userForm.jsp : Uses form tags (s:form, s:textfield, s:submit) to create a form. It also uses control tags (s:if, s:iterator) to display error messages.
    • welcome.jsp : Uses a data tag (s:property) to display a message.

This example demonstrates how to use different Struts2 tags to create a form, handle user input, and display messages dynamically.