Author: mrdon Date: Sat Jun 18 17:27:26 2005 New Revision: 191314 URL: http://svn.apache.org/viewcvs?rev=191314&view=rev Log: * Added a command that sets the original URI (servletPath) of the request. This allows code to retrieve the URI that started the request despite what forwards may have occurred. This could have also been done as added code to the SelectModule command, but I wasn't sure how "pure" we wanted commands to be. * Changed the "action" attribute of the Form taglib to be optional. If omitted, the original URI of the request will be used. PR: 6686
Added: struts/core/trunk/src/share/org/apache/struts/chain/commands/AbstractSetOriginalURI.java struts/core/trunk/src/share/org/apache/struts/chain/commands/servlet/SetOriginalURI.java struts/core/trunk/src/test/org/apache/struts/chain/commands/servlet/TestSetOriginalURI.java Modified: struts/core/trunk/conf/share/chain-config.xml struts/core/trunk/src/share/org/apache/struts/Globals.java struts/taglib/trunk/doc/userGuide/struts-html.xml struts/taglib/trunk/src/java/org/apache/struts/taglib/html/FormTag.java Modified: struts/core/trunk/conf/share/chain-config.xml URL: http://svn.apache.org/viewcvs/struts/core/trunk/conf/share/chain-config.xml?rev=191314&r1=191313&r2=191314&view=diff ============================================================================== --- struts/core/trunk/conf/share/chain-config.xml (original) +++ struts/core/trunk/conf/share/chain-config.xml Sat Jun 18 17:27:26 2005 @@ -117,6 +117,11 @@ <!-- Identify the Locale for this request --> <command className="org.apache.struts.chain.commands.servlet.SelectLocale"/> + + + <!-- Set (if needed) the URI of the original request --> + <command + className="org.apache.struts.chain.commands.servlet.SetOriginalURI"/> <!-- Set (if needed) no cache HTTP response headers --> Modified: struts/core/trunk/src/share/org/apache/struts/Globals.java URL: http://svn.apache.org/viewcvs/struts/core/trunk/src/share/org/apache/struts/Globals.java?rev=191314&r1=191313&r2=191314&view=diff ============================================================================== --- struts/core/trunk/src/share/org/apache/struts/Globals.java (original) +++ struts/core/trunk/src/share/org/apache/struts/Globals.java Sat Jun 18 17:27:26 2005 @@ -73,6 +73,14 @@ public static final String MODULE_PREFIXES_KEY = "org.apache.struts.globals.MODULE_PREFIXES"; + /** + * The request attribute under which we store the original URI of the + * request. + * @since Struts 1.3 + */ + public static final String ORIGINAL_URI_KEY = + "org.apache.struts.globals.ORIGINAL_URI_KEY"; + /** * The request attributes key under which your action should store an * <code>org.apache.struts.action.ActionErrors</code> object, if you Added: struts/core/trunk/src/share/org/apache/struts/chain/commands/AbstractSetOriginalURI.java URL: http://svn.apache.org/viewcvs/struts/core/trunk/src/share/org/apache/struts/chain/commands/AbstractSetOriginalURI.java?rev=191314&view=auto ============================================================================== --- struts/core/trunk/src/share/org/apache/struts/chain/commands/AbstractSetOriginalURI.java (added) +++ struts/core/trunk/src/share/org/apache/struts/chain/commands/AbstractSetOriginalURI.java Sat Jun 18 17:27:26 2005 @@ -0,0 +1,67 @@ +/* + * Copyright 2003,2004 The Apache Software Foundation. + * + * Licensed 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.struts.chain.commands; + + +import org.apache.struts.Globals; +import org.apache.struts.chain.contexts.ActionContext; +import org.apache.struts.config.ModuleConfig; + + +/** + * <p>Check to original uri is set, and if not, set it for this + * request.</p> + * + * @version $Rev: 179995 $ $Date: 2005-06-04 07:58:46 -0700 (Sat, 04 Jun 2005) $ + */ + +public abstract class AbstractSetOriginalURI extends ActionCommandBase { + + + // ---------------------------------------------------------- Public Methods + + + /** + * <p>Check to original uri is set, and if not, set it for this + * request.</p> + * + * @param actionCtx The <code>Context</code> for the current request + * + * @return <code>false</code> so that processing continues + */ + public boolean execute(ActionContext actionCtx) throws Exception { + // Set the original uri if not already set + if (!actionCtx.getRequestScope().containsKey(Globals.ORIGINAL_URI_KEY)) { + setOriginalURI(actionCtx); + } + return (false); + + } + + + // ------------------------------------------------------- Protected Methods + + + /** + * <p>Set the original uri.</p> + * + * @param context The <code>Context</code> for this request + */ + protected abstract void setOriginalURI(ActionContext context); + + +} Added: struts/core/trunk/src/share/org/apache/struts/chain/commands/servlet/SetOriginalURI.java URL: http://svn.apache.org/viewcvs/struts/core/trunk/src/share/org/apache/struts/chain/commands/servlet/SetOriginalURI.java?rev=191314&view=auto ============================================================================== --- struts/core/trunk/src/share/org/apache/struts/chain/commands/servlet/SetOriginalURI.java (added) +++ struts/core/trunk/src/share/org/apache/struts/chain/commands/servlet/SetOriginalURI.java Sat Jun 18 17:27:26 2005 @@ -0,0 +1,50 @@ +/* + * Copyright 2003,2004 The Apache Software Foundation. + * + * Licensed 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.struts.chain.commands.servlet; + + +import javax.servlet.http.HttpServletRequest; + +import org.apache.struts.Globals; +import org.apache.struts.chain.commands.AbstractSetOriginalURI; +import org.apache.struts.chain.contexts.ActionContext; +import org.apache.struts.chain.contexts.ServletActionContext; + + +/** + * <p>Set the servlet path.</p> + * + * @version $Rev: 179995 $ $Date: 2005-06-04 07:58:46 -0700 (Sat, 04 Jun 2005) $ + */ + +public class SetOriginalURI extends AbstractSetOriginalURI { + + + // ------------------------------------------------------- Protected Methods + + + protected void setOriginalURI(ActionContext context) { + + ServletActionContext swcontext = (ServletActionContext) context; + HttpServletRequest request = swcontext.getRequest(); + + request.setAttribute(Globals.ORIGINAL_URI_KEY, request.getServletPath()); + + } + + +} Added: struts/core/trunk/src/test/org/apache/struts/chain/commands/servlet/TestSetOriginalURI.java URL: http://svn.apache.org/viewcvs/struts/core/trunk/src/test/org/apache/struts/chain/commands/servlet/TestSetOriginalURI.java?rev=191314&view=auto ============================================================================== --- struts/core/trunk/src/test/org/apache/struts/chain/commands/servlet/TestSetOriginalURI.java (added) +++ struts/core/trunk/src/test/org/apache/struts/chain/commands/servlet/TestSetOriginalURI.java Sat Jun 18 17:27:26 2005 @@ -0,0 +1,63 @@ +package org.apache.struts.chain.commands.servlet; + +import junit.framework.TestCase; + +import org.apache.commons.chain.web.servlet.ServletWebContext; +import org.apache.struts.Globals; +import org.apache.struts.chain.commands.UnauthorizedActionException; +import org.apache.struts.chain.contexts.ServletActionContext; +import org.apache.struts.config.ActionConfig; +import org.apache.struts.mock.MockActionServlet; +import org.apache.struts.mock.MockHttpServletRequest; +import org.apache.struts.mock.MockHttpServletResponse; +import org.apache.struts.mock.MockPrincipal; +import org.apache.struts.mock.MockServletConfig; +import org.apache.struts.mock.MockServletContext; + +/* JUnitTest case for class: org.apache.struts.chain.commands.servlet.SetOriginalURI */ +public class TestSetOriginalURI extends TestCase { + + SetOriginalURI command = null; + + public TestSetOriginalURI(String _name) { + super(_name); + } + + + /* setUp method for test case */ + protected void setUp() throws Exception { + this.command = new SetOriginalURI(); + } + + /* tearDown method for test case */ + protected void tearDown() { + } + + public void testSetOriginalURI() throws Exception { + MockHttpServletRequest request = new MockHttpServletRequest("foo/", "bar.do", null, null); + MockServletConfig servletConfig = new MockServletConfig(); + MockServletContext servletContext = new MockServletContext(); + MockActionServlet servlet = new MockActionServlet(servletContext, servletConfig); + servlet.initInternal(); + ServletActionContext saContext = new ServletActionContext(servletContext, request, new MockHttpServletResponse()); + saContext.setActionServlet(servlet); + + + boolean result = command.execute(saContext); + assertTrue(!result); + String uri = (String)request.getAttribute(Globals.ORIGINAL_URI_KEY); + assertTrue("Original uri not correct: "+uri, "bar.do".equals(uri)); + + + request.setPathElements("foo/", "bar2.do", null, null); + uri = (String)request.getAttribute(Globals.ORIGINAL_URI_KEY); + assertTrue("Original uri not correct: "+uri, "bar.do".equals(uri)); + + } + + /* Executes the test case */ + public static void main(String[] argv) { + String[] testCaseList = {TestSetOriginalURI.class.getName()}; + junit.textui.TestRunner.main(testCaseList); + } +} Modified: struts/taglib/trunk/doc/userGuide/struts-html.xml URL: http://svn.apache.org/viewcvs/struts/taglib/trunk/doc/userGuide/struts-html.xml?rev=191314&r1=191313&r2=191314&view=diff ============================================================================== --- struts/taglib/trunk/doc/userGuide/struts-html.xml (original) +++ struts/taglib/trunk/doc/userGuide/struts-html.xml Sat Jun 18 17:27:26 2005 @@ -1625,13 +1625,15 @@ <attribute> <name>action</name> - <required>true</required> + <required>false</required> <rtexprvalue>true</rtexprvalue> <info> <p>The URL to which this form will be submitted. This value is also used to select the ActionMapping we are assumed to be processing, from which we can identify - the appropriate form bean and scope.</p> + the appropriate form bean and scope. If a value is not + provided, the original URI (servletPath) for the request is + used. </p> <p>If you are using extension mapping for selecting the controller servlet, this value should be equal to the Modified: struts/taglib/trunk/src/java/org/apache/struts/taglib/html/FormTag.java URL: http://svn.apache.org/viewcvs/struts/taglib/trunk/src/java/org/apache/struts/taglib/html/FormTag.java?rev=191314&r1=191313&r2=191314&view=diff ============================================================================== --- struts/taglib/trunk/src/java/org/apache/struts/taglib/html/FormTag.java (original) +++ struts/taglib/trunk/src/java/org/apache/struts/taglib/html/FormTag.java Sat Jun 18 17:27:26 2005 @@ -747,6 +747,15 @@ * @exception JspException if a required value cannot be looked up */ protected void lookup() throws JspException { + + // If the action is not specified, use the original request uri + if (this.action == null) { + HttpServletRequest request = + (HttpServletRequest) pageContext.getRequest(); + String uri = (String)request.getAttribute(Globals.ORIGINAL_URI_KEY); + setAction(uri); + + } // Look up the module configuration information we need moduleConfig = TagUtils.getInstance().getModuleConfig(pageContext); --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]