FileDocCategorySizeDatePackage
URL.javaAPI DocExample15720Mon Jul 23 13:26:36 BST 2007org.apache.struts2.components

URL

public class URL extends Component

This tag is used to create a URL.

You can use the "param" tag inside the body to provide additional request parameters.

NOTE:

When includeParams is 'all' or 'get', the parameter defined in param tag will take precedence and will not be overriden if they exists in the parameter submitted. For example, in Example 3 below, if there is a id parameter in the url where the page this tag is included like http://://editUser.action?id=3333&name=John the generated url will be http://:/context>/editUser.action?id=22&name=John cause the parameter defined in the param tag will take precedence.

  • action (String) - (value or action choose either one, if both exist value takes precedence) action's name (alias)
  • value (String) - (value or action choose either one, if both exist value takes precedence) the url itself
  • scheme (String) - http scheme (http, https) default to the scheme this request is in
  • namespace - action's namespace
  • method (String) - action's method, default to execute()
  • encode (Boolean) - url encode the generated url. Default is true
  • includeParams (String) - The includeParams attribute may have the value 'none', 'get' or 'all'. Default is 'get'. none - include no parameters in the URL get - include only GET parameters in the URL (default) all - include both GET and POST parameters in the URL
  • includeContext (Boolean) - determine wheather to include the web app context path. Default is true.

Examples



<-- Example 1 -->
<s:url value="editGadget.action">
<s:param name="id" value="%{selected}" />
</s:url>

<-- Example 2 -->
<s:url action="editGadget">
<s:param name="id" value="%{selected}" />
</s:url>

<-- Example 3-->
<s:url includeParams="get" >
<s:param name="id" value="%{'22'}" />
</s:url>


see
Param

Fields Summary
private static final Log
LOG
public static final String
NONE
The includeParams attribute may have the value 'none', 'get' or 'all'. It is used when the url tag is used without a value attribute. Its value is looked up on the ValueStack If no includeParams is specified then 'get' is used. none - include no parameters in the URL get - include only GET parameters in the URL (default) all - include both GET and POST parameters in the URL
public static final String
GET
public static final String
ALL
private HttpServletRequest
req
private HttpServletResponse
res
protected String
includeParams
protected String
scheme
protected String
value
protected String
action
protected String
namespace
protected String
method
protected boolean
encode
protected boolean
includeContext
protected String
portletMode
protected String
windowState
protected String
portletUrlType
protected String
anchor
protected String
urlIncludeParams
protected ExtraParameterProvider
extraParameterProvider
Constructors Summary
public URL(com.opensymphony.xwork2.util.ValueStack stack, HttpServletRequest req, HttpServletResponse res)


           
        super(stack);
        this.req = req;
        this.res = res;
    
Methods Summary
public booleanend(java.io.Writer writer, java.lang.String body)

        String scheme = req.getScheme();

        if (this.scheme != null) {
            scheme = this.scheme;
        }

        String result;
        if (value == null && action != null) {
            if(Dispatcher.getInstance().isPortletSupportActive() && PortletActionContext.isPortletRequest()) {
                result = PortletUrlHelper.buildUrl(action, namespace, parameters, portletUrlType, portletMode, windowState);
            }
            else {
                result = determineActionURL(action, namespace, method, req, res, parameters, scheme, includeContext, encode);
            }
        } else {
            if(Dispatcher.getInstance().isPortletSupportActive() && PortletActionContext.isPortletRequest()) {
                result = PortletUrlHelper.buildResourceUrl(value, parameters);
            }
            else {
                String _value = value;

                // We don't include the request parameters cause they would have been
                // prioritised before this [in start(Writer) method]
                if (_value != null && _value.indexOf("?") > 0) {
                    _value = _value.substring(0, _value.indexOf("?"));
                }
                result = UrlHelper.buildUrl(_value, req, res, parameters, scheme, includeContext, encode);
            }
        }
        if ( anchor != null && anchor.length() > 0 ) {
            result += '#" + anchor;
        }

        String id = getId();

        if (id != null) {
            getStack().getContext().put(id, result);

            // add to the request and page scopes as well
            req.setAttribute(id, result);
        } else {
            try {
                writer.write(result);
            } catch (IOException e) {
                throw new StrutsException("IOError: " + e.getMessage(), e);
            }
        }
        return super.end(writer, body);
    
