1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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 * <s:file name="anUploadFile" accept="text/*" />
38 * <s:file name="anohterUploadFIle" accept="text/html,text/plain" />
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
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
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 }