Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

In the Coding Actions lesson, we created a Logon class that tests for input. In the Selecting Results lesson, we act on the outcome of that test.

Selecting an "Input" Result

After the Action processes the a request, a result is selected to provide the response. A result may simply forward to a an HTML page, or a JavaServer page, a FreeMaker or Velocity template, or the result might construct a PDF or some other complex report (like JasperReports). 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.

The Code

Code Block
xml
formatxml
titleactionstruts.xml
borderStylesolid
<action name="helloName2Logon" class="tutorial.HelloName2Logon">
       <result nametype="success">helloName2-success.jsp<redirectAction">Menu</result>
       <result name="error">helloName-errorinput">/Logon.jsp</result>
     </action>

How The Code Works

  • If we enter a username and password into the form, the Logon Action will return "success".
    • "success" is the default result code, so the framework will use the "Menu" action as response. (Which we haven't written yet.)
  • If we do not enter both credentials, the Logon Action will return "input", and the framework will use the Logon.jsp as the response.

In the Hello World lesson, our results 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 action-default package defines result types for the Results provided with the framework. If your package uses a custom result type, you can add it to your package.

types.

...


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

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

      <action name="image" class="tutorial.ImageAction">
        <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 secured, 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.

...


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

The Logon mapping uses a different return type for "success" (the default result code). The redirectAction result type takes the name of an Action (as configured in the struts.xml file) as a parameter, and then issues a client-side redirect to the new action. As a result, the URI on the browser's location bar will change.

Using a Stub Page

As we develop web applications, we often need to make forward references – we need to refer to an action we haven't written yet. For example, in the first part of the lesson, the next step is to open the "Menu" page. If we Logon successfully, there will be no where to go, since "Menu" doesn't exist yet.

One way to work around this problem is to create a stub "Menu" page.

The Code

Code Block
formatHTML
titleMissing.jsp
borderStylesolid

<html>
<head><title>Missing Feature</title></head>

<body>
<p>
    This feature is under construction.
    Please try again in the next iteration.
</p>
</body>
</html>

How the Code Works

  • When the Login class returns "Menu", the framework will match it to our default wildcard mapping.
  • The framework will return the stub "Menu.jsp" for now.
Tip

If you are not using wildcards, another way to inject a "missing" page would be to specify a <default-action-ref> element

Including a Missing Page

If you are building an application page by page, it can be worthwhile to setup a standard "Missing" page, and then include it from your stubs.

The Code

Code Block
formatHTML
titleMenu.jsp
borderStylesolid

<%@ taglib prefix="s" uri="/struts-tags" %>
<s:include value="Missing.jsp" /> 

How the Code Works

  • When the Menu.jsp renders, it will include the content of the standard Missing.jsp.

What to Remember

Note
titleGlobal 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.

...

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

(lightbulb) For more, see Result Types in the Core Developers Guide.

Next

Onward to Understanding Interceptors Validating Input

Prev

Return to Understanding Coding Actions