01: package com.opensymphony.webwork.components;
02:
03: import com.opensymphony.xwork.util.OgnlValueStack;
04:
05: import javax.servlet.http.HttpServletRequest;
06: import javax.servlet.http.HttpServletResponse;
07:
08: import org.apache.commons.logging.Log;
09: import org.apache.commons.logging.LogFactory;
10:
11: /**
12: * <!-- START SNIPPET: javadoc -->
13: * Renders an HTML file input element.
14: * <!-- END SNIPPET: javadoc -->
15: *
16: * <p/> <b>Examples</b>
17: *
18: * <pre>
19: * <!-- START SNIPPET: example -->
20: * <ww:file name="anUploadFile" accept="text/*" />
21: * <ww:file name="anohterUploadFIle" accept="text/html,text/plain" />
22: * <!-- END SNIPPET: example -->
23: * </pre>
24: *
25: * @author Patrick Lightbody
26: * @author Rene Gielen
27: * @version $Revision: 2468 $
28: * @since 2.2
29: *
30: * @ww.tag name="file" tld-body-content="JSP" tld-tag-class="com.opensymphony.webwork.views.jsp.ui.FileTag"
31: * description="Render a file input field"
32: */
33: public class File extends UIBean {
34: private final static Log log = LogFactory.getLog(File.class);
35:
36: final public static String TEMPLATE = "file";
37:
38: protected String accept;
39: protected String size;
40:
41: public File(OgnlValueStack stack, HttpServletRequest request,
42: HttpServletResponse response) {
43: super (stack, request, response);
44: }
45:
46: protected String getDefaultTemplate() {
47: return TEMPLATE;
48: }
49:
50: public void evaluateParams() {
51: super .evaluateParams();
52:
53: Form form = (Form) findAncestor(Form.class);
54: if (form != null) {
55: String encType = (String) form.getParameters().get(
56: "enctype");
57: if (!"multipart/form-data".equals(encType)) {
58: // uh oh, this isn't good! Let's warn the developer
59: log
60: .warn("WebWork has detected a file upload UI tag (ww:file) being used without a form set to enctype 'multipart/form-data'. This is probably an error!");
61: }
62:
63: String method = (String) form.getParameters().get("method");
64: if (!"post".equalsIgnoreCase(method)) {
65: // uh oh, this isn't good! Let's warn the developer
66: log
67: .warn("WebWork has detected a file upload UI tag (ww:file) being used without a form set to method 'POST'. This is probably an error!");
68: }
69: }
70:
71: if (accept != null) {
72: addParameter("accept", findString(accept));
73: }
74:
75: if (size != null) {
76: addParameter("size", findString(size));
77: }
78: }
79:
80: /**
81: * HTML accept attribute to indicate accepted file mimetypes
82: * @ww.tagattribute required="false"
83: */
84: public void setAccept(String accept) {
85: this .accept = accept;
86: }
87:
88: /**
89: * HTML size attribute
90: * @ww.tagattribute required="false" type="Integer"
91: */
92: public void setSize(String size) {
93: this.size = size;
94: }
95: }
|