private java.lang.StringextractQueryString()

        // Parse the query string to make sure that the parameters come from the query, and not some posted data
        String query = req.getQueryString();
        if (query == null) {
            query = (String) req.getAttribute("javax.servlet.forward.query_string");
        }

        if (query != null) {
            // Remove possible #foobar suffix
            int idx = query.lastIndexOf('#");

            if (idx != -1) {
                query = query.substring(0, idx);
            }
        }
        return query;
    
private voidincludeExtraParameters()

        if (extraParameterProvider != null) {
            mergeRequestParameters(value, parameters, extraParameterProvider.getExtraParameters());
        }
    
private voidincludeGetParameters()

        if(!(Dispatcher.getInstance().isPortletSupportActive() && PortletActionContext.isPortletRequest())) {
            String query = extractQueryString();
            mergeRequestParameters(value, parameters, UrlHelper.parseQueryString(query));
        }
    
protected voidmergeRequestParameters(java.lang.String value, java.util.Map parameters, java.util.Map contextParameters)
Merge request parameters into current parameters. If a parameter is already present, than the request parameter in the current request and value atrribute will not override its value. The priority is as follows:-
  • parameter from the current request (least priority)
  • parameter form the value attribute (more priority)
  • parameter from the param tag (most priority)

param
value the value attribute (url to be generated by this component)
param
parameters component parameters
param
contextParameters request parameters


        Map mergedParams = new LinkedHashMap(contextParameters);

        // Merge contextParameters (from current request) with parameters specified in value attribute
        // eg. value="someAction.action?id=someId&venue=someVenue"
        // where the parameters specified in value attribute takes priority.

        if (value != null && value.trim().length() > 0 && value.indexOf("?") > 0) {
            mergedParams = new LinkedHashMap();

            String queryString = value.substring(value.indexOf("?")+1);

            mergedParams = UrlHelper.parseQueryString(queryString);
            for (Iterator iterator = contextParameters.entrySet().iterator(); iterator.hasNext();) {
                Map.Entry entry = (Map.Entry) iterator.next();
                Object key = entry.getKey();

                if (!mergedParams.containsKey(key)) {
                    mergedParams.put(key, entry.getValue());
                }
            }
        }


        // Merge parameters specified in value attribute
        // eg. value="someAction.action?id=someId&venue=someVenue"
        // with parameters specified though param tag
        // eg. <param name="id" value="%{'someId'}" />
        // where parameters specified through param tag takes priority.

        for (Iterator iterator = mergedParams.entrySet().iterator(); iterator.hasNext();) {
            Map.Entry entry = (Map.Entry) iterator.next();
            Object key = entry.getKey();

            if (!parameters.containsKey(key)) {
                parameters.put(key, entry.getValue());
            }
        }
    
public voidsetAction(java.lang.String action)

        this.action = action;
    
public voidsetAnchor(java.lang.String anchor)

        this.anchor = anchor;
    
public voidsetEncode(boolean encode)

        this.encode = encode;
    
public voidsetExtraParameterProvider(org.apache.struts2.components.URL$ExtraParameterProvider provider)

        this.extraParameterProvider = provider;
    
public voidsetIncludeContext(boolean includeContext)

        this.includeContext = includeContext;
    
public voidsetIncludeParams(java.lang.String includeParams)

        this.includeParams = includeParams;
    
public voidsetMethod(java.lang.String method)

        this.method = method;
    
public voidsetNamespace(java.lang.String namespace)

        this.namespace = namespace;
    
public voidsetPortletMode(java.lang.String portletMode)

        this.portletMode = portletMode;
    
public voidsetPortletUrlType(java.lang.String portletUrlType)

        this.portletUrlType = portletUrlType;
    
public voidsetScheme(java.lang.String scheme)

        this.scheme = scheme;
    
public voidsetUrlIncludeParams(java.lang.String urlIncludeParams)

        this.urlIncludeParams = urlIncludeParams;
    
public voidsetValue(java.lang.String value)

        this.value = value;
    
public voidsetWindowState(java.lang.String windowState)

        this.windowState = windowState;
    
public booleanstart(java.io.Writer writer)

        boolean result = super.start(writer);

        if (value != null) {
            value = findString(value);
        }

        // no explicit url set so attach params from current url, do
        // this at start so body params can override any of these they wish.
        try {
            // ww-1266
            String includeParams = (urlIncludeParams != null ? urlIncludeParams.toLowerCase() : GET);

            if (this.includeParams != null) {
                includeParams = findString(this.includeParams);
            }

            if (NONE.equalsIgnoreCase(includeParams)) {
                mergeRequestParameters(value, parameters, Collections.EMPTY_MAP);
            } else if (ALL.equalsIgnoreCase(includeParams)) {
                mergeRequestParameters(value, parameters, req.getParameterMap());

                // for ALL also include GET parameters
                includeGetParameters();
                includeExtraParameters();
            } else if (GET.equalsIgnoreCase(includeParams) || (includeParams == null && value == null && action == null)) {
                includeGetParameters();
                includeExtraParameters();
            } else if (includeParams != null) {
                LOG.warn("Unknown value for includeParams parameter to URL tag: " + includeParams);
            }
        } catch (Exception e) {
            LOG.warn("Unable to put request parameters (" + req.getQueryString() + ") into parameter map.", e);
        }


        return result;