Save This Page
Home » struts-2.1.8.1-src » org.apache » struts2 » components » [javadoc | source]
    1   /*
    2    * $Id: File.java 651946 2008-04-27 13:41:38Z apetrelli $
    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   
   22   package org.apache.struts2.components;
   23   
   24   import javax.servlet.http.HttpServletRequest;
   25   import javax.servlet.http.HttpServletResponse;
   26   
   27   import org.apache.struts2.views.annotations.StrutsTag;
   28   import org.apache.struts2.views.annotations.StrutsTagAttribute;
   29   
   30   import com.opensymphony.xwork2.util.ValueStack;
   31   import com.opensymphony.xwork2.util.logging.Logger;
   32   import com.opensymphony.xwork2.util.logging.LoggerFactory;
   33   
   34   /**
   35    * <!-- START SNIPPET: javadoc -->
   36    * Renders an HTML file input element.
   37    * <!-- END SNIPPET: javadoc -->
   38    *
   39    * <p/> <b>Examples</b>
   40    *
   41    * <pre>
   42    * <!-- START SNIPPET: example -->
   43    * &lt;s:file name="anUploadFile" accept="text/*" /&gt;
   44    * &lt;s:file name="anohterUploadFIle" accept="text/html,text/plain" /&gt;
   45    * <!-- END SNIPPET: example -->
   46    * </pre>
   47    *
   48    */
   49   @StrutsTag(
   50       name="file",
   51       tldTagClass="org.apache.struts2.views.jsp.ui.FileTag",
   52       description="Render a file input field",
   53       allowDynamicAttributes=true)
   54   public class File extends UIBean {
   55       private final static Logger LOG = LoggerFactory.getLogger(File.class);
   56   
   57       final public static String TEMPLATE = "file";
   58   
   59       protected String accept;
   60       protected String size;
   61   
   62       public File(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
   63           super(stack, request, response);
   64       }
   65   
   66       protected String getDefaultTemplate() {
   67           return TEMPLATE;
   68       }
   69   
   70       public void evaluateParams() {
   71           super.evaluateParams();
   72   
   73           Form form = (Form) findAncestor(Form.class);
   74           if (form != null) {
   75               String encType = (String) form.getParameters().get("enctype");
   76               if (!"multipart/form-data".equals(encType)) {
   77                   // uh oh, this isn't good! Let's warn the developer
   78                   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!");
   79               }
   80   
   81               String method = (String) form.getParameters().get("method");
   82               if (!"post".equalsIgnoreCase(method)) {
   83                   // uh oh, this isn't good! Let's warn the developer
   84                   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!");
   85               }
   86           }
   87   
   88           if (accept != null) {
   89               addParameter("accept", findString(accept));
   90           }
   91   
   92           if (size != null) {
   93               addParameter("size", findString(size));
   94           }
   95       }
   96   
   97       @StrutsTagAttribute(description="HTML accept attribute to indicate accepted file mimetypes")
   98       public void setAccept(String accept) {
   99           this.accept = accept;
  100       }
  101   
  102       @StrutsTagAttribute(description="HTML size attribute", required=false, type="Integer")
  103       public void setSize(String size) {
  104           this.size = size;
  105       }
  106   }

Save This Page
Home » struts-2.1.8.1-src » org.apache » struts2 » components » [javadoc | source]