Recently a project student had a requirement to use file upload functionality in JSF Portlet. Since Mojarra's JSF Implementation does not provide fileupload component, he wanted to use the fileupload component provided by MyFaces Tomahawk component library. He used the example from this blog and portletized it. But it didn't work with either OpenPortal JSF Portlet Bridge or Apache MyFaces JSR329 Portlet Bridge. I found another blog post that talks about extending OpenPortal JSF Portlet Bridge inorder to provide support for fileupload. It worked, but i wanted a generic solution that works with both OpenPortal JSF Portlet Bridge and Apache MyFaces JSR329 Portlet Bridge or any other JSF Portlet Bridge. So i used the Portlet Filter approach based on that blog. Ofcourse this solution will work only on portals that support JSR286.

Following are the steps to add fileupload functionality in your jsf portlet

1. Add the following JARs to the WEB-INF/lib of the jsf portlet webapp. The version numbers doesn't matter as long as you get the newest.

2. Add the following portlet filter entry in the portlet.xml


name_of_the_portlet ........ PortletFileUploadFilter com.sun.portlet.PortletFileUploadFilter ACTION_PHASE PortletFileUploadFilter name_of_the_portlet
3. Define the fileupload component in the JSF page (you will use the name to get FileItem from the request)


4. After you submit the JSF page, you can obtain the org.apache.commons.fileupload.FileItem for the input file component from the PortletRequest as follows. Once you have access to FileItem you can get the name, the I/O stream of the uploaded file.


FacesContext facesContext = FacesContext.getCurrentInstance();PortletRequest portletRequest = (PortletRequest) facesContext.getExternalContext().getRequest();Fil eItem fileItem = (FileItem)portletRequest.getAttribute("name_of_the_component"); See the example jsf fileupload portlet that uses this functionality.




Read More about [JSF Portlet File Upload...