如何在Liferay 6.0.6中的JSF portlet中上传文件

问题描述:

我正在开发Liferay 6.0.6(Plugins SDK 6.1)的JSF 2.0 portlet,我需要文件上传功能。我尝试了以下不同的解决方案没有成功:如何在Liferay 6.0.6中的JSF portlet中上传文件

任何建议如何做到这一点是值得欢迎的,也是黑客或使用其他技术,而不是JSF。

为什么不使用标准的HTML表单这样:

<form action="your_action_goes_here" method="post" enctype="multipart/form-data"> 
    <input type="file" name="file" id="file" /> 
    <input type="submit" name="submit" value="Submit" /> 
</form> 

然后在你的Java代码重写processAction方法(通常是在扩展了GenericPortlet也许Liferay的MVCPortlet或的JSPPortlet(用于5.2.3)一类)然后你可以通过以下方式获得文件本身:

public void processAction(ActionRequest actionRequest, ActionResponse actionResponse) { 
    UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(actionRequest); 
    File file = (File) uploadRequest.getFile("file"); 
    // Do something with your file here 
} 

工作完成! :)这只是框架代码,并且会有异常处理,但您的IDE将对此提供帮助。

~~编辑~~~

其他可能的解决方案可能使用:

HttpServletRequest req = FacesUtil.getRequest(); 
UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(req); 

我是从有:http://ironicprogrammer.blogspot.com/2010/03/file-upload-in-jsf2.html

是,任何帮助吗?

+0

是的,我也试过(http://www.portalteam.net/blogs/using-file-upload-in-liferay-jsf-portlet)覆盖org.portletfaces.bridge.GenericFacesPortlet。这里的问题是processAction从来没有被调用(只有processEvent(EventRequest eventRequest,EventResponse eventResponse)),因此我无法获得File。 我试图在其他地方调用PortalUtil.getUploadPortelRequest,但仍然无法获取文件... – katis 2012-01-09 14:04:29

+0

我发现了另一种可能的解决方案,不知道它会不会工作,但认为我会张贴并看到! :) – Jonny 2012-01-09 14:36:40

bridge:inputFilePortletFacesBridge 2.0.1的组件适用于Liferay 6.1 EE,适用于使用JSF 2.0的Portlet 2.0 portlet。因为我们使用的是Primefaces(v3.2),我也尝试过使用它的uploadcomponent,但是那个doesn't work in portlets呢。它正在为future version of the PortletFacesBridge/Primefaces工作。

什么工作对我来说是:

XHTML:

<f:view xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:f="http://java.sun.com/jsf/core" 
    xmlns:ui="http://java.sun.com/jsf/facelets" 
    xmlns:p="http://primefaces.org/ui" 
    xmlns:bridge="http://portletfaces.org/bridge"> 

... 
    <h:form enctype="multipart/form-data" method="POST"> 
     <bridge:inputFile id="icon" binding="#{bean.attachment}" /> 
    </h:form> 
... 

豆:中Primefaces v3.2

import org.portletfaces.bridge.component.UploadedFile 

... 

private transient HtmlInputFile attachment; 

... 

public HtmlInputFile getAttachment() { 
    return attachment; 
} 

public void setAttachment(HtmlInputFile attachment) { 
    this.attachment = attachment; 
} 

public String addApplication() { 
    UploadedFile uploadedFile = attachment.getUploadedFile(); 
    ... 
} 

我已经成功地使用文件上传组件和内置的桥梁:INPUTFILE与Liferay-Faces v3.1.0-RC1在Liferay-6.1-EE上。仍然是候选版本,但相当稳定。 虽然没有使用Primefaces上传组件的高级功能。 感谢Neil Griffin先生和其他几个人在使Portlet环境中的JSF 2.x工作方面做得非常出色。