Struts2 core components

Struts2 core components

Struts2, a popular Java-based web application framework, follows the Model-View-Controller (MVC) design pattern. The core components of Struts2 work together to provide a flexible and powerful framework for building web applications. These components include Actions, Interceptors, Results, ValueStack and OGNL, Tag Libraries, and Configuration files.

Struts2 core components

Key Components

  • 1.  Action : The core of the MVC controller, responsible for handling user requests and executing business logic.
  • 2.  Interceptors : Provide pre-processing and post-processing logic around actions, handling cross-cutting concerns like validation, logging, and exception handling.
  • 3.  Result : Defines what should be rendered back to the user, such as JSP pages, Freemarker templates, or redirects.
  • 4.  ValueStack and OGNL : Manages data flow between the application and the view, allowing easy access to data in JSPs and other view technologies.
  • 5.  Tag Libraries : Provides a set of custom tags for use in JSPs to create dynamic content.
  • 6.  Configuration Files : XML files that configure the Struts2 framework, such as struts.xml for action mappings and struts.properties for various settings.

Explanation and Java Example

1. Action

Actions are simple POJOs that encapsulate the business logic of the application. They are invoked when a user makes a request, and they return a result indicating the outcome of the processing.

Example
java
package com.example.action;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorldAction extends ActionSupport {
    private String message;

    public String execute() {
        message = "Hello, World!";
        return SUCCESS;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

2. Interceptors

Interceptors provide a mechanism for cross-cutting concerns by allowing pre-processing and post-processing of requests. They are configured in the struts.xml file and can be applied globally or to specific actions.

Example
java
package com.example.interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class LoggingInterceptor extends AbstractInterceptor {
    @Override
    public String intercept(ActionInvocation invocation) throws Exception {
        System.out.println("Before action execution: " + invocation.getAction().getClass().getName());
        String result = invocation.invoke();
        System.out.println("After action execution: " + invocation.getAction().getClass().getName());
        return result;
    }
}

Configuration in struts.xml
xml
<struts>
    <package name="default" extends="struts-default">
        <interceptors>
            <interceptor name="logging" class="com.example.interceptor.LoggingInterceptor"/>
            <interceptor-stack name="customStack">
                <interceptor-ref name="logging"/>
                <interceptor-ref name="defaultStack"/>
            </interceptor-stack>
        </interceptors>
        <action name="helloWorld" class="com.example.action.HelloWorldAction">
            <interceptor-ref name="customStack"/>
            <result name="success">/success.jsp</result>
        </action>
    </package>
</struts>

3. Result

Results determine what gets returned to the user after an action is executed. Common results include JSP pages, redirects, and JSON responses.

Configuration in struts.xml
xml
<action name="helloWorld" class="com.example.action.HelloWorldAction">
    <result name="success">/success.jsp</result>
</action>

4. ValueStack and OGNL

The ValueStack is a central place where all the data in a Struts2 application is stored. OGNL (Object-Graph Navigation Language) is used to access and manipulate data within the ValueStack.

Java Example
java
private String message = "Hello, World!";
public String getMessage() {
    return message;
}

In the JSP
jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <title>Struts2 Hello World</title>
</head>
<body>
    <h1><s:property value="message"/></h1>
</body>
</html>

5. Tag Libraries

Struts2 provides a rich set of tag libraries that help in creating dynamic web content. These tags are used in JSPs to interact with the data in the ValueStack.

In the JSP
jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <title>Struts2 Hello World</title>
</head>
<body>
    <s:form action="helloWorld">
        <s:textfield name="message" label="Message"/>
        <s:submit value="Submit"/>
    </s:form>
</body>
</html>

6. Configuration Files

Configuration files like struts.xml and struts.properties are used to define action mappings, interceptor stacks, and other settings required by the framework.

Example of struts.xml
xml
<struts>
    <package name="default" extends="struts-default">
        <action name="helloWorld" class="com.example.action.HelloWorldAction">
            <result name="success">/success.jsp</result>
        </action>
    </package>
</struts>

Conclusion

Struts2 core components work together to provide a robust and flexible framework for developing Java-based web applications. The framework’s emphasis on separating concerns and simplifying configuration makes it a powerful tool for developers.