从动作侦听器调用时,getter方法返回null

时间:2019-01-17 20:15:03

标签: jsf primefaces

我有一个带有p:selectOneMenu组件的应用程序。该组件用于确定要上传的文件类别,以便在上传文件时可以对其进行一些工作。

我实现了这个post的答案,似乎可以正确调用fileType的setter方法。但是一旦提交文件并调用handleFileUpload方法,fileType getter方法将返回null。

例如,如果我选择Foo,那么我会得到输出

File type changed to: Foo

但是当我点击上传按钮时,我得到了输出

The file type selected is null

当我期望

The file type selected is Foo

是什么原因导致get方法返回两个不同的结果,并且有一种解决方法?

main.xhtml

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html 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">
    <meta http-equiv="refresh"
        content="#{session.maxInactiveInterval};url=index.xhtml" />
    <f:view contentType="text/html">
        <h:head>
            <title>File Upload</title>
        </h:head>
        <p:layout fullPage="true">
            <p:layoutUnit position="center">
                <ui:insert name="pagebody" />
            </p:layoutUnit>
        </p:layout>
    </f:view>
</html>

index.xhtml

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<ui:composition 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" template="/templates/main.xhtml">
    <ui:define name="pagebody">     
        <h:body>
            <h:form id="uploadform" enctype="multipart/form-data">

                <h:panelGrid columns="2" style="margin-bottom:10px" cellpadding="5">
                    <h:outputText value="File Type:" />
                    <p:selectOneMenu value="#{uploadBean.fileType}">
                        <f:selectItem itemLabel="Foo" itemValue="Foo"/>
                        <f:selectItem itemLabel="Bar" itemValue="Bar"/>
                        <f:ajax listener="#{uploadBean.changeFileType}" />
                    </p:selectOneMenu>
                </h:panelGrid>

                <br />
                <p:fileUpload fileUploadListener="#{uploadBean.handleFileUpload}" mode="advanced"/> 
            </h:form>
        </h:body>
    </ui:define>
</ui:composition>

UploadBean.java

@RequestScoped
@ManagedBean(name = "uploadBean")
public class UploadBean implements java.io.Serializable {

    private static final long serialVersionUID = 1L;
    private String fileType = null;

    @PostConstruct
    public void init() {
    }

    public void handleFileUpload(FileUploadEvent event){
        System.out.println("The file type selected is " + this.getFileType());
    }

    public String getFileType() {
        return fileType;
    }

    public void setFileType(String fileType) {
        this.fileType = fileType;
    }

    public void changeFileType() {
        System.out.println("File type changed to: " + this.getFileType());
    }
}

1 个答案:

答案 0 :(得分:0)

正如@Kukeltje指出的那样,问题是我的bean的作用域不正确。从RequestScoped切换到ViewScoped可以解决我遇到的问题。