Struts 2 > Home > Tutorials > Step by Step > Understanding Results |

Struts 2
Understanding Results
Edit Page Browse Space Add Page Add News
Added by victorsosa, last edited by Ted Husted on Sep 04, 2006 (view change)

After the Action processes the request, a result is selected to provide the response. A result may simply forward to a HTML page, or a JavaServer page, a FreeMaker or Velocity template, or the result might construct a PDF or some other complex report. There may be multiple results available to an action mapping. To indicate which one to select, the Action class returns a name corresponding to the appropriate result.

struts.xml

    <action name="helloName2" class="tutorial.HelloName2">
      <result name="success">HelloName2-success.jsp</result>
      <result name="error">HelloName-error.jsp</result>
    </action>

So far, our results have used the default type, `Dispatcher`. The `Dispatcher` forwards to another web resource. Other kinds of views can be used by specifying a different result type.

## Configuring Result Types

The `struts-default` package defines result types for the Result classes provided with the framework. If your package uses a custom result type, you can add it to your package.

struts.xml

    <xwork>
       <include name="struts-default.xml"/>
       <package name="default" extends="struts-default">

          <result-types>
            <result-type name="Image" class="tutorial.ImageResult" />
          </result-types>

          <action name="Image" class="tutorial.Image">
            <result name="success" type="Image"/>
         </action>
       </package>
    </xwork>

## Configuring Global Results

It's not unusual for mappings to share a need for the same result. For example, if your application is secure, then many mappings might need to return "login" if the security check fails. Rather than define a common result in every action mapping, the framework lets you define global results.

struts.xml

    <package name="default" extends="struts-default">
       <global-results>
          <result name="login" type="redirect-action">login</result>
          <result name="unauthorized">Unauthorized.jsp</result>
       </global-results>
       <!-- other package declarations -->
    </package>

| Global Absolutes Because global results are searched after local results, you can override any global result mapping by creating a local result mapping. Results can indicate resources using relative or absolute URIs. Because you may not know the context in which a result is being invoked, it's best to use absolute paths for global results.

## Summary

The framework offers a variety of result types. An Action can select the appropriate result by name, without actually knowing what result type will be rendered. If a result is needed by multiple action mappings, it can be defined once as a global result.

Next | Onward to Understanding Interceptors
Prev | Return to Coding Actions