Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
⭐Introduction
2.
⭐Zero-configuration
2.1.
🪜Steps to Build
3.
⭐Building an Application
3.1.
✏️Creating index.jsp
3.2.
✏️Creating Action Class
3.3.
✏️Creating View Components
4.
Frequently Asked Questions
4.1.
Is Struts 2 thread safe?
4.2.
What internal processes does Struts framework have?
4.3.
What is the vulnerability in Apache Struts?
4.4.
What are the Struts 2 core components?
4.5.
What is struts 2 used for?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Zero-configuration File by Convention

Author soham Medewar
0 upvote

⭐Introduction

Apache Struts 2 is an open-source web application framework for creating Java EE web applications. In order to encourage developers to employ a model-view-controller (MVC) architecture, it makes use of the Java Servlet API. 

Struts

The WebWork framework, which was created as a spin-off of Apache Struts 1, aims to provide improvements and refinements while maintaining the same general design.

⭐Zero-configuration

Without the struts.xml configuration file, we can still develop struts 2 applications. You can make a zero-configuration file in one of two ways:

  • Using convention
  • Using annotation

In this article, we will be using the convention method to create struts 2 applications without using the struts.xml configuration file.

🪜Steps to Build

The following are the steps to build a zero-configuration file using the convention method:

🔸Make an input page (optional)

🔸Build action class

🔸Build view components

⭐Building an Application

Building Application

In this application, we will be creating only 4 pages. The 4 pages are as follows:

🔸index.jsp

🔸LoginAction.java

🔸login-success.jsp

🔸login-error.jsp

✏️Creating index.jsp

The jsp webpage generates a form using the struts UI tags. It receives the user's name and password.

<%@ taglib uri="/struts-tags" prefix="s" %>  
<s:form action="login">  
<s:textfield name="username" label="Username"></s:textfield>  
<s:password name="password" label="Password"></s:password>  
<s:submit value="Login"></s:submit>  
</s:form>  

✏️Creating Action Class

The name of the action class must have action as a suffix following the request name, or it must implement an action interface (or extent ActionSupport). If you don't wish to implement the Action interface and the request name as login, the action class name should be LoginAction.

The action class must be located inside the action or actions or struts or struts2 package.

package action;  
public class LoginAction 
{  
private String name,password;  
public String getName() 
{  
    return name;  
}  
public void setName(String name) 
{  
    this.name = name;  
}    
public String getPassword() 
{  
    return password;  
}  
public void setPassword(String password) 
{  
    this.password = password;  
}  
public String execute(){  
    if(password.equals("struts"))
    {  
        return "success";  
    }  
    else{
       return "error";
    }
}  
} 

✏️Creating View Components

The view components must be placed in the WEB-INF/content directory.

The request name - (hyphen) string returned by the action class must be used to name the view components. If the request name is login and the action class returns the success and error of the string, the file names must be login-success.jsp and login-error.jsp.

login-error.jsp

Sorry username or password error!  
<jsp:include page="/index.jsp"></jsp:include>  


login-success.jsp

<%@ taglib uri="/struts-tags" prefix="s" %>  
Welcome,<s:property value="name"/>  

 

Must Read Apache Server

Frequently Asked Questions

Is Struts 2 thread safe?

Yes, it is thread-safe.

What internal processes does Struts framework have?

In particular, the Struts framework automatically creates a LoginForm object and populates it with the form data given in the request when an incoming request matches /login. Validate's default value is true by default. This instructs the framework to call the form bean's validate function.

What is the vulnerability in Apache Struts?

A flaw in Apache Struts has been found that could lead to remote code execution. An open-source framework called Apache Struts is used to create Java web applications. Remote code execution can be possible if this vulnerability is successfully exploited.

What are the Struts 2 core components?

Struts2's main components are Action, Interceptors, and Result pages. Struts2 provides numerous methods for creating Action classes and configuring them using struts.

What is struts 2 used for?

Apache Struts 2 is a free and open-source web application framework used to create Java EE web applications. It extends the Java Servlet API in order to encourage developers to employ a model-view-controller (MVC) architecture.

Conclusion

In this article, we have learned to create an application without using the struts.xml configuration file. That’s all from the article.

Be Curious

You can refer to the introduction to spring boot, the 19 best python frameworks, and flask introduction to learn more about different frameworks. 

That's the end of the article. I hope you all like this article. 

Do upvote our blogs if you find them helpful and engaging!

Happy Learning, Ninjas!

Thank You
Live masterclass