View Javadoc

    1   /*
    2    * $Id: File.java 497654 2007-01-19 00:21:57Z rgielen $
    3    *
    4    * Licensed to the Apache Software Foundation (ASF) under one
    5    * or more contributor license agreements.  See the NOTICE file
    6    * distributed with this work for additional information
    7    * regarding copyright ownership.  The ASF licenses this file
    8    * to you under the Apache License, Version 2.0 (the
    9    * "License"); you may not use this file except in compliance
    10   * with the License.  You may obtain a copy of the License at
    11   *
    12   *  http://www.apache.org/licenses/LICENSE-2.0
    13   *
    14   * Unless required by applicable law or agreed to in writing,
    15   * software distributed under the License is distributed on an
    16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    17   * KIND, either express or implied.  See the License for the
    18   * specific language governing permissions and limitations
    19   * under the License.
    20   */
    21  package org.apache.struts2.components;
    22
    23  import javax.servlet.http.HttpServletRequest;
    24  import javax.servlet.http.HttpServletResponse;
    25
    26  import org.apache.commons.logging.Log;
    27  import org.apache.commons.logging.LogFactory;
    28  import org.apache.struts2.views.annotations.StrutsTag;
    29  import org.apache.struts2.views.annotations.StrutsTagAttribute;
    30
    31  import com.opensymphony.xwork2.util.ValueStack;
    32
    33  /***
    34   * <!-- START SNIPPET: javadoc -->
    35   * Renders an HTML file input element.
    36   * <!-- END SNIPPET: javadoc -->
    37   *
    38   * <p/> <b>Examples</b>
    39   *
    40   * <pre>
    41   * <!-- START SNIPPET: example -->
    42   * &lt;s:file name="anUploadFile" accept="text/*" /&gt;
    43   * &lt;s:file name="anohterUploadFIle" accept="text/html,text/plain" /&gt;
    44   * <!-- END SNIPPET: example -->
    45   * </pre>
    46   *
    47   */
    48  @StrutsTag(name="file", tldTagClass="org.apache.struts2.views.jsp.ui.FileTag", description="Render a file input field")
    49  public class File extends UIBean {
    50      private final static Log log = LogFactory.getLog(File.class);
    51
    52      final public static String TEMPLATE = "file";
    53
    54      protected String accept;
    55      protected String size;
    56
    57      public File(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
    58          super(stack, request, response);
    59      }
    60
    61      protected String getDefaultTemplate() {
    62          return TEMPLATE;
    63      }
    64
    65      public void evaluateParams() {
    66          super.evaluateParams();
    67
    68          Form form = (Form) findAncestor(Form.class);
    69          if (form != null) {
    70              String encType = (String) form.getParameters().get("enctype");
    71              if (!"multipart/form-data".equals(encType)) {
    72                  // uh oh, this isn't good! Let's warn the developer
    73                  log.warn("Struts has detected a file upload UI tag (s:file) being used without a form set to enctype 'multipart/form-data'. This is probably an error!");
    74              }
    75
    76              String method = (String) form.getParameters().get("method");
    77              if (!"post".equalsIgnoreCase(method)) {
    78                  // uh oh, this isn't good! Let's warn the developer
    79                  log.warn("Struts has detected a file upload UI tag (s:file) being used without a form set to method 'POST'. This is probably an error!");
    80              }
    81          }
    82
    83          if (accept != null) {
    84              addParameter("accept", findString(accept));
    85          }
    86
    87          if (size != null) {
    88              addParameter("size", findString(size));
    89          }
    90      }
    91
    92      @StrutsTagAttribute(description="HTML accept attribute to indicate accepted file mimetypes")
    93      public void setAccept(String accept) {
    94          this.accept = accept;
    95      }
    96
    97      @StrutsTagAttribute(description="HTML size attribute", required=false, type="Integer")
    98      public void setSize(String size) {
    99          this.size = size;
    100     }
    101 }