Skip to content
This repository was archived by the owner on Jan 30, 2019. It is now read-only.

Commit 150d2c7

Browse files
author
Arun Gupta
committed
Added a new sample for h:inputFile - traditional and Ajax way
Former-commit-id: 9363e9fa2d98ceabe45e8079d8e4d10518991d2c
1 parent 08046e0 commit 150d2c7

File tree

6 files changed

+199
-0
lines changed

6 files changed

+199
-0
lines changed

samples/jsf/file-upload/pom.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<artifactId>jsf</artifactId>
6+
<groupId>org.sample</groupId>
7+
<version>1.0-SNAPSHOT</version>
8+
<relativePath>../pom.xml</relativePath>
9+
</parent>
10+
11+
<groupId>org.glassfish.sample</groupId>
12+
<artifactId>file-upload</artifactId>
13+
<version>1.0-SNAPSHOT</version>
14+
<packaging>war</packaging>
15+
16+
<name>file-upload</name>
17+
</project>
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at packager/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
package org.glassfish.sample.flie.upload;
41+
42+
import java.io.BufferedReader;
43+
import java.io.IOException;
44+
import java.io.InputStreamReader;
45+
import javax.enterprise.context.RequestScoped;
46+
import javax.inject.Named;
47+
import javax.servlet.http.Part;
48+
49+
/**
50+
* @author Arun Gupta
51+
*/
52+
@Named
53+
@RequestScoped
54+
public class FileUploadBean {
55+
private Part file;
56+
private String text;
57+
58+
public Part getFile() {
59+
return file;
60+
}
61+
62+
public void setFile(Part file) {
63+
this.file = file;
64+
if (null != file) {
65+
try {
66+
BufferedReader reader = new BufferedReader(new InputStreamReader(file.getInputStream()));
67+
String string = reader.readLine();
68+
StringBuilder builder = new StringBuilder();
69+
while (string != null) {
70+
builder.append(string);
71+
string = reader.readLine();
72+
}
73+
text = builder.toString();
74+
} catch (IOException ex) {
75+
76+
}
77+
}
78+
}
79+
80+
public String getText() {
81+
return text;
82+
}
83+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://java.sun.com/xml/ns/javaee"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
5+
</beans>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
3+
<context-param>
4+
<param-name>javax.faces.PROJECT_STAGE</param-name>
5+
<param-value>Development</param-value>
6+
</context-param>
7+
<servlet>
8+
<servlet-name>Faces Servlet</servlet-name>
9+
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
10+
<load-on-startup>1</load-on-startup>
11+
</servlet>
12+
<servlet-mapping>
13+
<servlet-name>Faces Servlet</servlet-name>
14+
<url-pattern>/faces/*</url-pattern>
15+
</servlet-mapping>
16+
<session-config>
17+
<session-timeout>
18+
30
19+
</session-timeout>
20+
</session-config>
21+
<welcome-file-list>
22+
<welcome-file>faces/index.xhtml</welcome-file>
23+
</welcome-file-list>
24+
</web-app>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?xml version='1.0' encoding='UTF-8' ?>
2+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3+
<html xmlns="http://www.w3.org/1999/xhtml"
4+
xmlns:h="http://java.sun.com/jsf/html"
5+
xmlns:f="http://java.sun.com/jsf/core">
6+
<h:head>
7+
<title>File Upload</title>
8+
<script type="text/javascript">
9+
var onload_cb = function() {
10+
}
11+
var statusUpdate = function statusUpdate(data) {
12+
var statusArea = document.getElementById("fileUploadBean.text");
13+
var text = statusArea.value;
14+
text = text + "Name: " + data.source.id;
15+
if (data.type === "event") {
16+
text = text + " Event: " + data.status + " ";
17+
}
18+
statusArea.value = text;
19+
}
20+
</script>
21+
</h:head>
22+
<h:body>
23+
<h1>Ajax File Upload</h1>
24+
25+
<h:form>
26+
<h:commandButton value="Traditional File Upload" action="ajax-upload"/><p/>
27+
</h:form>
28+
29+
Pick a text file:
30+
<h:form enctype="multipart/form-data" prependId="false">
31+
<h:inputFile value="#{fileUploadBean.file}"/><br/>
32+
33+
<h:commandButton value="Upload">
34+
<f:ajax execute="@all" render="@all" onevent="statusUpdate"/>
35+
</h:commandButton><p/>
36+
37+
<h2>File Contents:</h2>
38+
<h:outputText value="#{fileUploadBean.text}"/>
39+
</h:form>
40+
<h2>Status:</h2>
41+
<textarea id="fileUploadBean.text" cols="40" rows="10" readonly="readonly" />
42+
</h:body>
43+
</html>
44+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version='1.0' encoding='UTF-8' ?>
2+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3+
<html xmlns="http://www.w3.org/1999/xhtml"
4+
xmlns:h="http://java.sun.com/jsf/html">
5+
<h:head>
6+
<title>File Upload</title>
7+
</h:head>
8+
<h:body>
9+
<h1>Traditional File Upload</h1>
10+
11+
<h:form>
12+
<h:commandButton value="Ajax File Upload" action="ajax-upload"/><p/>
13+
</h:form>
14+
15+
Pick a text file:
16+
<h:form enctype="multipart/form-data">
17+
<h:inputFile value="#{fileUploadBean.file}"/><br/>
18+
19+
<h:commandButton value="Upload"/><p/>
20+
21+
<h2>File Contents:</h2>
22+
<h:outputText value="#{fileUploadBean.text}"/>
23+
</h:form>
24+
</h:body>
25+
</html>
26+

0 commit comments

Comments
 (0)