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.
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
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.
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>
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.