xwork configuration
ITWeb/개발일반 2010. 3. 11. 22:32원본글 : http://wiki.opensymphony.com/display/WW/Result+Types
Attribute | Required | Description |
---|---|---|
name | yes | key to for other packages to reference |
extends | no | inherits package behavior of the package it extends |
namespace | no | see Namespace Configuration |
abstract | no | declares package to be abstract (no action configurations required in package) |
<action name="foo" class="mypackage.simpleAction>
<result name="success" type="dispatcher">greeting.jsp</result>
</action>
<action name="bar" class="mypackage.simpleAction">
<result name="success" type="dispatcher">bar1.jsp</result>
</action>
</package>
<package name="mypackage1" namespace="/">
<action name="moo" class="mypackage.simpleActtion">
<result name="success" type="dispatcher">moo.jsp</result>
</action>
</package>
<package name="mypackage2" namespace="/barspace">
<action name="bar" class="mypackage.simpleAction">
<result name="success" type="dispatcher">bar2.jsp</result>
</action>
</package>
<include file="xwork-default.xml"/>
<include file="xwork-extension.xml"/>
<include file="xwork-mail.xml"/>
<include file="xwork-xmlrpc.xml"/>
....
</xwork>
Actions are the basic "unit-of-work" in XWork, they define, well, actions. An action will usually be a request, (and usually a button click, or form submit). The main action element has two parts, the friendly name (referenced in the URL, i.e. saveForm.action) and the corresponding "handler" class.
<action name="formTest" class="com.opensymphony.xwork.example.SampleAction" method="processSample">
The optional "method" parameter tells XWork which method to call based upon this action. If you leave the method parameter blank, XWork will call the method execute() by default. If there is no execute() method and no method specified in the xml file, XWork will throw an exception.
<package name="myPackage" ....>
...
<default-action-ref name="simpleViewResultAction">
<!--
An example of a default action that is just a simple class
that has 3 fields: successUrl, errorUrl, and inputUrl. This action
parses the request url to set the result values. In the normal case
it just renders velocity results of the same name as the requested url.
-->
<action name="simpleViewResultAction" class="SimpleViewResultAction">
<result type="velocity">${successUrl}</result>
<result name="error" type="velocity">${errorUrl}</result>
<result name="input" type="velocity">${inputUrl}</result>
</action>
...
</package>
Result tags tell XWork what to do next after the action has been called. There are a standard set of result codes built-in to XWork, (in the Action interface) they include:
String SUCCESS = "success"; String NONE = "none"; String ERROR = "error"; String INPUT = "input"; String LOGIN = "login";
You can extend these as you see fit. Most of the time you will have either SUCCESS or ERROR, with SUCCESS moving on to the next page in your application;
<result name="success" type="dispatcher"> <param name="location">/thank_you.jsp</param> </result>
...and ERROR moving on to an error page, or the preceding page;
<result name="error" type="dispatcher"> <param name="location">/error.jsp</param> </result>
Results are specified in a xwork xml config file (xwork.xml) nested inside <action>. If the location param is the only param being specified in the result tag, you can simplify it as follows:
<action name="bar" class="myPackage.barAction"> <result name="success" type="dispatcher"> <param name="location">foo.jsp</param> </result> </action>
or simplified
<action name="bar" class="myPackage.barAction"> <result name="success" type="dispatcher">foo.jsp</result> </action>
or even simplified further
<action name="bar" class="myPackage.barAction"> <result>foo.jsp</result> </action>
<result-type name="dispatcher" class="com.opensymphony.webwork.dispatcher.ServletDispatcherResult" default="true"/>
<result-type name="redirect" class="com.opensymphony.webwork.dispatcher.ServletRedirectResult"/>
<result-type name="velocity" class="com.opensymphony.webwork.dispatcher.VelocityResult"/>
<result-type name="chain" class="com.opensymphony.xwork.ActionChainResult"/>
<result-type name="xslt" class="com.opensymphony.webwork.views.xslt.XSLTResult"/>
<result-type name="jasper" class="com.opensymphony.webwork.views.jasperreports.JasperReportsResult"/>
<result-type name="freemarker" class="com.opensymphony.webwork.views.freemarker.FreemarkerResult"/>
<result-type name="httpheader" class="com.opensymphony.webwork.dispatcher.HttpHeaderResult"/>
<result-type name="stream" class="com.opensymphony.webwork.dispatcher.StreamResult"/>
<result-type name="plaintext" class="com.opensymphony.webwork.dispatcher.PlainTextResult" />
</result-types>
Result Types
Webwork provides several implementations of the com.opensymphony.xwork.Result interface to make web-based interactions with your actions simple. These result types include:
- Chain Result - used for Action Chaining
- Dispatcher Result - used for JSP integration
- FreeMarker Result - used for FreeMarker integration
- HttpHeader Result - used to control special HTTP behaviors
- JasperReports Result - used for JasperReports integration
- Redirect Result - used to redirect to another URL
- Redirect Action Result - used to redirect to another action
- Stream Result - used to stream an InputStream back to the browser (usually for file downloads)
- Velocity Result - used for Velocity integration
- XSL Result - used for XML/XSLT integration
- PlainText Result - used to display the raw content of a particular page (eg. jsp, html etc)
- Flash Result - used to store current action into http session such that it could be retrieved and push into the stack in subsequent request through FlashInterceptor.
- JSON Result - used to return back JSON response (application/json) by converting an instance of JSONObject obtained through action's property into JSON.
<interceptors> <interceptor name="security" class="com.mycompany.security.SecurityInterceptor"/> <interceptor-stack name="defaultComponentStack"> <interceptor-ref name="component"/> <interceptor-ref name="defaultStack"/> </interceptor-stack> </interceptors>
To use them in your actions:
<action name="VelocityCounter" class="com.opensymphony.xwork.example.counter.SimpleCounter"> <result name="success">...</result> <interceptor-ref name="defaultComponentStack"/> </action>