mirror of
https://github.com/apache/struts.git
synced 2026-08-31 19:35:40 +00:00
Moves documentation to the wiki
git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@1422955 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
@@ -33,111 +33,8 @@ import java.util.HashMap;
|
||||
import java.util.StringTokenizer;
|
||||
|
||||
/**
|
||||
* <!-- START SNIPPET: description -->
|
||||
*
|
||||
* Improved restful action mapper that adds several ReST-style improvements to
|
||||
* action mapping, but supports fully-customized URL's via XML. The two primary
|
||||
* ReST enhancements are:
|
||||
* <ul>
|
||||
* <li>If the method is not specified (via '!' or 'method:' prefix), the method is
|
||||
* "guessed" at using ReST-style conventions that examine the URL and the HTTP
|
||||
* method.</li>
|
||||
* <li>Parameters are extracted from the action name, if parameter name/value pairs
|
||||
* are specified using PARAM_NAME/PARAM_VALUE syntax.
|
||||
* </ul>
|
||||
* <p>
|
||||
* These two improvements allow a GET request for 'category/action/movie/Thrillers' to
|
||||
* be mapped to the action name 'movie' with an id of 'Thrillers' with an extra parameter
|
||||
* named 'category' with a value of 'action'. A single action mapping can then handle
|
||||
* all CRUD operations using wildcards, e.g.
|
||||
* </p>
|
||||
* <pre>
|
||||
* <action name="movie/*" className="app.MovieAction">
|
||||
* <param name="id">{0}</param>
|
||||
* ...
|
||||
* </action>
|
||||
* </pre>
|
||||
* <p>
|
||||
* This mapper supports the following parameters:
|
||||
* </p>
|
||||
* <ul>
|
||||
* <li><code>struts.mapper.idParameterName</code> - If set, this value will be the name
|
||||
* of the parameter under which the id is stored. The id will then be removed
|
||||
* from the action name. This allows restful actions to not require wildcards.
|
||||
* </li>
|
||||
* </ul>
|
||||
* <p>
|
||||
* The following URL's will invoke its methods:
|
||||
* </p>
|
||||
* <ul>
|
||||
* <li><code>GET: /movie/ => method="index"</code></li>
|
||||
* <li><code>GET: /movie/Thrillers => method="view", id="Thrillers"</code></li>
|
||||
* <li><code>GET: /movie/Thrillers!edit => method="edit", id="Thrillers"</code></li>
|
||||
* <li><code>GET: /movie/new => method="editNew"</code></li>
|
||||
* <li><code>POST: /movie/ => method="create"</code></li>
|
||||
* <li><code>PUT: /movie/Thrillers => method="update", id="Thrillers"</code></li>
|
||||
* <li><code>DELETE: /movie/Thrillers => method="remove", id="Thrillers"</code></li>
|
||||
* </ul>
|
||||
* <p>
|
||||
* To simulate the HTTP methods PUT and DELETE, since they aren't supported by HTML,
|
||||
* the HTTP parameter "__http_method" will be used.
|
||||
* </p>
|
||||
* <p>
|
||||
* The syntax and design for this feature was inspired by the ReST support in Ruby on Rails.
|
||||
* See <a href="http://ryandaigle.com/articles/2006/08/01/whats-new-in-edge-rails-simply-restful-support-and-how-to-use-it">
|
||||
* http://ryandaigle.com/articles/2006/08/01/whats-new-in-edge-rails-simply-restful-support-and-how-to-use-it
|
||||
* </a>
|
||||
* </p>
|
||||
*
|
||||
* <!-- END SNIPPET: description -->
|
||||
*
|
||||
* <!-- START SNIPPET: example -->
|
||||
*
|
||||
* To use the Restful2ActionMapper in an existing struts application we have to change
|
||||
* the strus.mapper.class constant and let it point to the Restful2ActionMapper
|
||||
* {code:xml}
|
||||
* <constant name="struts.mapper.class" value="org.apache.struts2.dispatcher.mapper.Restful2ActionMapper" />
|
||||
* {code}
|
||||
*
|
||||
* The problem with the above approach is that we may break existing actions because
|
||||
* the Restful2ActionMapper tries to guess the method name using conventions
|
||||
* that aren't applicable to normal action classes.
|
||||
*
|
||||
* To overcome the above problem, we have to use a different action mapper depending on the url we want to process.
|
||||
* REST actions will be processed by the Restful2ActionMapper and non-REST actions by the {@link DefaultActionMapper}
|
||||
*
|
||||
* To achieve that we have to rely on nampespaces and the {@link PrefixBasedActionMapper} that can choose
|
||||
* which action mapper to use for a particular url based on a prefix (the action namespace).
|
||||
*
|
||||
* To put everything together, we create a package for our rest actions
|
||||
* {code:xml}
|
||||
* <package name="rest" namespace="/rest" extends="struts-default">
|
||||
* ....interceptor config
|
||||
* <action name="movie/*" class="app.MovieAction">
|
||||
* <param name="id">{0}</param>
|
||||
* ....results
|
||||
* </action>
|
||||
* ....
|
||||
* </package>
|
||||
* {code}
|
||||
*
|
||||
* All other actions remain in their existing packages and namespaces
|
||||
* we use the PrefixBasedActionMapper telling it to use the Restful2ActionMapper for actions
|
||||
* in the /rest namespace and the DefaultActionMapper for all other actions
|
||||
*
|
||||
* {code:xml}
|
||||
* <constant name="struts.mapper.class" value="org.apache.struts2.dispatcher.mapper.PrefixBasedActionMapper" />
|
||||
* <constant name="struts.mapper.prefixMapping" value="/rest:restful2,:struts" />
|
||||
* {code}
|
||||
*
|
||||
* For the Restful2ActionMapper to work we also have to set
|
||||
*
|
||||
* {code:xml}
|
||||
* <constant name="struts.enable.SlashesInActionNames" value="true" />
|
||||
* <constant name="struts.mapper.alwaysSelectFullNamespace" value="false" />
|
||||
* {code}
|
||||
*
|
||||
* <!-- END SNIPPET: example -->
|
||||
* Extended version of {@link RestfulActionMapper}, see documentation for more details
|
||||
* http://struts.apache.org/2.x/docs/restfulactionmapper.html
|
||||
*/
|
||||
public class Restful2ActionMapper extends DefaultActionMapper {
|
||||
|
||||
|
||||
@@ -21,47 +21,21 @@
|
||||
|
||||
package org.apache.struts2.dispatcher.mapper;
|
||||
|
||||
import java.net.URLDecoder;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.StringTokenizer;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.apache.struts2.RequestUtils;
|
||||
|
||||
import com.opensymphony.xwork2.config.ConfigurationManager;
|
||||
import com.opensymphony.xwork2.util.logging.Logger;
|
||||
import com.opensymphony.xwork2.util.logging.LoggerFactory;
|
||||
import org.apache.struts2.RequestUtils;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.net.URLDecoder;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.StringTokenizer;
|
||||
|
||||
/**
|
||||
* <!-- START SNIPPET: description -->
|
||||
*
|
||||
* A custom action mapper using the following format:
|
||||
* <p/>
|
||||
* <p/>
|
||||
* <ul><tt>http://HOST/ACTION_NAME/PARAM_NAME1/PARAM_VALUE1/PARAM_NAME2/PARAM_VALUE2</tt></ul>
|
||||
* <p/>
|
||||
* You can have as many parameters you'd like to use. Alternatively the URL can be shortened to the following:
|
||||
* <p/>
|
||||
* <ul><tt>http://HOST/ACTION_NAME/PARAM_VALUE1/PARAM_NAME2/PARAM_VALUE2</tt></ul>
|
||||
* <p/>
|
||||
* This is the same as:
|
||||
* <p/>
|
||||
* <ul><tt>http://HOST/ACTION_NAME/ACTION_NAME + "Id"/PARAM_VALUE1/PARAM_NAME2/PARAM_VALUE2</tt></ul>
|
||||
* <p/>
|
||||
* Suppose for example we would like to display some articles by id at using the following URL sheme:
|
||||
* <p/>
|
||||
* <ul><tt>http://HOST/article/Id</tt></ul>
|
||||
* <p/>
|
||||
* <p/>
|
||||
* Your action just needs a setArticleId() method, and requests such as /article/1, /article/2, etc will all map
|
||||
* to that URL pattern.
|
||||
*
|
||||
* <!-- END SNIPPET: description -->
|
||||
*
|
||||
* Simple Restfull Action Mapper to support REST application
|
||||
* See docs for more information
|
||||
* http://struts.apache.org/2.x/docs/restfulactionmapper.html
|
||||
*/
|
||||
public class RestfulActionMapper implements ActionMapper {
|
||||
protected static final Logger LOG = LoggerFactory.getLogger(RestfulActionMapper.class);
|
||||
|
||||
Reference in New Issue
Block a user