1. You need 2 Seam components. A "backing bean" required by Tomahawk and SLSB to deal with your JSF action:
1a. Backing Bean:
@Name("uploadBean") public class UploadBackingBean { private UploadedFile file; public void setFile(UploadedFile file) { this.file = file; } @NotNull public UploadedFile getFile() { return this.file; } }
1b. Action and its interface
@Stateless @Name("upload") @Interceptors(SeamInterceptor.class) public class UploadAction implements Upload { private Logger logger; /** * O arquivo que foi enviado. */ @In private UploadBackingBean uploadBean; @In private FacesContext facesContext; @PostConstruct public void init() { logger = Logger.getLogger(this.getClass()); logger.debug("init()"); } public String upload() { logger.debug("upload()"); UploadedFile file = uploadBean.getFile(); logger.debug("Abrindo o arquivo como DOM4J"); logger.debug(file.getName()); facesContext.addMessage(null, new FacesMessage("Nome do arquivo: " + file.getName())); return "success"; } }
@Local public interface Upload { public String upload(); }
2. You need to define a tomahawk.taglib.xml if you want to be able to access your tomahawk upload component via facelets. It shoud go in your .war WEB-INF
<?xml version="1.0"?> <!DOCTYPE facelet-taglib PUBLIC "-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN" "facelet-taglib_1_0.dtd"> <facelet-taglib> <!-- author: thomas.jachmann@mindmatters.de --> <namespace>http://myfaces.apache.org/tomahawk</namespace> <tag> <tag-name>inputFileUpload</tag-name> <component> <component-type>org.apache.myfaces.HtmlInputFileUpload</component-type> </component> </tag> </facelet-taglib>
A full taglib definition is available here: http://wiki.apache.org/myfaces-data/attachments/Use_Facelets_with_Tomahawk/attachments/tomahawk.taglib.xml
3. Apart from the normal Seam stuff in your web.xml you need to reference whatever is that Tomahawk needs:
<!-- My Faces Extensions Filter --> <filter> <filter-name>extensionsFilter</filter-name> <filter-class>org.apache.myfaces.component.html.util.ExtensionsFilter</filter-class> <init-param> <param-name>uploadMaxFileSize</param-name> <param-value>100m</param-value> <!-- <description>Set the size limit for uploaded files. Format: 10 - 10 bytes 10k - 10 KB 10m - 10 MB 1g - 1 GB </description> --> </init-param> <init-param> <param-name>uploadThresholdSize</param-name> <param-value>100k</param-value> <!-- <description>Set the threshold size - files below this limit are stored in memory, files above this limit are stored on disk. Format: 10 - 10 bytes 10k - 10 KB 10m - 10 MB 1g - 1 GB </description> --> </init-param> <!-- <init-param> <param-name>uploadRepositoryPath</param-name> <param-value>/temp</param-value> <description>Set the path where the intermediary files will be stored. </description> </init-param> --> </filter> <filter-mapping> <filter-name>extensionsFilter</filter-name> <url-pattern>*.seam</url-pattern> </filter-mapping> <!-- MyFaces Tomahawk Library --> <context-param> <param-name>facelets.LIBRARIES</param-name> <param-value>/WEB-INF/tomahawk.taglib.xml</param-value> </context-param>
4. The HTML would look something like this:
<h:form enctype="multipart/form-data"> <t:inputFileUpload storage="file" value="#{uploadBean.file}"></t:inputFileUpload> <h:commandButton value="Submit" action="#{upload.upload}"></h:commandButton> <h:messages></h:messages> </h:form>
5. Finally I put the tomahawk.jar and the commons-file-upload.jar and common-io.jar in the .ear that packages the web application and the EJB's. Of course you need to add the class-path in the MANIFEST.MF of the .ear file.
Class-Path: jboss-seam.jar commons-fileupload-1.1.jar commons-io-1.2.jar tomahawk.jar
Hope it helps.
Regards.
Marcio Endo
-
Ok, so I have attached an example on how to implement file upload with Seam. The file is fileupload.zip.
I suck at Ant, so it doesn't contain a nice build.xml. But you CAN generate the necessary .ear by running:
ant -f packaging-build.xml
The attached example worked with:
JBoss-4.0.4.CR2
JBoss-Seam-1.0.0.CR2
MyFaces 1.1.1
Tomahawk 1.1.1
Regards.
Marcio Endo
There's no need to update the manifest file. I have managed to integrated all of the tomahawk components without touching the manifest. I'll be posting the configuration in a while
Regards.
Fady Matar
I have put together a sample File Upload that does not require a backing bean and uses the seam gen tool to create the project.
Regards
Arthur Marinis
Comments