primefaces复选框没有绑定

时间:2014-08-11 14:39:55

标签: jsf jsf-2 primefaces

你好我正在做一个upoding图像系统,上传方法很好,但我有一个简单的复选框,但当我想要检索该值时,从来没有被绑定。

我的JSF

    <h:body>

<h:form>
    <h:panelGrid columns="2" cellpadding="2">

        <p:panel id="panel" header="Local import">

            <p:fileUpload style="padding:10px"
                fileUploadListener="#{fileUploadView.handleFileUpload}"
                mode="advanced" dragDropSupport="false" multiple="true"
                update="messages" sizeLimit="900000" fileLimit="100"
                allowTypes="/(\.|\/)(gif|jpe?g|png)$/" />

            <p:growl id="messages" showDetail="true" />

            <p:outputLabel for="category" value="Category" />
            <p:inputText id="category" value="#{fileUploadView.path}"
                required="true" label="Path">
                <f:validateLength minimum="1" />
                <p:ajax update="msgCategory" event="keyup" />
            </p:inputText>
            <p:message for="category" id="msgCategory" display="icon" />

            <p:outputLabel for="document" value="Is a single document." />
            <p:selectBooleanCheckbox style="padding:10px"
                value="#{fileUpload.singleDocument}" id="document" />
        </p:panel>


    </h:panelGrid>

</h:form>

这是托管bean,在session scoope中设置。

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;

import org.primefaces.event.FileUploadEvent;

@ManagedBean

public class FileUpload {

private String path = null;
private Boolean singleDocument;

public void handleFileUpload(FileUploadEvent event) {
    FacesMessage message = new FacesMessage("Succesful", event.getFile()
            .getFileName() + " is uploaded.");
    FacesContext.getCurrentInstance().addMessage(null, message);
    try {
        copyFile(event.getFile().getFileName(), event.getFile()
                .getInputstream());
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public void copyFile(String fileName, InputStream in) {
    try {

        // write the inputStream to a FileOutputStream
        OutputStream out = new FileOutputStream(new File(path + fileName));
        System.out.println(path + fileName);
        int read = 0;
        byte[] bytes = new byte[1024];

        while ((read = in.read(bytes)) != -1) {
            out.write(bytes, 0, read);
        }

        in.close();
        out.flush();
        out.close();

        System.out.println("New file created!");
        System.out.println(singleDocument);
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }
}

// Getters and setters

public String getPath() {
    return path;
}

public void setPath(String path) {
    this.path = path;
}

public Boolean getSingleDocument() {
    return singleDocument;
}

public void setSingleDocument(Boolean singleDocument) {
    this.singleDocument = singleDocument;
}

}

提前感谢您的时间和答案

1 个答案:

答案 0 :(得分:0)

问题来自变量从未初始化的事实。可以将private Boolean singleDocument;更改为private boolean singleDocument,也可以将变量赋值给某个地方(零args构造函数或@PostConstruct带注释的方法,或将其更改为private Boolean singleDocument = Boolean.FALSE;)...