Struts - SPLessons

Struts 2 Result Type

Home > Lesson > Chapter 19
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Struts 2 Result Type

Struts 2 Result Type

shape Description

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

shape Description

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. [c] <result name="success">success.jsp</result> [/c]

shape Example

In struts 2 default result type is taken the dispatcher type. [c] <result name="success" type="dispatcher"> success.jsp </result> [/c] 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 [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> [/xml]

FreeMaker result type

shape Description

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.

shape Example

login.fm [c] Welcome to SPLessons ${id} ${name} [/c] FreeMaker type is defined in the struts.xml file. [xml] <pre><action name="result" class=" Action class"> <result name="login" type="freemaker"> <param name=" view page location">/resource/login.fm</param> </result> </action> [/xml]

Redirect result type

shape Description

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. [xml] <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> [/xml]

Summary

shape Key Points

  • 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.