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