Struts 2 Results & Result Types - Apache Struts 2

what is the function of the results and result types in struts 2?

As mentioned previously, the <results> tag plays the role of a view in the Struts2 MVC framework. It views only the result types. Whereas the action is responsible for executing the business logic. Whenever the user executes the business logic the next immediate step is to display the view using the <results> tag.
There are some rules that are attached with the results. For example, if the action method is to authenticate a user, you will get three possible outcomes. (a) Successful Login (b) Unsuccessful Login - Incorrect username or password (c) Account Locked.
In this way, when the action method gets configured, there will be three possible outcome strings with the three different views to render the outcome. This has been already seen this in the previous examples.
But, Struts2 does not tie you up with using JSP as the view technology. The main purpose of the MVC paradigm is to put the layers separate and highly configurable. For example, if you want to return to XML or JSON from a Web2.0 client as the output, you need to create a new result type for XML or JSON and achieve this.
Struts consists of a number of predefined result types and whatever has been seen previously that was the default result type dispatcher, which is used to dispatch to JSP pages. Struts also allow the user to use other mark up languages for the view technology to display the results and popular choices include Velocity, Freemaker, XSLT and Tiles.

The dispatcher result type

The dispatcher result type is the default type, and is used if any other result type is not specified particularly. It's used to forward to a servlet, JSP, HTML page, and so on, on the server. It uses the RequestDispatcher.forward() method.
We have seen the "shorthand" version in the earlier examples, where we had provided a JSP path as the body of the result tag.
We can also specify the JSP file using a <param name="location"> tag within the <result...> element as follows:
We can also supply a parse parameter, which is true by default. The parse parameter helps whether the location parameter will be parsed for OGNL expressions or not.

The FreeMaker result type

In this example you can see how we can use FreeMaker as the view technology. Freemaker is known as popular templating engine that helps to generate the output using predefined templates. So to create a Freemaker template file called hello.fm here is the template with the following contents:
Here in the above file is a template where name is a parameter which we have discussed earlier. That will be passed from outside using the defined action. The user will keep this file in the CLASSPATH. Next, let us modify the struts.xml to specify the result as follows:
Let us keep our HelloWorldAction.java, HelloWorldAction.jsp and index.jsp files as we have created them in examples chapter. Now Right click on the project name and click Export > WAR File to create a War file. Then deploy this WAR in the Tomcat's webapps directory. Finally, start Tomcat server and try to access URL http://localhost:8080/HelloWorldStruts2/index.jsp. This will give you following screen:
helloworldstruts 4(2)
Enter a value "Struts2" and submit the page. You should see the next page
helloworld struts 5(1)
As you can see above, this is exactly same as the JSP view except that we are not tied to using JSP as the view technology. We have used
Freemaker in this example.

The redirect result type

The redirect result type calls the standard response.sendRedirect() method, causing the browser to create a new request to the given location.
Now provide the location either in the body of the <result...> element or as a <param name="location"> element. Redirect also supports the parseparameter. Here's an example configured using XML:
So just modify or change your struts.xml file so as to define redirect type as mentioned above and create a new file NewWorld.jpg where you will have to redirect whenever hello action will return success. For better knowledge and understanding check the Struts 2 Redirect Action example that will let you know the process.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Apache Struts 2 Topics