0

总结一下:我想将用户通过<p:fileUpload>使用fileUploadControllerManagedBean 作为其的表单传递的文件上传fileUploadListener到某个路径,然后将该路径与文件名一起获取并以某种方式将其存储(问题是 - 如何?)在wydarzenieMBManagedBean 中,因为我在我的方法中需要filePath作为字符串addWydarzenie()将此路径存储在数据库中并稍后使用它。

我的数据库中有一个名为“Wydarzenie”的表。它有许多值,例如名称等,我在我的 JSF 表单中指定这些值供用户使用 primefaces 填写。获取它们没有问题,我只是使用 ManagedBean 存储输入,然后使用 addWydarzenie() 方法将此输入写入数据库。

utworzWydarzenie.xhtml 表单的一部分:

...
<h:outputLabel for="opis" value="Opis :" />
<p:inputTextarea id="opis" value="#{wydarzenieMB.opis}" label="opis">
...
<p:commandButton id="addWydarzenie" value="Zatwierdź" action="#{wydarzenieMB.addWydarzenie}" ajax="false"/>

我在 wydarzenieManagedBean 中的 addWydarzenie() 方法:

public String addWydarzenie() {
    try {
        Wydarzenie wydarzenie = new Wydarzenie();
        ...
        wydarzenie.setOpis(getOpis());
        ...
        getWydarzenieServiceImpl().addWydarzenie(wydarzenie);
        return SUCCESS;
    } catch (DataAccessException e) {
        e.printStackTrace();
    }   

    return ERROR;
}

例如,我使用的值没有问题,因为我指定了要存储它们p:inputText的 bean 的值(例如:)。value="#{wydarzenieMB.opis}"问题从我的<p:fileUpload>字段开始,因为:
1. 我没有在那里指定值,因为没有返回值(?) - 仅上传文件
2. 我正在使用fileUploadControllerManagedBean 处理文件上传但我想要它拥有的上传文件的 filePath 字符串存储在我的wydarzenieMBManagedBean 中。

这是我从 utworzWydarzenie.xhtml 表单上传文件的代码:

...
<h:outputLabel for="plakat" value="Plakat :" />
<p:fileUpload id="plakat" update="messages" fileUploadListener="#{fileUploadController.handleFileUpload}" multiple="false" sizeLimit="1000000" allowTypes="/(\.|\/)(gif|jpe?g|png)$/" />
<p:growl id="messages" showDetail="true"/>
...  

还有我的 fileUploadController 类:

@ManagedBean(name="fileUploadController")
@RequestScoped
public class FileUploadController 
{  
private String destination="E:/PROJEKT ZESPOLOWY/events/WebContent/resources/plakaty/";
private String sciezkaPliku = "";           // complete file path including destination and file name

public void handleFileUpload(FileUploadEvent event) throws IOException { 
    FacesMessage msg = new FacesMessage("Plik: ", event.getFile().getFileName() + " został poprawnie wysłany na serwer.");
    FacesContext.getCurrentInstance().addMessage(null, msg);

    String path = destination;

    SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmss");
    String name = fmt.format(new Date())
            + event.getFile().getFileName().substring(
                  event.getFile().getFileName().lastIndexOf('.'));
    File file = new File(path + name);

    sciezkaPliku += path + name;            // I set file path here

    InputStream is = event.getFile().getInputstream();
    OutputStream out = new FileOutputStream(file);
    byte buf[] = new byte[1024];
    int len;
    while ((len = is.read(buf)) > 0)
        out.write(buf, 0, len);
    is.close();
    out.close();
}   

public String getSciezkaPliku() {
    return sciezkaPliku;
}

public void setSciezkaPliku(String sciezkaPliku) {
    this.sciezkaPliku = sciezkaPliku;
}
}  

它被声明为 ManagedBean,因为我试图访问它的sciezkaPliku变量,它是wydarzenieMB这样的文件路径:

@ManagedProperty(value="#{fileUploadController.sciezkaPliku}")
private String plakat;

但它始终为空。我还尝试完整地wydarzenieMB进入内部fileUploadController并使用它的 setter 方法:

@ManagedBean(name="fileUploadController")
@RequestScoped
public class FileUploadController 
{  
    @ManagedProperty(value="#{wydarzenieMB}")
    WydarzenieManagedBean w;

    public void handleFileUpload(FileUploadEvent event) throws IOException { 

        ...
        sciezkaPliku += path + name;            // I set file path here
        w.setPlakat(sciezkaPliku);
        ...

    }
    ...
}

但它也没有成功。文件上传并保存在指定的文件夹中没有问题,当我sciezkaPlikuhandleFileUpload它打印时可以,但是当我使用addWydarzenie()它创建新的 Wydarzenie 时总是null.

有任何想法吗?

4

1 回答 1

0

如果我清楚地理解了您的问题,我建议您在FileUploadController中将您的范围更改为“@SessionScoped”。我有同样的问题,它是通过这种方式解决的。

检查出来并告诉我结果;)

于 2013-04-12T15:36:56.733 回答