简体   繁体   English

如何在Liferay 6.0.6的JSF Portlet中上载文件

[英]How to upload a file in JSF portlet in Liferay 6.0.6

I'm developing a JSF 2.0 portlet for Liferay 6.0.6 (Plugins SDK 6.1) and I need file upload functionality. 我正在为Liferay 6.0.6(Plugins SDK 6.1)开发一个JSF 2.0 Portlet,并且我需要文件上传功能。 I tried the following different solutions without success: 我尝试了以下不同的解决方案,但均未成功:

Any suggestion how to do it is welcomed, also hacks or using other technologies than JSF. 任何建议如何做到这一点都受到欢迎,也可以黑客入侵或使用JSF以外的其他技术。

Why not use a standard HTML form so: 为什么不使用标准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>

Then in your Java code override the processAction method (usually in a class that extends GenericPortlet or maybe Liferay's MVCPortlet or JSPPortlet (for 5.2.3)) and then you can get the file itself by: 然后在您的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
}

Job done! 任务完成! :) This is only skeleton code, and there will be exception handling you need to do but your IDE will help with that. :)这只是框架代码,将需要执行异常处理,但是您的IDE会帮助您。

~~ EDIT ~~~ ~~编辑~~~

Other possible solution maybe to use: 其他可能的解决方案可能使用:

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

This I got from: http://ironicprogrammer.blogspot.com/2010/03/file-upload-in-jsf2.html 这是我从以下网站获得的: http : //ironicprogrammer.blogspot.com/2010/03/file-upload-in-jsf2.html

Is that any help? 有什么帮助吗?

Use the JSF standard h:inputFile tag (or any file upload tag from any popular component suite ): 使用JSF标准的h:inputFile标签(或任何流行的组件套件中的任何文件上传标签 ):

<h:form enctype="multipart/form-data">
    <h:inputFile value="#{bean.uploadedPart}" />
</h:form>

private Part uploadedPart;

public Part getUploadedPart() {
    return uploadedPart;
}

public void setUploadedPart(Part uploadedPart) {
    this.uploadedPart = uploadedPart;
}

I have successfully used file upload component of Primefaces v3.2 and the built in bridge:inputFile with Liferay-Faces v3.1.0-RC1 on Liferay-6.1-EE. 我已经在Liferay-6.1-EE上成功使用了Primefaces v3.2的文件上传组件和Liferay-Faces v3.1.0-RC1的内置bridge:inputFile。 Still a release candidate but pretty stable. 仍然是候选版本,但相当稳定。 Did not use advanced features of Primefaces upload component though. 但是没有使用Primefaces上传组件的高级功能。 Thanks to Mr. Neil Griffin and several others in doing an excellent job on making JSF 2.x work in portlet environment. 感谢Neil Griffin先生和其他一些人在使JSF 2.x在Portlet环境中工作方面所做的出色工作。

The bridge:inputFile component of PortletFacesBridge 2.0.1 works for me on Liferay 6.1 EE for a Portlet 2.0 portlet using JSF 2.0. PortletFacesBridge 2.0.1bridge:inputFile组件可在Liferay 6.1 EE上为我使用JSF 2.0的Portlet 2.0 portlet运作。 Because we're using Primefaces (v3.2), I also tried to use its uploadcomponent , but that doesn't work in portlets yet. 因为我们使用的是Primefaces(v3.2),所以我也尝试使用了它的uploadcomponent ,但这在portlet中不起作用 It's being worked on for a future version of the PortletFacesBridge/Primefaces . 正在为PortletFacesBridge / Primefaces未来版本进行开发

What worked for me was: 对我有用的是:

xhtml: 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>
...

bean: 豆角,扁豆:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM