FileDocCategorySizeDatePackage
RestfulActionMapper.javaAPI DocExample4812Mon Jul 23 13:26:36 BST 2007org.apache.struts2.dispatcher.mapper

RestfulActionMapper

public class RestfulActionMapper extends Object implements ActionMapper
A custom action mapper using the following format:

    http://HOST/ACTION_NAME/PARAM_NAME1/PARAM_VALUE1/PARAM_NAME2/PARAM_VALUE2

You can have as many parameters you'd like to use. Alternatively the URL can be shortened to the following:

    http://HOST/ACTION_NAME/PARAM_VALUE1/PARAM_NAME2/PARAM_VALUE2

This is the same as:

    http://HOST/ACTION_NAME/ACTION_NAME + "Id"/PARAM_VALUE1/PARAM_NAME2/PARAM_VALUE2

Suppose for example we would like to display some articles by id at using the following URL sheme:

    http://HOST/article/Id

Your action just needs a setArticleId() method, and requests such as /article/1, /article/2, etc will all map to that URL pattern.

Fields Summary
protected static final Log
LOG
Constructors Summary
Methods Summary
public ActionMappinggetMapping(javax.servlet.http.HttpServletRequest request, com.opensymphony.xwork2.config.ConfigurationManager configManager)


    /* (non-Javadoc)
     * @see org.apache.struts2.dispatcher.mapper.ActionMapper#getMapping(javax.servlet.http.HttpServletRequest)
     */
          
        String uri = RequestUtils.getServletPath(request);

        int nextSlash = uri.indexOf('/", 1);
        if (nextSlash == -1) {
            return null;
        }

        String actionName = uri.substring(1, nextSlash);
        HashMap<String,String> parameters = new HashMap<String,String>();
        try {
            StringTokenizer st = new StringTokenizer(uri.substring(nextSlash), "/");
            boolean isNameTok = true;
            String paramName = null;
            String paramValue;

            // check if we have the first parameter name
            if ((st.countTokens() % 2) != 0) {
                isNameTok = false;
                paramName = actionName + "Id";
            }

            while (st.hasMoreTokens()) {
                if (isNameTok) {
                    paramName = URLDecoder.decode(st.nextToken(), "UTF-8");
                    isNameTok = false;
                } else {
                    paramValue = URLDecoder.decode(st.nextToken(), "UTF-8");

                    if ((paramName != null) && (paramName.length() > 0)) {
                        parameters.put(paramName, paramValue);
                    }

                    isNameTok = true;
                }
            }
        } catch (Exception e) {
            LOG.warn(e);
        }

        return new ActionMapping(actionName, "", "", parameters);
    
public java.lang.StringgetUriFromActionMapping(ActionMapping mapping)

        String base = mapping.getNamespace() + mapping.getName();
        for (Iterator iterator = mapping.getParams().entrySet().iterator(); iterator.hasNext();) {
            Map.Entry entry = (Map.Entry) iterator.next();
            String name = (String) entry.getKey();
            if (name.equals(mapping.getName() + "Id")) {
                base = base + "/" + entry.getValue();
                break;
            }
        }

        return base;