You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by cr...@apache.org on 2001/02/20 06:20:13 UTC

cvs commit: jakarta-struts/web/example logon.jsp registration.jsp subscription.jsp

craigmcc    01/02/19 21:20:13

  Modified:    src/doc  struts-html.xml
               src/share/org/apache/struts/action Action.java
                        ActionServlet.java
               src/share/org/apache/struts/taglib/html FormTag.java
                        LocalStrings.properties
               web/example logon.jsp registration.jsp subscription.jsp
  Log:
  Fix the <html:form action="xxx"> attribute to correctly work for both
  extension mapping and path mapping of the controller servlet -- at least
  as long as your servlet container correctly allows the following to work:
  
  	InputStream is =
  	  getServletContext().getResourceAsStream("/WEB-INF/web.xml");
  
  like it is supposed to.
  
  The new recommended approach is to set the action attribute of <html:form>
  to exactly the path of the Action to which you wish the form to be
  submitted.  The <html:form> tag will understand how the action servlet is
  being mapped, and will construct an appropriate URL accordingly.
  
  For backwards compatibility, the existence of the extension-mapping
  extension on an action attribute still works, so either of the following
  tags would work in the Struts example application:
  
  	<html:form action="/logon.do" ...>
  
  	<html:form action="/logon" ...>
  
  The latter path will also work if the controller servlet is path-mapped,
  but you will need to adjust the paths for several of the <forward>
  elements in this case.
  
  Submitted by:  Marcus Ahnve <ma...@lecando.com>
  PR: Bugzilla #587
  
  Revision  Changes    Path
  1.15      +2 -2      jakarta-struts/src/doc/struts-html.xml
  
  Index: struts-html.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-struts/src/doc/struts-html.xml,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- struts-html.xml	2001/02/20 02:58:59	1.14
  +++ struts-html.xml	2001/02/20 05:20:05	1.15
  @@ -1018,8 +1018,8 @@
                           <p>If you are using extension mapping for selecting the
                           controller servlet, this value should be equal to the
                           <code>path</code> attribute of the corresponding
  -                        <code>&lt;action&gt;</code> element, plus the correct
  -                        file extension suffix.</p>
  +                        <code>&lt;action&gt;</code> element, optionally
  +                        followed by the correct extension suffix.</p>
   
                           <p>If you are using path mapping to select the
                           controller servlet, this value should be exactly equal
  
  
  
  1.17      +14 -4     jakarta-struts/src/share/org/apache/struts/action/Action.java
  
  Index: Action.java
  ===================================================================
  RCS file: /home/cvs/jakarta-struts/src/share/org/apache/struts/action/Action.java,v
  retrieving revision 1.16
  retrieving revision 1.17
  diff -u -r1.16 -r1.17
  --- Action.java	2001/01/11 00:16:56	1.16
  +++ Action.java	2001/02/20 05:20:07	1.17
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/action/Action.java,v 1.16 2001/01/11 00:16:56 craigmcc Exp $
  - * $Revision: 1.16 $
  - * $Date: 2001/01/11 00:16:56 $
  + * $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/action/Action.java,v 1.17 2001/02/20 05:20:07 craigmcc Exp $
  + * $Revision: 1.17 $
  + * $Date: 2001/02/20 05:20:07 $
    *
    * ====================================================================
    *
  @@ -106,7 +106,7 @@
    * by this Action.
    *
    * @author Craig R. McClanahan
  - * @version $Revision: 1.16 $ $Date: 2001/01/11 00:16:56 $
  + * @version $Revision: 1.17 $ $Date: 2001/02/20 05:20:07 $
    */
   
   public class Action {
  @@ -207,6 +207,16 @@
        */
       public static final String MULTIPART_KEY =
           "org.apache.struts.action.mapping.multipartclass";
  +
  +
  +    /**
  +     * The context attributes key under which we store the mapping defined
  +     * for our controller serlet, which will be either a path-mapped pattern
  +     * (<code>/action/*</code>) or an extension mapped pattern
  +     * (<code>*.do</code>).
  +     */
  +    public static final String SERVLET_KEY =
  +        "org.apache.struts.action.SERVLET_MAPPING";
   
   
       /**
  
  
  
  1.59      +83 -4     jakarta-struts/src/share/org/apache/struts/action/ActionServlet.java
  
  Index: ActionServlet.java
  ===================================================================
  RCS file: /home/cvs/jakarta-struts/src/share/org/apache/struts/action/ActionServlet.java,v
  retrieving revision 1.58
  retrieving revision 1.59
  diff -u -r1.58 -r1.59
  --- ActionServlet.java	2001/02/14 00:19:46	1.58
  +++ ActionServlet.java	2001/02/20 05:20:07	1.59
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/action/ActionServlet.java,v 1.58 2001/02/14 00:19:46 craigmcc Exp $
  - * $Revision: 1.58 $
  - * $Date: 2001/02/14 00:19:46 $
  + * $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/action/ActionServlet.java,v 1.59 2001/02/20 05:20:07 craigmcc Exp $
  + * $Revision: 1.59 $
  + * $Date: 2001/02/20 05:20:07 $
    *
    * ====================================================================
    *
  @@ -230,7 +230,7 @@
    * </ul>
    *
    * @author Craig R. McClanahan
  - * @version $Revision: 1.58 $ $Date: 2001/02/14 00:19:46 $
  + * @version $Revision: 1.59 $ $Date: 2001/02/20 05:20:07 $
    */
   
   public class ActionServlet
  @@ -369,6 +369,20 @@
   
   
       /**
  +     * The URL pattern to which we are mapped in our web application
  +     * deployment descriptor.
  +     */
  +    protected String servletMapping = null;
  +
  +
  +    /**
  +     * The servlet name under which we are registered in our web application
  +     * deployment descriptor.
  +     */
  +    protected String servletName = null;
  +
  +
  +    /**
        * Are we using the new configuration file format?
        */
       protected boolean validate = true;
  @@ -452,6 +466,7 @@
           initUpload();
           initDataSources();
   	initOther();
  +        initServlet();
   
       }
   
  @@ -548,6 +563,26 @@
   
   
       /**
  +     * Remember a servlet mapping from our web application deployment
  +     * descriptor, if it is for this servlet.
  +     *
  +     * @param servletName The name of the servlet being mapped
  +     * @param urlPattern The URL pattern to which this servlet is mapped
  +     */
  +    public void addServletMapping(String servletName, String urlPattern) {
  +
  +        if (debug >= 1)
  +            log("Process servletName=" + servletName +
  +                ", urlPattern=" + urlPattern);
  +        if (servletName == null)
  +            return;
  +        if (servletName.equals(this.servletName))
  +            this.servletMapping = urlPattern;
  +
  +    }
  +
  +
  +    /**
        * Return a JDBC data source associated with this application, if any.
        *
        * @param key The servlet context attribute key under which this data
  @@ -1327,6 +1362,50 @@
       }
       
       
  +    /**
  +     * Initialize the servlet mapping under which our controller servlet
  +     * is being accessed.  This will be used in the <code>&html:form&gt;</code>
  +     * tag to generate correct destination URLs for form submissions.
  +     */
  +    protected void initServlet() throws ServletException {
  +
  +        // Remember our servlet name
  +        this.servletName = getServletConfig().getServletName();
  +
  +        // Prepare a Digester to scan the web application deployment descriptor
  +        Digester digester = new Digester();
  +        digester.push(this);
  +        digester.setDebug(this.debug);
  +        digester.setValidating(false);
  +        digester.addCallMethod("web-app/servlet-mapping",
  +                               "addServletMapping", 2);
  +        digester.addCallParam("web-app/servlet-mapping/servlet-name", 0);
  +        digester.addCallParam("web-app/servlet-mapping/url-pattern", 1);
  +
  +        // Process the web application deployment descriptor
  +        InputStream input= null;
  +        try {
  +            input =
  +                getServletContext().getResourceAsStream("/WEB-INF/web.xml");
  +            digester.parse(input);
  +        } catch (Throwable e) {
  +            log(internal.getMessage("configWebXml"), e);
  +        } finally {
  +            if (input != null)
  +                input = null;
  +        }
  +
  +        // Record a servlet context attribute (if appropriate)
  +        if (debug >= 1)
  +            log("Mapping for servlet '" + servletName + "' = '" +
  +                servletMapping + "'");
  +        if (servletMapping != null)
  +            getServletContext().setAttribute(Action.SERVLET_KEY,
  +                                             servletMapping);
  +
  +    }
  +
  +
       /**
        * Initialize upload parameters and "bufferSize", "multipartClass",
        * "maxFileSize", "tempDir"
  
  
  
  1.5       +31 -14    jakarta-struts/src/share/org/apache/struts/taglib/html/FormTag.java
  
  Index: FormTag.java
  ===================================================================
  RCS file: /home/cvs/jakarta-struts/src/share/org/apache/struts/taglib/html/FormTag.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- FormTag.java	2001/01/27 23:21:08	1.4
  +++ FormTag.java	2001/02/20 05:20:09	1.5
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/taglib/html/FormTag.java,v 1.4 2001/01/27 23:21:08 craigmcc Exp $
  - * $Revision: 1.4 $
  - * $Date: 2001/01/27 23:21:08 $
  + * $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/taglib/html/FormTag.java,v 1.5 2001/02/20 05:20:09 craigmcc Exp $
  + * $Revision: 1.5 $
  + * $Date: 2001/02/20 05:20:09 $
    *
    * ====================================================================
    *
  @@ -85,7 +85,7 @@
    * properties correspond to the various fields of the form.
    *
    * @author Craig R. McClanahan
  - * @version $Revision: 1.4 $ $Date: 2001/01/27 23:21:08 $
  + * @version $Revision: 1.5 $ $Date: 2001/02/20 05:20:09 $
    */
   
   public class FormTag extends TagSupport {
  @@ -632,22 +632,39 @@
   
   
       /**
  -     * Return the form action converted into a server-relative URL.  The
  -     * URL string is composed of the following pieces:
  -     * <ul>
  -     * <li>The context path of this web application.</li>
  -     * <li>If the <code>action</code> property value does not start with
  -     *     a slash, a slash is inserted.</li>
  -     * <li>The <code>action</code> property value.</li>
  +     * Return the form action converted into a server-relative URL.
        */
       protected String getActionMappingURL() {
   
           HttpServletRequest request =
               (HttpServletRequest) pageContext.getRequest();
           StringBuffer value = new StringBuffer(request.getContextPath());
  -        if (!action.startsWith("/"))
  -            value.append("/");
  -        value.append(action);
  +        
  +        // Use our servlet mapping, if one is specified
  +        String servletMapping = (String)
  +            pageContext.getAttribute(Action.SERVLET_KEY,
  +                                     PageContext.APPLICATION_SCOPE);
  +        if (servletMapping != null) {
  +            String actionMapping = getActionMappingName();
  +            if (servletMapping.startsWith("*.")) {
  +                value.append(actionMapping);
  +                value.append(servletMapping.substring(1));
  +            } else if (servletMapping.endsWith("/*")) {
  +                value.append(servletMapping.substring
  +                             (0, servletMapping.length() - 2));
  +                value.append(actionMapping);
  +            }
  +        }
  +
  +        // Otherwise, assume extension mapping is in use and extension is
  +        // already included in the action property
  +        else {
  +            if (!action.startsWith("/"))
  +                value.append("/");
  +            value.append(action);
  +        }
  +
  +        // Return the completed value
           return (value.toString());
   
       }
  
  
  
  1.7       +1 -0      jakarta-struts/src/share/org/apache/struts/taglib/html/LocalStrings.properties
  
  Index: LocalStrings.properties
  ===================================================================
  RCS file: /home/cvs/jakarta-struts/src/share/org/apache/struts/taglib/html/LocalStrings.properties,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- LocalStrings.properties	2001/02/20 00:18:53	1.6
  +++ LocalStrings.properties	2001/02/20 05:20:09	1.7
  @@ -1,5 +1,6 @@
   common.io=Encountered input/output error: {0}
   enumerateTag.enumeration=Cannot create enumeration for {0}
  +errorsTag.errors=Cannot process ActionErrors instance of class {0}
   formTag.collections=Cannot find ActionMappings or ActionFormBeans collection
   formTag.create=Exception creating bean of class {0}: {1}
   formTag.formBean=Cannot retrieve definition for form bean {0}
  
  
  
  1.17      +1 -1      jakarta-struts/web/example/logon.jsp
  
  Index: logon.jsp
  ===================================================================
  RCS file: /home/cvs/jakarta-struts/web/example/logon.jsp,v
  retrieving revision 1.16
  retrieving revision 1.17
  diff -u -r1.16 -r1.17
  --- logon.jsp	2001/02/14 00:39:52	1.16
  +++ logon.jsp	2001/02/20 05:20:11	1.17
  @@ -11,7 +11,7 @@
   
   <html:errors/>
   
  -<html:form action="/logon.do" focus="username">
  +<html:form action="/logon" focus="username">
   <table border="0" width="100%">
   
     <tr>
  
  
  
  1.18      +1 -1      jakarta-struts/web/example/registration.jsp
  
  Index: registration.jsp
  ===================================================================
  RCS file: /home/cvs/jakarta-struts/web/example/registration.jsp,v
  retrieving revision 1.17
  retrieving revision 1.18
  diff -u -r1.17 -r1.18
  --- registration.jsp	2001/01/27 23:31:15	1.17
  +++ registration.jsp	2001/02/20 05:20:11	1.18
  @@ -24,7 +24,7 @@
   
   <html:errors/>
   
  -<html:form action="/saveRegistration.do">
  +<html:form action="/saveRegistration">
   <html:hidden property="action"/>
   <table border="0" width="100%">
   
  
  
  
  1.23      +1 -1      jakarta-struts/web/example/subscription.jsp
  
  Index: subscription.jsp
  ===================================================================
  RCS file: /home/cvs/jakarta-struts/web/example/subscription.jsp,v
  retrieving revision 1.22
  retrieving revision 1.23
  diff -u -r1.22 -r1.23
  --- subscription.jsp	2001/01/27 23:31:16	1.22
  +++ subscription.jsp	2001/02/20 05:20:11	1.23
  @@ -33,7 +33,7 @@
   
   <html:errors/>
   
  -<html:form action="/saveSubscription.do" focus="host">
  +<html:form action="/saveSubscription" focus="host">
   <html:hidden property="action"/>
   <table border="0" width="100%">