Struts - SPLessons

Chapter 19

Struts 2 Result Type

Struts 2 Result Type

Struts 2 Result Type are just JSP pages that map to an action, Struts 2 is supported different Result/Result Type View pages. Action class business logic is executed and the result forwarded to View pages. In Struts2, <result> tag is used in struts.xml configuration file under the action tag. Following are the types of result types and result tag will be kept in struts.xml file.

Dispatcher result type

Dispatcher result type is the default result type. If one do not write any result type in struts.xml file, the container will go to the default pages. like JSP pages or HTML pages. It is internally called the RequestDispatcher.forward(). Action class is executed and generated the response and this response is forwarded to success.jsp page.

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

In struts 2 default result type is taken the dispatcher type.

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

If one configure the success.jsp page in Web content folder, have to write the <param> tag (optional). change the success.jsp page location in Web content folder and write the <param > tag under <result> tag. <param > represent location of View pages.

struts.xml

    //If we are taken the success.jsp page in resource folder.
<action name="result" class=" Action class">
     <result name="success" type="dispatcher">
        <param name=" view page location">/resource/success.jsp</param>
     </result>
</action>

FreeMaker result type

FreeMaker result type is a type of view page, which generate the output using predefined templates. One can defined the result type=” free maker” ,which internally call the dispatcher.include().

If one pass the input from the browser, and that input value is set to the action class through request object. Then, the properties can be called in another FreeMakerView page using predefined templates. FreeMaker view pages saved on filename.fm.

login.fm

Welcome to SPLessons
${id}
${name}

FreeMaker type is defined in the struts.xml file.

<pre><action name="result" class=" Action class">
     <result name="login" type="freemaker">
        <param name=" view page location">/resource/login.fm</param>
     </result>
</action>

Redirect result type

When the defined result type is redirect in struts.xml file, then the container will call response.sendRedirect() method internally. Create the response in a new page in given location.

<struts>
   <package name="default" extends="struts-default">
       <action name="login" class="LoginActionClassName" method="execute">
          <result name="login" type="redirect">
             <param>/Newlogin.jsp</param>
          <result>
       <action> 
   <package>
<struts>


Summary

  • Struts 2 Result Type – The tag is utilized to expose the view.
  • Struts 2 Result Type – Dispatcher is the default result type.
  • Struts 2 Result Type – The free maker engine uses predefined templates to get an output.