alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Struts example source code file (URL.java)

This example Struts source code file (URL.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Struts tags/keywords

boolean, boolean, componenturlprovider, http, inject, io, request, response, servlet, specifies, string, strutstagattribute, strutstagattribute, the, the, url, url, urlprovider, whether

The Struts URL.java source code

/*
 * $Id: URL.java 753015 2009-03-12 21:00:23Z musachy $
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.apache.struts2.components;

import java.io.Writer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.StrutsConstants;
import org.apache.struts2.views.annotations.StrutsTag;
import org.apache.struts2.views.annotations.StrutsTagAttribute;

import com.opensymphony.xwork2.inject.Inject;
import com.opensymphony.xwork2.util.ValueStack;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;

/**
 * <!-- START SNIPPET: javadoc -->
 *
 * <p>This tag is used to create a URL.

* * <p>You can use the <param> tag inside the body to provide * additional request parameters. If the value of a param is an Array or * an Iterable all the values will be added to the URL.</p> * * <b>NOTE: * <p>By default request parameters will be separated using escaped ampersands (i.e., &amp;). * This is necessary for XHTML compliance, however, when using the URL generated by this tag * with the <s:property> tag, the <b>escapeAmp attribute should be used to disable * ampersand escaping.</p> * * <b>NOTE: * <p>When includeParams is 'all' or 'get', the parameter defined in a <param> * tag will take precedence over any params included due to the includeParams attribute. For * example, in Example 3 below, if there is a id parameter in the url where the page this * tag is included like http://<host>:<port>/<context>/editUser.action?id=3333&name=John * the generated url will be http://<host>:<port>/<context>/editUser.action?id=22&name=John * because the parameter defined in the param tag will take precedence.</p> * * <!-- END SNIPPET: javadoc --> * * * <!-- START SNIPPET: params --> * * <ul> * <li>action (String) - (value or action choose either one, if both exist value takes precedence) action's name (alias)
  • * <li>value (String) - (value or action choose either one, if both exist value takes precedence) the url itself
  • * <li>scheme (String) - http scheme (http, https) defaults to the scheme this request is in * <li>namespace - action's namespace * <li>method (String) - action's method name, defaults to 'execute' * <li>encode (Boolean) - url encode the generated url. Defaults to 'true'. * <li>includeParams (String) - The includeParams attribute may have the value 'none', 'get' or 'all'. Defaults to '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 * </li> * <li>includeContext (Boolean) - Specifies whether to include the web app context path. Defaults to 'true'. * <li>escapeAmp (Boolean) - Specifies whether to escape ampersand (&) to (&amp;) or not. Defaults to 'true'. * <li>portletMode (String) - The resulting portlet mode. * <li>windowState (String) - The resulting portlet window state. * <li>portletUrlType (String) - Specifies if this should be a portlet render or action URL. * <li>forceAddSchemeHostAndPort (Boolean) - Specifies whether to force the addition of scheme, host and port or not. * </ul> * * <!-- END SNIPPET: params --> * * <p/> Examples * <pre> * <!-- START SNIPPET: example --> * * <-- 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> * * <!-- END SNIPPET: example --> * </pre> * * @see Param * */ @StrutsTag(name="url", tldTagClass="org.apache.struts2.views.jsp.URLTag", description="This tag is used to create a URL") public class URL extends ContextBean { private static final Logger LOG = LoggerFactory.getLogger(URL.class); private UrlProvider urlProvider; private UrlRenderer urlRenderer; public URL(ValueStack stack, HttpServletRequest req, HttpServletResponse res) { super(stack); urlProvider = new ComponentUrlProvider(this, this.parameters); urlProvider.setHttpServletRequest(req); urlProvider.setHttpServletResponse(res); } @Inject(StrutsConstants.STRUTS_URL_INCLUDEPARAMS) public void setUrlIncludeParams(String urlIncludeParams) { urlProvider.setUrlIncludeParams(urlIncludeParams); } @Inject public void setUrlRenderer(UrlRenderer urlRenderer) { urlProvider.setUrlRenderer(urlRenderer); this.urlRenderer = urlRenderer; } @Inject(required=false) public void setExtraParameterProvider(ExtraParameterProvider provider) { urlProvider.setExtraParameterProvider(provider); } public boolean start(Writer writer) { boolean result = super.start(writer); urlRenderer.beforeRenderUrl(urlProvider); return result; } public boolean end(Writer writer, String body) { urlRenderer.renderUrl(writer, urlProvider); return super.end(writer, body); } public String findString(String expr) { return super.findString(expr); } public UrlProvider getUrlProvider() { return urlProvider; } @StrutsTagAttribute(description="The includeParams attribute may have the value 'none', 'get' or 'all'", defaultValue="none") public void setIncludeParams(String includeParams) { urlProvider.setIncludeParams(includeParams); } @StrutsTagAttribute(description="Set scheme attribute") public void setScheme(String scheme) { urlProvider.setScheme(scheme); } @StrutsTagAttribute(description="The target value to use, if not using action") public void setValue(String value) { urlProvider.setValue(value); } @StrutsTagAttribute(description="The action to generate the URL for, if not using value") public void setAction(String action) { urlProvider.setAction(action); } @StrutsTagAttribute(description="The namespace to use") public void setNamespace(String namespace) { urlProvider.setNamespace(namespace); } @StrutsTagAttribute(description="The method of action to use") public void setMethod(String method) { urlProvider.setMethod(method); } @StrutsTagAttribute(description="Whether to encode parameters", type="Boolean", defaultValue="true") public void setEncode(boolean encode) { urlProvider.setEncode(encode); } @StrutsTagAttribute(description="Whether actual context should be included in URL", type="Boolean", defaultValue="true") public void setIncludeContext(boolean includeContext) { urlProvider.setIncludeContext(includeContext); } @StrutsTagAttribute(description="The resulting portlet mode") public void setPortletMode(String portletMode) { urlProvider.setPortletMode(portletMode); } @StrutsTagAttribute(description="The resulting portlet window state") public void setWindowState(String windowState) { urlProvider.setWindowState(windowState); } @StrutsTagAttribute(description="Specifies if this should be a portlet render or action URL. Default is \"render\". To create an action URL, use \"action\".") public void setPortletUrlType(String portletUrlType) { urlProvider.setPortletUrlType(portletUrlType); } @StrutsTagAttribute(description="The anchor for this URL") public void setAnchor(String anchor) { urlProvider.setAnchor(anchor); } @StrutsTagAttribute(description="Specifies whether to escape ampersand (&) to (&amp;) or not", type="Boolean", defaultValue="true") public void setEscapeAmp(boolean escapeAmp) { urlProvider.setEscapeAmp(escapeAmp); } @StrutsTagAttribute(description="Specifies whether to force the addition of scheme, host and port or not", type="Boolean", defaultValue="false") public void setForceAddSchemeHostAndPort(boolean forceAddSchemeHostAndPort) { urlProvider.setForceAddSchemeHostAndPort(forceAddSchemeHostAndPort); } }

    Other Struts examples (source code examples)

    Here is a short list of links related to this Struts URL.java source code file:

    ... this post is sponsored by my books ...

    #1 New Release!

    FP Best Seller

     

    new blog posts

     

    Copyright 1998-2021 Alvin Alexander, alvinalexander.com
    All Rights Reserved.

    A percentage of advertising revenue from
    pages under the /java/jwarehouse URI on this website is
    paid back to open source projects.