Introduction
The struts 2 is an open-source java framework based on the technologies like Java Server Pages(JSP), Extensible Markup Language(XML), Servlet, etc. This framework used the MVC(Model View Controller) architecture. The Struts 2 framework is developed by the Apache foundation. The struts 2 framework helps us to create flexible, responsive web applications with good maintenance. Every framework or language has elements we can use to develop the application. Let’s learn the struts 2 elements in this article.
Struts 2 elements
The elements are the basic units in any programming language or framework to build the applications. The struts 2 framework also has the essential elements we use to develop the application. Let’s learn the primary struts 2 elements with examples now.
Struts element
The struts element is used to develop a struts application. It tells the browser where to start the application code and includes all the elements inside it into the application.
Syntax
<struts>
<!-- code inside elements -->
</struts>
Package element
The package element is a sub-element of the struts element. It is used inside the struts element to define any package or module we extended in the application. The package element extends the struts default package, is we do not extend ay specific package.
Syntax
<package>
<!-- code inside elements -->
</package>
Attributes
The attributes are used with the elements to define additional properties or characters of an element. The attributes used with the package element are
Name: the name of the package.
Namespace: the namespace in which the package we extended exists.
Extend: used to extend the modules or packages.
Action element
It is the sub-element of the package element and is included inside the code. It is used to define any action that needs to take place.
Syntax
<action>
<!-- code inside elements -->
</action>
Attributes
name: the name of the action to be performed
class: the class where we have the method to perform any action
method: the method which contains the code to perform the action.
Result element
The result attribute is a sub-element of the action element. This element defines the result of the action performed.
Syntax
<result>
<!-- code inside elements -->
</result>
Attribute
name: name of the result
type: type of the result
Other elements
There are a lot of elements other than the ones mentioned above, like include, constant, etc. The elements mentioned above are the most used and primary struts 2 elements necessary to create an application.
Let’s create a struts.xml file for developing the struts application. We also include the .jsp files for action and results. You can learn how to create a struts application from the article create a struts 2 application.
struts.xml
<?xml version = "1.0" Encoding = "UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name = "welcome" extends = "struts-default">
<action name = "welcome" class = "com.struts2.Welcome" method = "execute">
<result name = "success">/Result.jsp</result>
</action>
</package>
</struts>
Result.jsp
<%@ page contentType = "text/html; charset = UTF-8" %>
<%@ taglib prefix = "s" uri = "/struts-tags" %>
<html>
<body>
Welcome to the struts 2 framework
</body>
</html>
We created the struts.xml file for the struts application in the above code. The action will be performed based on the given attributes, and then the result will be displayed according to the result.jsp file. We also create another XML to code the action and java files, which you can learn and code according to your application by referring to the article link provided above.