001: /*
002: * $Id: File.java 497654 2007-01-19 00:21:57Z rgielen $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.components;
022:
023: import javax.servlet.http.HttpServletRequest;
024: import javax.servlet.http.HttpServletResponse;
025:
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028: import org.apache.struts2.views.annotations.StrutsTag;
029: import org.apache.struts2.views.annotations.StrutsTagAttribute;
030:
031: import com.opensymphony.xwork2.util.ValueStack;
032:
033: /**
034: * <!-- START SNIPPET: javadoc -->
035: * Renders an HTML file input element.
036: * <!-- END SNIPPET: javadoc -->
037: *
038: * <p/> <b>Examples</b>
039: *
040: * <pre>
041: * <!-- START SNIPPET: example -->
042: * <s:file name="anUploadFile" accept="text/*" />
043: * <s:file name="anohterUploadFIle" accept="text/html,text/plain" />
044: * <!-- END SNIPPET: example -->
045: * </pre>
046: *
047: */
048: @StrutsTag(name="file",tldTagClass="org.apache.struts2.views.jsp.ui.FileTag",description="Render a file input field")
049: public class File extends UIBean {
050: private final static Log log = LogFactory.getLog(File.class);
051:
052: final public static String TEMPLATE = "file";
053:
054: protected String accept;
055: protected String size;
056:
057: public File(ValueStack stack, HttpServletRequest request,
058: HttpServletResponse response) {
059: super (stack, request, response);
060: }
061:
062: protected String getDefaultTemplate() {
063: return TEMPLATE;
064: }
065:
066: public void evaluateParams() {
067: super .evaluateParams();
068:
069: Form form = (Form) findAncestor(Form.class);
070: if (form != null) {
071: String encType = (String) form.getParameters().get(
072: "enctype");
073: if (!"multipart/form-data".equals(encType)) {
074: // uh oh, this isn't good! Let's warn the developer
075: log
076: .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!");
077: }
078:
079: String method = (String) form.getParameters().get("method");
080: if (!"post".equalsIgnoreCase(method)) {
081: // uh oh, this isn't good! Let's warn the developer
082: log
083: .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!");
084: }
085: }
086:
087: if (accept != null) {
088: addParameter("accept", findString(accept));
089: }
090:
091: if (size != null) {
092: addParameter("size", findString(size));
093: }
094: }
095:
096: @StrutsTagAttribute(description="HTML accept attribute to indicate accepted file mimetypes")
097: public void setAccept(String accept) {
098: this .accept = accept;
099: }
100:
101: @StrutsTagAttribute(description="HTML size attribute",required=false,type="Integer")
102: public void setSize(String size) {
103: this.size = size;
104: }
105: }
|