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 an HTML page, a JavaServer Pagepage, 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 the a name of corresponding to the appropriate result.
The Code
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<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>
Reducing Configuration Duplication with Global Result Mappings
It's not unusual for mappings to share Results. If your application is secured, then many mappings will need to return "login".
Another way to reduce the amount of configuration in the action.xml
is through the use of global result mappings. Web applications often have a common set of results that are used across many action mappings. Common results include redirects to login actions and permission-denied pages. Rather than define each of these results in every action mapping, the framework lets you centralize the definitions for the common pages.
...
<package name="default" extends="webwork-default">
<global-results>
<result name="login"
type="redirect">/login!default.action</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 | ||||||
---|---|---|---|---|---|---|
| ||||||
<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 |
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 | ||||||
---|---|---|---|---|---|---|
| ||||||
<%@ 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
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.
For more, see Result Types in the Core Developers Guide.
Next | |
---|---|
Prev |
Note | ||
---|---|---|
| ||
Because global results are searched after local results, you can override any global result mapping by creating a local result mapping for a specific action. Recall that results can point to locations using relative or absolute paths. Because you may not know the context in which they're being invoked, it's best to use absolute paths for global results. |
Next | Onward to Understanding Interceptors |
---|---|
Prev | Return to Understanding Actions |