In the Struts2 MVC framework, the results tag serves as a view. The activity must carry out the business logic. The view is displayed using the <results> tag when the business logic has been carried out.
In the following article, we will learn about the view tag, its type, and its work in detail.
The result Tag
A set of navigational guidelines is frequently included with the results. For instance, there are three possible results if the action method is to authenticate a user.
A Successful login
An unsuccessful login-incorrect password or username;
Account locked
In this case, three potential outcome strings and three alternative rendering views will be specified for the action method.
However, JSP is not a requirement for using Struts2 as the view technology. After all, keeping the layers distinct and highly flexible is the entire point of the MVC model. For a Web2.0 client, you could want to provide XML or JSON as the output. In this situation, you may accomplish this by creating a new result type for XML or JSON.
Whatever we've already seen was the default result type dispatcher, which is used to dispatch to JSP pages, and Struts comes with several predefined result types. Other markup languages, including Velocity, Freemaker, XSLT, and Tiles, are supported by Struts for usage in the view technology that displays the results.
Result Types
We have three Result types, namely-
The Dispatcher Result Type
The FreeMaker Result Type
The Redirect Result Type
The Dispatcher Result Type
The dispatcher result type is utilized if no alternative result type is supplied. It is employed to forward to a server-side servlet, JSP, HTML page, etc. The RequestDispatcher.forward() function is employed.
We used the "shorthand" version where the result tag's body was a JSP route.
<result name = "success">
/HelloWorld.jsp
</result>
In the result...> element, we can additionally provide the JSP file by using the param name = "location"> tag.
We can also provide a parse parameter, which by default, is true. The parse parameter controls the location parameter's parsing for OGNL expressions.
The FreeMaker Result Type
In this illustration, we'll explore how FreeMaker may be used as the view technology. A standard templating engine used to produce output from predetermined templates is called Freemaker. Create the following material in a Freemaker template file called hellothere.fm.
Hello World ${name}
The file mentioned above is a template, and the name is a parameter that will be supplied from an outside source when the defined action is used. This file will remain in your CLASSPATH.
Next, let's alter struts.xml to indicate the desired outcome as follows:
<?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>
<constant name = "dev" value = "true" />
<package name = "hellotothisworld" extends = "struts-default">
<action name = "hellothere"
class = "com.CN.struts2.HelloWorldAction"
method = "execute">
<result name = "succ" type = "freemarker">
<param name = "location1">/hellothere.fm</param>
</result>
</action>
</package>
</struts>
Let's maintain the HelloWorldAction.java, HelloWorldAction.jsp, and index.jsp files that we prepared in the examples chapter.
To produce a War file, right-click on the project name and select Export > WAR File.
After that, deploy this WAR to the webapps directory of Tomcat. Launch the Tomcat server, then attempt to visit the page at http://localhost:8080/HelloWorldStruts2/index.jsp.
Enter the value "Struts2" and submit the page. You should see the next page.
As you can see, everything about this is identical to the JSP view, except that we are not constrained to utilizing JSP as the view technology. In this illustration, Freemaker is utilized.
The Redirect Result Type
The redirect result type calls the standard answer. The sendRedirect() method makes a new request to the specified destination by the browser.
The location can be specified either in the result...> element's body or as a param name = "location"> element. The redirect likewise supports the parsed parameter. Here is an XML configuration sample.
<action name = "hello"
class = "com.tutorialspoint.struts2.HelloWorldAction"
method = "execute">
<result name = "success" type = "redirect">
<param name = "location">
/NewWorld.jsp
</param >
</result>
</action>
Add the above redirect type definition to your struts.xml file, and then create a new file called NewWorld.jpg, to which you will be routed anytime the hello action returns successfully. For further comprehension, see the Struts 2 Redirect Action sample.
Frequently Asked Questions
Can we use Struts and Spring together?
You don't use Spring MVC and Struts together because they are interchangeable. However, showing activity on this post is acceptable when Struts and Spring IoC are combined. MVC framework is typically provided by Struts (most Production support & maintenance applications are already integrated with it).
Is Struts 2 the same as Struts?
Struts 1 allows for distinct Request Processors (lifecycles) for every module, but every module's Actions must use the same lifecycle. Through interceptor stacks, Struts 2 creates various lifecycles for each action. As required, custom stacks can be constructed and applied to multiple Actions.
What is Struts 2 used for?
An open-source framework for creating Java EE web applications is Apache Struts 2. It uses and extends the Java Servlet API to encourage developers to employ a model-view-controller (MVC) architecture.
What is a model in Struts 2 framework?
The data and business logic are contained in the model. The Action component in Struts 2 implements the model. The MVC Pattern's view component is what makes the presentation. JSP, Velocity Template, Freemaker, or another presentation-layer technology is typically used to construct the view in Struts 2.
What is POJO in struts2?
The action class in struts 2 is POJO (Plain Old Java Object). You are not required to implement interfaces or extend classes if you use POJO. Typically, an execute method that represents the business logic should be supplied.
Conclusion
This article taught us about the view tag, its type, and its work in detail. We began with a brief introduction to the result tag and dived deep into its types.