2012-01-08 57 views

回答

3

爲什麼不使用標準的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

0

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(); 
    ... 
} 
0

我已經成功地使用文件上傳組件和內置的橋樑:INPUTFILE與Liferay-Faces v3.1.0-RC1在Liferay-6.1-EE上。仍然是候選版本,但相當穩定。 雖然沒有使用Primefaces上傳組件的高級功能。 感謝Neil Griffin先生和其他幾個人在使Portlet環境中的JSF 2.x工作方面做得非常出色。

相關問題