View Javadoc

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