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