问题
I am unsucessfuly trying to bind a RichInputFile with a viewScopeBean.
Here is my code :
jsff :
<af:panelGroupLayout id="pgl1" layout="horizontal">
<af:inputFile label="Upload File" id="if1"
binding="#{viewScope.userBean.inputFile}"
valueChangeListener="#{viewScope.userBean.onFileUploadValueChangeListener}"
autoSubmit="true"/>
<af:spacer width="10" height="10" id="s7"/>
<af:commandButton text="Upload" id="cb1"
disabled="#{viewScope.userBean.inputFile.value == null ? true : false}"
partialTriggers="if1"
actionListener="#{viewScope.userBean.onUploadFile}"/>
</af:panelGroupLayout>
bean :
public class UserBean{
private RichInputFile inputFile;
private UploadedFile file;
private String fileContent;
private String fileName;
private InputStream inputstream;
public UserBean() {
super();
}
public void onFileUploadValueChangeListener(ValueChangeEvent valueChangeEvent) {
resetValue();
file = (UploadedFile)valueChangeEvent.getNewValue();
try {
inputstream = file.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
}
public void onUploadFile(ActionEvent actionEvent) {
if (file != null && inputstream != null) {
fileName = file.getFilename();
StringWriter writer = new StringWriter();
}
if (inputFile != null) {
inputFile.resetValue();
inputFile.setValid(true);
}
}
public void resetValue() {
if (fileName != null)
fileName = null;
if (fileContent != null)
fileContent = null;
if (inputstream != null)
inputstream = null;
}
public void setInputFile(RichInputFile inputFile) {
this.inputFile = inputFile;
}
public RichInputFile getInputFile() {
return inputFile;
}
public void setFile(UploadedFile file) {
this.file = file;
}
public UploadedFile getFile() {
return file;
}
public String getFileContent() {
return fileContent;
}
public String getFileName() {
return fileName;
}
}
When i select a file on the upload file element, the file is selected but the ValueChangeEvent is not trigger and the onFileUploadValueChangeListener is not called. PPR occur and the RichInputFile inputFile value is still null, displaying "No file selected" on the screen. (the binding is successfull)
What should i correct to have the valueChangeEvent triggered on an af:inputFile ?
回答1:
I found the anwser.
To have the af:inputFile valueChangeEvent triggered it is mandatory to have it surround by a af:form usesUpload="true" tag :
<af:panelGroupLayout id="pgl1" layout="horizontal">
<af:form usesUpload="true" id="f1">
<af:inputFile label="Upload File" id="if1"
binding="#{viewScope.userBean.inputFile}"
valueChangeListener="#{viewScope.userBean.onFileUploadValueChangeListener}"
autoSubmit="true"/>
<af:spacer width="10" height="10" id="s7"/>
<af:commandButton text="Upload" id="cb1"
disabled="#{viewScope.userBean.inputFile.value == null ? true : false}"
partialTriggers="if1"
actionListener="#{viewScope.userBean.onUploadFile}"/>
</af:form>
</af:panelGroupLayout>
来源:https://stackoverflow.com/questions/36032476/adf-afinputfile-does-not-trigger-valuechangeevent-with-valuechangelistener