Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Struts2 result type


May 15, 2021 Struts2


Table of contents


As mentioned earlier, the label plays the role of view in the Struts2 MVC framework. Action is responsible for executing the business logic, and the next step is to display the view using the hashtags.
There are usually some navigation rules attached to the results. For example, if action is to authenticate a user, there are three possible outcomes: (a) a successful login (b) failure to log on: an incorrect user name or password (c) account lockout.
In this case, action will configure three possible result strings and three different views to render the results, as we saw in our previous example.
However, Struts2 is not bound to use JSP as a view technique. A fter all, the purpose of the MVC paradigm is to keep layers separate and highly configurable. F or example, for a Web 2.0 client, you might want to return XML or JSON as output. In this case, you can create a new result type for XML or JSON and implement this.
Struts provides many predefined result types, and what we've seen is the default result type dispatcher, which is used to distribute to JSP pages. Struts allows you to render results for view technology in other markup languages, including Velocity, Freemaker, XSLT, and Tiles.

Dispatcher result type

The dispatcher result type is the default type, which is used if no other result type is specified. I t is used to forward servlets, JSPs, HTML, etc. to the server. It uses the RequestDispatcher.forward() method.
We saw the "short" version in the previous example, where we used a JSP path as the body of the result label.

<result name="success">
   /HelloWorld.jsp
</result>

We can also use the slt;result ... T he label in the element specifies the JSP file as follows:

<result name="success" type="dispatcher">
   <param name="location">
      /HelloWorld.jsp
   </param >
</result>

We can also use a parse parameter, which is true by default. T he parse parameter determines whether the position parameter will be resolved for the OGNL expression.

FreeMaker result type

In this example, we'll show you how to use FreeMaker as a view technique. F reemaker is a popular template engine that uses predefined templates to generate output. L et's create a Freemaker template file hello.fm the following:

Hello World ${name}

The file above is a template where name is an argument that will be passed from the outside using the defined action. Y ou can keep this file in CLASSPATH. N ext, let's refer to the following modified struts .xml to specify the results:

<?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="struts.devMode" value="true" />
   <package name="helloworld" extends="struts-default">

      <action name="hello" 
         class="cn.w3cschool.struts2.HelloWorldAction"
         method="execute">
         <result name="success" type="freemarker">
            <param name="location">/hello.fm</param>
         </result>
      </action>
      
   </package>

</struts>

We continue to keep the HelloWorldAction .java, HelloWorldAction.jsp and index files .jsp chapter. N ow right-click on the project name, and then click "Export" and "WAR File" to create a WAR file. T he WAR file is then deployed in Tomcat's webapps directory. F inally, start the Tomcat server and try to access the URL http://localhost:8080/HelloWorldStruts2/index.jsp. T he following page will be displayed:

Struts2 result type

Enter the value "Struts2" and submit it, you can see the following page

Struts2 result type

In this example we used Freemaker, and you can see that this is exactly the same as the JSP view, but we are not bound to use JSP as a view technique.

The type of redirect result

The redirect result type calls the standard response.sendRedirect() method, allowing the browser to create a new request to a given location.
We can be in the .lt;result... G iven a position in the body of an element or as an element of the "location" of the name of the element. Redirect also supports parse parameters, and here's an example of using XML configuration:

<action name="hello" 
   class="com.tutorialspoint.struts2.HelloWorldAction"
   method="execute">
   <result name="success" type="redirect">
       <param name="location">
         /NewWorld.jsp
      </param >
   </result>
</action>

So just modify your struts.xml file to define the type of direct result mentioned above and create a new file NewWorld.jpg, which will produce a direct result when your hello action returns "success". You can check the Redirect Action example for Struts 2 to learn more about it.