001: package org.apache.velocity.tools.struts;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024: import org.apache.velocity.tools.view.tools.LinkTool;
025:
026: /**
027: * <p>View tool to work with URI links in Struts.</p>
028: * <p><pre>
029: * Template example(s):
030: * <a href="$link.action.update">update something</a>
031: * #set( $base = $link.forward.MyPage.anchor('view') )
032: * <a href="$base.param('select','this')">view this</a>
033: * <a href="$base.param('select','that')">view that</a>
034: *
035: * Toolbox configuration:
036: * <tool>
037: * <key>link</key>
038: * <scope>request</scope>
039: * <class>org.apache.velocity.tools.struts.StrutsLinkTool</class>
040: * </tool>
041: * </pre></p>
042: *
043: * <p>This tool should only be used in the request scope.</p>
044: *
045: * @author <a href="mailto:sidler@teamup.com">Gabe Sidler</a>
046: * @author Nathan Bubna
047: *
048: * @version $Id: StrutsLinkTool.java 479724 2006-11-27 18:49:37Z nbubna $
049: */
050: public class StrutsLinkTool extends LinkTool {
051: protected static final Log LOG = LogFactory
052: .getLog(StrutsLinkTool.class);
053:
054: private String get;
055:
056: /**
057: * <p>This exists to enable a simplified syntax for using this tool in a
058: * template. Now, users can do <code>$link.action.saveFoo</code> instead of
059: * <code>$link.setAction('saveFoo')</code> and
060: * <code>$link.forward.profile</code> instead of
061: * <code>$link.setForward('profile')</code>. Neat, eh? :)</p>
062: * @since VelocityTools 1.3
063: */
064: public StrutsLinkTool get(String getme) {
065: StrutsLinkTool sub = null;
066: if ("action".equalsIgnoreCase(this .get)) {
067: sub = setAction(getme);
068: } else if ("forward".equalsIgnoreCase(this .get)) {
069: sub = setForward(getme);
070: } else {
071: sub = (StrutsLinkTool) this .duplicate();
072: }
073: if (sub == null) {
074: return null;
075: }
076: sub.get = getme;
077: return sub;
078: }
079:
080: /**
081: * <p>Returns a copy of the link with the given action name
082: * converted into a server-relative URI reference. This method
083: * does not check if the specified action really is defined.
084: * This method will overwrite any previous URI reference settings
085: * but will copy the query string.</p>
086: *
087: * @param action an action path as defined in struts-config.xml
088: *
089: * @return a new instance of StrutsLinkTool
090: */
091: public StrutsLinkTool setAction(String action) {
092: String url = StrutsUtils.getActionMappingURL(application,
093: request, action);
094: if (url == null) {
095: LOG.warn("In method setAction(" + action
096: + "): Parameter does not map to a valid action.");
097: return null;
098: }
099: return (StrutsLinkTool) copyWith(url);
100: }
101:
102: /**
103: * <p>Returns a copy of the link with the given global forward name
104: * converted into a server-relative URI reference. If the parameter
105: * does not map to an existing global forward name, <code>null</code>
106: * is returned. This method will overwrite any previous URI reference
107: * settings but will copy the query string.</p>
108: *
109: * @param forward a global forward name as defined in struts-config.xml
110: *
111: * @return a new instance of StrutsLinkTool
112: */
113: public StrutsLinkTool setForward(String forward) {
114: String url = StrutsUtils.getForwardURL(request, application,
115: forward);
116: if (url == null) {
117: LOG.warn("In method setForward(" + forward
118: + "): Parameter does not map to a valid forward.");
119: return null;
120: }
121: return (StrutsLinkTool) copyWith(url);
122: }
123:
124: }
|