This How-To post provides instructions for adding a file upload component to a web interaction in StarPound Studio. The file upload component in JSF enables a user to select a file from their local workstation and upload it to a server location.
To complete this process, you will do the following:
Set up the application in StarPound Studio.
Create the page source of the Web Form task to include the JSF tags required for the file upload component.
Create the Script task code to save the uploaded file to the StarPound file system.
Version Information
This How-To applies to StarPound 1.2.0 and higher. To accomplish this procedure with an earlier version of StarPound, contact StarPound Technologies Support dev_support@starpoundtech.com or the forum.
Set Up the Application
The first step in the procedure is to set up the application in StarPound Studio to include the model elements that enable the file upload function. The application must include:
A Web Form task with the <x:inputFileUpload> JSF component, which displays an input field and Browse button for locating the file
A Script task to save the selected file
Note: The application in the sample project also includes model elements that store the content of the uploaded file in the Business Data Document and display file information to the user upon successful upload.
Modify the Web Form Task
To create the file upload control on the web form, you must add several lines to the source code of the Web Form task. You may want to use the New JSP Page Wizard to create a new Web Form task in the proper format. Then you need to edit the page source to add the required file upload JSF tags.
Do the following:
In StarPound Studio, locate or create the Web Form task that should contain the file upload control.
Double-click the Web Form task to open it in the JSP Editor.
Modify the source to add the required tags, using the code sample below as a guideline.
Click Save to close and save the changes to the page source.
You need to add or modify the following sections of code:
Add the declaration <%@ taglib uri="http://myfaces.apache.org/tomahawk" prefix="x"%> at the top of the code.
Modify the <h:form> tag to add enctype="multipart/form-data".
<tr> <td> Select a Text File:<x:inputFileUpload id="myFileId" value="#{myBean.upFile}" storage="file" required="true" size="50"/> </td> </tr>
Modify the Script Task
Once you have added the file upload control to the Web Form task, you use a Script task to store the selected file on the server.
Do the following:
In StarPound Studio, locate or create the Script task that directly follows the Web Form task.
Double-click the Script task to display its properties.
On the Script tab, click Edit to display the source code in the Java Editor.
Modify or add the code to process the selected file and save it on the server. Use the sample below as a guideline.
Click Save to close and save the changes to the code.
Sample 1
HttpSession session = bpContext.getHTTPSession(); Object o = (Object) session.getAttribute("myBean"); // in real app, the "args[0]" is the root directory defined by application. String[] args = new String[] {"/tmp/"};
This How-To post provides
This How-To post provides instructions for adding a file upload component to a web interaction in StarPound Studio. The file upload component in JSF enables a user to select a file from their local workstation and upload it to a server location.
For more information on developing web forms with JSF in StarPound Studio, see the StarPound Studio JSF Authoring Guide.
Procedure Overview
To complete this process, you will do the following:
Version Information
This How-To applies to StarPound 1.2.0 and higher. To accomplish this procedure with an earlier version of StarPound, contact StarPound Technologies Support
dev_support@starpoundtech.comor the forum.Set Up the Application
The first step in the procedure is to set up the application in StarPound Studio to include the model elements that enable the file upload function. The application must include:
Note: The application in the sample project also includes model elements that store the content of the uploaded file in the Business Data Document and display file information to the user upon successful upload.
Modify the Web Form Task
To create the file upload control on the web form, you must add several lines to the source code of the Web Form task. You may want to use the New JSP Page Wizard to create a new Web Form task in the proper format. Then you need to edit the page source to add the required file upload JSF tags.
Do the following:
You need to add or modify the following sections of code:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri=" http://myfaces.apache.org/tomahawk" prefix="x"%>
...
<c:set target="${requestScope.WIManager}" property="autocomplete" value="false"/>
<h:form id="WFT_1147185040663" onsubmit="return checkClicks();" enctype="multipart/form-data">
<input type="hidden" name="REQUEST_TOKEN_KEY" value="<%= org.starpound.runtime.core.BPIDProvider.getGUID(request) %>"/>
<h:inputHidden value="#{WIManager.allDocuments}"/>
<%--
END_GENERATED
--%>
<%@ page import="org.starpound.wim.UploadBean" %>
<jsp:useBean id="myBean" scope="session" class="org.starpound.wim.UploadBean"/>
...
<tr>
<td>
Select a Text File:<x:inputFileUpload id="myFileId" value="#{myBean.upFile}" storage="file" required="true" size="50"/>
</td>
</tr>
Modify the Script Task
Once you have added the file upload control to the Web Form task, you use a Script task to store the selected file on the server.
Do the following:
Sample 1
HttpSession session = bpContext.getHTTPSession();Object o = (Object) session.getAttribute("myBean");
// in real app, the "args[0]" is the root directory defined by application.
String[] args = new String[] {"/tmp/"};
try {
bpContext.getFile(null, "upload.txt", BPFile.PERSISTANCE_PROC_INST).setContent(((UploadBean)bpContext.getHTTPSession().getAttribute("myBean")).getContent());
} catch (Exception e) {
e.printStackTrace();
throw new BPScriptletException(e);
}
Sample 2
FileOutputStream fis = null;;try {
fis = new FileOutputStream("/tmp/upload.txt");
fis.write(((UploadBean)bpContext.getHTTPSession().getAttribute("myBean")).getContent());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally {
if(fis!=null)
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Sergey Lebedev
I've inserted the How-to text
I've inserted the How-to text into post for now. We'll see what is wrong with your account.
Sergey Lebedev