You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@beehive.apache.org by ri...@apache.org on 2005/05/26 19:41:42 UTC

svn commit: r178657 - in /incubator/beehive/trunk/samples/netui-samples: ./ WEB-INF/src/org/apache/beehive/samples/netui/resources/fileupload/ fileupload/

Author: rich
Date: Thu May 26 10:41:41 2005
New Revision: 178657

URL: http://svn.apache.org/viewcvs?rev=178657&view=rev
Log:
Added a file upload sample.

tests: build.dist (WinXP)
BB, run.tests: linux (linux)


Added:
    incubator/beehive/trunk/samples/netui-samples/WEB-INF/src/org/apache/beehive/samples/netui/resources/fileupload/
    incubator/beehive/trunk/samples/netui-samples/WEB-INF/src/org/apache/beehive/samples/netui/resources/fileupload/messages.properties   (with props)
    incubator/beehive/trunk/samples/netui-samples/fileupload/
    incubator/beehive/trunk/samples/netui-samples/fileupload/Controller.java   (with props)
    incubator/beehive/trunk/samples/netui-samples/fileupload/index.jsp   (with props)
    incubator/beehive/trunk/samples/netui-samples/fileupload/results.jsp   (with props)
    incubator/beehive/trunk/samples/netui-samples/fileupload/success.jsp   (with props)
Modified:
    incubator/beehive/trunk/samples/netui-samples/index.jsp

Added: incubator/beehive/trunk/samples/netui-samples/WEB-INF/src/org/apache/beehive/samples/netui/resources/fileupload/messages.properties
URL: http://svn.apache.org/viewcvs/incubator/beehive/trunk/samples/netui-samples/WEB-INF/src/org/apache/beehive/samples/netui/resources/fileupload/messages.properties?rev=178657&view=auto
==============================================================================
--- incubator/beehive/trunk/samples/netui-samples/WEB-INF/src/org/apache/beehive/samples/netui/resources/fileupload/messages.properties (added)
+++ incubator/beehive/trunk/samples/netui-samples/WEB-INF/src/org/apache/beehive/samples/netui/resources/fileupload/messages.properties Thu May 26 10:41:41 2005
@@ -0,0 +1,7 @@
+# Messages for the /fileupload/Controller.java example
+
+errors.filerequired=Please provide a file to upload.
+errors.fileextension=Only files with names ending in ".txt" or ".html" are allowed.
+
+error.prefix=<span style="color:red">
+error.suffix=</span>

Propchange: incubator/beehive/trunk/samples/netui-samples/WEB-INF/src/org/apache/beehive/samples/netui/resources/fileupload/messages.properties
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/beehive/trunk/samples/netui-samples/fileupload/Controller.java
URL: http://svn.apache.org/viewcvs/incubator/beehive/trunk/samples/netui-samples/fileupload/Controller.java?rev=178657&view=auto
==============================================================================
--- incubator/beehive/trunk/samples/netui-samples/fileupload/Controller.java (added)
+++ incubator/beehive/trunk/samples/netui-samples/fileupload/Controller.java Thu May 26 10:41:41 2005
@@ -0,0 +1,135 @@
+/*
+ * Copyright 2004-2005 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * $Header:$
+ */
+package fileupload;
+
+import java.io.Serializable;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+
+import javax.servlet.http.HttpServletRequest;
+
+import org.apache.beehive.netui.pageflow.annotations.Jpf;
+import org.apache.beehive.netui.pageflow.Forward;
+import org.apache.beehive.netui.pageflow.PageFlowController;
+import org.apache.beehive.netui.pageflow.Validatable;
+
+import org.apache.struts.upload.FormFile;
+import org.apache.struts.action.ActionMapping;
+import org.apache.struts.action.ActionError;
+import org.apache.struts.action.ActionMessages;
+
+
+/**
+ * Demonstration of file upload to a page flow action.
+ */
+@Jpf.Controller(
+    simpleActions={
+        @Jpf.SimpleAction(name="begin",path="index.jsp")
+    },
+    messageBundles={
+        @Jpf.MessageBundle(
+            bundlePath="org.apache.beehive.samples.netui.resources.fileupload.messages"
+        )
+    },
+
+    // For security, multipart request handling is disabled by default.  It can be enabled on a
+    // per-pageflow basis (as it is here), or in WEB-INF/beehive-netui-config.xml, using the
+    // <multipart-handler> element in <pageflow-config>.
+    multipartHandler=Jpf.MultipartHandler.memory
+)
+public class Controller extends PageFlowController
+{
+    @Jpf.Action(
+        forwards={
+            @Jpf.Forward(
+                name="success",
+                path="results.jsp",
+                actionOutputs={
+                    @Jpf.ActionOutput(name="uploadForm", type=UploadForm.class, required=true)
+                }
+            )
+        },
+        validationErrorForward=@Jpf.Forward(name="failure", path="index.jsp")
+    )
+    public Forward upload(UploadForm form)
+    {
+        // Add the UploadForm as an "action output" for the results page.
+        Forward fwd = new Forward("success");
+        fwd.addActionOutput("uploadForm", form);
+        return fwd;
+    }
+
+    public static class UploadForm
+        implements Serializable, Validatable
+    {
+        private String _label;
+        private FormFile _file;
+
+
+        public void setLabel( String label )
+        {
+            _label = label;
+        }
+
+        public String getLabel()
+        {
+            return _label;
+        }
+
+        public void setFile( FormFile file )
+        {
+            _file = file;
+        }
+
+        /**
+         * The submitted file (required) ends up as this FormFile object.
+         */
+        @Jpf.ValidatableProperty(
+            validateRequired=@Jpf.ValidateRequired(messageKey="errors.filerequired")
+        )
+        public FormFile getFile()
+        {
+            return _file;
+        }
+        
+        /**
+         * In addition to any declarative validation that is defined per-property, this method
+         * ensures that the submitted file is an HTML file or a text file.
+         */
+        public void validate(ActionMapping mapping, HttpServletRequest req, ActionMessages errors)
+        {
+            if (_file != null)
+            {
+                String fileName = _file.getFileName();
+                if (! fileName.endsWith(".html") && ! fileName.endsWith(".txt"))
+                {
+                    errors.add("file", new ActionError("errors.fileextension"));
+                }
+            }
+        }
+        
+        /**
+         * This getter is bound to from results.jsp.
+         */
+        public String getFileDataString()
+            throws FileNotFoundException, IOException
+        {
+            return new String(_file.getFileData());
+        }
+    }
+}

