Basic

2. What is the difference between Struts 1 and Struts 2?

Overview

The Apache Struts framework is a free, open-source solution for creating Java web applications. It provides the infrastructure for developing applications following the Model-View-Controller (MVC) design pattern. Understanding the differences between Struts 1 and Struts 2 is crucial for Java developers, as it helps in making informed decisions about upgrading existing applications or starting new projects with the most suitable version.

Key Concepts

  1. Architecture: Differences in the MVC implementation between Struts 1 and Struts 2.
  2. Request Processing: How each version handles HTTP requests differently.
  3. Tag Libraries: Variations in the use and capabilities of tag libraries in Struts 1 vs. Struts 2.

Common Interview Questions

Basic Level

  1. What are the main architectural differences between Struts 1 and Struts 2?
  2. How do Struts 1 and Struts 2 handle form data differently?

Intermediate Level

  1. Can you explain the role of the Action classes in Struts 1 and Struts 2?

Advanced Level

  1. Discuss the advancements in Struts 2 regarding the Interceptor stack over Struts 1's FilterDispatcher.

Detailed Answers

1. What are the main architectural differences between Struts 1 and Struts 2?

Answer: Struts 1 is based on the Model 2 approach of the MVC pattern, where requests are routed through a single servlet which then delegates control to specific action classes. Struts 2, on the other hand, operates more flexibly by using an interceptor-based approach. Actions in Struts 2 are POJOs (Plain Old Java Objects) which eliminates the need for actions to inherit from a framework-specific class, providing more flexibility and ease of testing.

Key Points:
- Struts 1 actions extend an Action class, while Struts 2 actions can be simple POJOs.
- Struts 2 uses an interceptor stack for request processing, which is more flexible and configurable than the Struts 1 approach.
- In Struts 2, views can access the Action class properties directly, whereas Struts 1 requires form beans for this purpose.

Example:

// This C# example is illustrative. Java-based Struts code would follow Java syntax but demonstrates similar principles.

// Struts 1 Action example
public class MyAction extends Action {
    public ActionForward execute(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
            // Action logic here
            return mapping.findForward("success");
    }
}

// Struts 2 Action example (POJO style)
public class MyAction {
    private String message;

    public String execute() throws Exception {
        setMessage("Hello, World!");
        return "success";
    }

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

    public String getMessage() {
        return message;
    }
}

2. How do Struts 1 and Struts 2 handle form data differently?

Answer: In Struts 1, form data is captured in ActionForm beans, which must be explicitly defined for each form. The framework then populates these beans with request parameters automatically. Struts 2 simplifies this process by eliminating the need for ActionForm beans. Instead, form parameters are directly mapped to the action class properties, making the action class itself the model in MVC terms.

Key Points:
- Struts 1 requires separate ActionForm beans for each form.
- Struts 2 maps form parameters directly to action class properties.
- This direct mapping in Struts 2 reduces boilerplate code and simplifies development.

Example:

// Struts 1 ActionForm example
public class LoginForm extends ActionForm {
    private String username;
    private String password;
    // Getters and setters
}

// Struts 2 Action example (Direct property access)
public class LoginAction {
    private String username;
    private String password;
    // Getters and setters, form parameters map directly to these properties
}

3. Can you explain the role of the Action classes in Struts 1 and Struts 2?

Answer: In both Struts 1 and Struts 2, Action classes serve as the controller component of the MVC pattern, responsible for handling user requests. However, the way they are implemented and extended differs significantly between the two versions. In Struts 1, Action classes must extend a specific framework class, while in Struts 2, Action classes are POJOs, enhancing flexibility and testability. Furthermore, Struts 2 supports method-level configuration, allowing a single Action class to handle multiple requests differently.

Key Points:
- Struts 1 requires Action classes to extend an Action class.
- Struts 2 Action classes are POJOs with no need to extend framework-specific classes.
- Struts 2 supports method-level configuration within Action classes, offering greater flexibility.

Example:

// Struts 1 Action class
public class LoginAction extends Action {
    public ActionForward execute(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
            // Logic to process user login
            return mapping.findForward("success");
    }
}

// Struts 2 Action class
public class LoginAction {
    private String username;
    // Logic to process user login using direct property access and method-level configuration
    public String execute() {
        // Process login
        return "success";
    }
}

4. Discuss the advancements in Struts 2 regarding the Interceptor stack over Struts 1's FilterDispatcher.

Answer: Struts 2 introduced the concept of an interceptor stack, a highly configurable series of interceptor actions that are executed before and after an Action is executed. This allows for modular pre-processing and post-processing, such as authentication, logging, and validation, which can be reused across different Actions. This is a significant advancement over Struts 1, where similar functionality had to be implemented within each Action or through a custom servlet filter. The interceptor stack in Struts 2 makes it easier to manage cross-cutting concerns cleanly and efficiently.

Key Points:
- Struts 2's interceptor stack provides a modular way to handle cross-cutting concerns.
- Interceptors in Struts 2 can be globally applied or configured at the action level.
- This approach contrasts with Struts 1, where custom filters or action-specific code were required for similar functionality.

Example:

// Struts 2 Interceptor example
// Define an interceptor class (This is theoretical and serves to illustrate the concept)
public class LoggingInterceptor {
    public String intercept(ActionInvocation invocation) throws Exception {
        // Pre-action execution logic
        System.out.println("Before action execution");

        String result = invocation.invoke(); // Proceed to execute the action or next interceptor in the stack

        // Post-action execution logic
        System.out.println("After action execution");

        return result;
    }
}

// Configuration in struts.xml (not C#, illustrative purpose)
<interceptors>
    <interceptor name="logger" class="com.example.LoggingInterceptor"/>
    <interceptor-stack name="defaultStack">
        <interceptor-ref name="logger"/>
        // Other interceptors
    </interceptor-stack>
</interceptors>

<action name="exampleAction" class="com.example.actions.ExampleAction">
    <interceptor-ref name="defaultStack"/>
    <result name="success">/success.jsp</result>
</action>

This C# code block is used for illustrative purposes. The actual configuration and implementation would use Java syntax and XML configuration files in a Struts 2 application.