Propchange: incubator/beehive/trunk/samples/netui-samples/fileupload/Controller.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/beehive/trunk/samples/netui-samples/fileupload/index.jsp
URL: http://svn.apache.org/viewcvs/incubator/beehive/trunk/samples/netui-samples/fileupload/index.jsp?rev=178657&view=auto
==============================================================================
--- incubator/beehive/trunk/samples/netui-samples/fileupload/index.jsp (added)
+++ incubator/beehive/trunk/samples/netui-samples/fileupload/index.jsp Thu May 26 10:41:41 2005
@@ -0,0 +1,78 @@
+<%--
+   Copyright 2004-2005 The Apache Software Foundation.
+
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+   
+       http://www.apache.org/licenses/LICENSE-2.0
+   
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+  
+   $Header:$
+--%>
+<%@ page language="java" contentType="text/html;charset=UTF-8"%>
+<%@ taglib prefix="netui" uri="http://beehive.apache.org/netui/tags-html-1.0"%>
+<%@ taglib prefix="netui-data" uri="http://beehive.apache.org/netui/tags-databinding-1.0"%>
+<%@ taglib prefix="netui-template" uri="http://beehive.apache.org/netui/tags-template-1.0"%>
+
+<netui-template:template templatePage="/resources/template/template.jsp">
+
+    <netui-template:setAttribute name="sampleTitle" value="File Upload"/>
+
+    <netui-template:section name="main">
+        <p>
+            The <code>&lt;netui:fileUpload&gt;</code> tag below binds to the <code>file</code>
+            property on the action's form bean.  The property is of type
+            <code>org.apache.struts.upload.FormFile</code>, which contains information about the
+            submitted file as well as the file data itself.
+        </p>
+        <p>
+            The <code>enctype</code> on this <code>&lt;netui:form&gt;</code> tag is
+            "multipart/form-data", which is required for uploading files.
+        </p>
+        <p>
+            Note that for security reasons, multipart request handing is <i>disabled</i> by
+            default.  To enable it (and to choose whether the temporary uploaded file lives in
+            memory or on disk), there are two options:
+            <ul>
+                <li>
+                    Set the <code>multipartHandler</code> attribute on the page flow's
+                    <code>@Jpf.Controller</code> annotation.  This enables/disables multipart
+                    handling for this page flow only.
+                </li>
+                <li>
+                    Set the <code>&lt;multipart-handler&gt;</code> within the
+                    <code>&lt;pageflow-config&gt;</code> element in
+                    WEB-INF/beehive-netui-config.xml.  This enables/disables multipart handling
+                    for all page flows in the webapp (and can be overridden by the
+                    <code>multipartHandler</code> annotation attribute as above).
+                </li>
+            </ul>
+        </p>
+        <h3>Upload a File</h3>
+        <netui:form action="upload" enctype="multipart/form-data">
+            <table>
+                <tr>
+                    <td>File:</td>
+                    <td>
+                        <netui:fileUpload dataSource="actionForm.file" readonly="false" size="40"/>
+                        <netui:error key="file"/>
+                    </td>
+                </tr>
+                <tr>
+                    <td>Label:</td>
+                    <td>
+                        <netui:textBox dataSource="actionForm.label"/>
+                    </td>
+                </tr>
+            </table>
+            <netui:button value="submit"/>
+        </netui:form>
+    </netui-template:section>
+
+</netui-template:template>

Propchange: incubator/beehive/trunk/samples/netui-samples/fileupload/index.jsp
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/beehive/trunk/samples/netui-samples/fileupload/results.jsp
URL: http://svn.apache.org/viewcvs/incubator/beehive/trunk/samples/netui-samples/fileupload/results.jsp?rev=178657&view=auto
==============================================================================
--- incubator/beehive/trunk/samples/netui-samples/fileupload/results.jsp (added)
+++ incubator/beehive/trunk/samples/netui-samples/fileupload/results.jsp Thu May 26 10:41:41 2005
@@ -0,0 +1,48 @@
+<%--
+   Copyright 2004-2005 The Apache Software Foundation.
+
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+   
+       http://www.apache.org/licenses/LICENSE-2.0
+   
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+  
+   $Header:$
+--%>
+<%@ page language="java" contentType="text/html;charset=UTF-8"%>
+<%@ taglib prefix="netui" uri="http://beehive.apache.org/netui/tags-html-1.0"%>
+<%@ taglib prefix="netui-data" uri="http://beehive.apache.org/netui/tags-databinding-1.0"%>
+<%@ taglib prefix="netui-template" uri="http://beehive.apache.org/netui/tags-template-1.0"%>
+
+<netui-data:declarePageInput name="uploadForm" type="org.apache.struts.upload.FormFile" required="true"/>
+
+<netui-template:template templatePage="/resources/template/template.jsp">
+
+    <netui-template:setAttribute name="sampleTitle" value="File Upload"/>
+
+    <netui-template:section name="main">
+        <i>Label:</i> ${pageInput.uploadForm.label}
+        <br/>
+        <br/>
+        <i>File Contents:</i>
+            <table border="1">
+                <tr>
+                    <td>
+                        <pre>${pageInput.uploadForm.fileDataString}</pre>
+                    </td>
+                </tr>
+            </table>
+        <br/>
+        <br/>
+        <br/>
+        <netui:anchor action="begin">start over</netui:anchor>
+
+    </netui-template:section>
+
+</netui-template:template>

Propchange: incubator/beehive/trunk/samples/netui-samples/fileupload/results.jsp
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/beehive/trunk/samples/netui-samples/fileupload/success.jsp
URL: http://svn.apache.org/viewcvs/incubator/beehive/trunk/samples/netui-samples/fileupload/success.jsp?rev=178657&view=auto
==============================================================================
--- incubator/beehive/trunk/samples/netui-samples/fileupload/success.jsp (added)
+++ incubator/beehive/trunk/samples/netui-samples/fileupload/success.jsp Thu May 26 10:41:41 2005
@@ -0,0 +1,31 @@
+<%--
+   Copyright 2004-2005 The Apache Software Foundation.
+
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+   
+       http://www.apache.org/licenses/LICENSE-2.0
+   
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+  
+   $Header:$
+--%>
+<%@ page language="java" contentType="text/html;charset=UTF-8"%>
+<%@ taglib prefix="netui" uri="http://beehive.apache.org/netui/tags-html-1.0"%>
+<%@ taglib prefix="netui-data" uri="http://beehive.apache.org/netui/tags-databinding-1.0"%>
+<%@ taglib prefix="netui-template" uri="http://beehive.apache.org/netui/tags-template-1.0"%>
+
+<netui-template:template templatePage="/resources/template/template.jsp">
+  <netui-template:setAttribute name="sampleTitle" value="Validation"/>
+  <netui-template:section name="main">
+        <p>Validation successful.</p>
+        <p><netui:anchor action="begin">Start over</netui:anchor></p>
+  </netui-template:section>
+</netui-template:template>
+  
+

Propchange: incubator/beehive/trunk/samples/netui-samples/fileupload/success.jsp
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: incubator/beehive/trunk/samples/netui-samples/index.jsp
URL: http://svn.apache.org/viewcvs/incubator/beehive/trunk/samples/netui-samples/index.jsp?rev=178657&r1=178656&r2=178657&view=diff
==============================================================================
--- incubator/beehive/trunk/samples/netui-samples/index.jsp (original)
+++ incubator/beehive/trunk/samples/netui-samples/index.jsp Thu May 26 10:41:41 2005
@@ -40,6 +40,10 @@
           <dd>Demonstrates Page Flow declarative exception handling.</dd>
       </dl>
       <dl>
+          <dt><netui:anchor href="fileupload/Controller.jpf" value="File Upload"/></dt>
+          <dd>Demonstrates file upload to a page flow.</dd>
+      </dl>
+      <dl>
           <dt><netui:anchor href="lifecycle/normal/NormalPageFlow.jpf" value="Lifecycle"/></dt>
           <dd>Demonstrates the lifecycle (creation/destruction) of "normal" and "long-lived" page
               flows.  Also demonstrates inheritance of actions.</dd>