Java ArrayList 在两次用户交互之间变为空?
Java ArrayList becomes empty between two user interactions?
我正在使用 JDeveloper 开发一个在 Weblogic Server 12c 上运行的 ADF 融合应用程序。
该应用程序包含一个 JSF 页面,其中包括一个文件输入、一个 table 和一个按钮。用户上传文件后,它会被解析,结果按预期显示在 table 中。用户的下一步是单击按钮将解析结果保存到数据库(通过支持 bean)。
这些是 JSF 文档中提到的元素:
<af:inputFile label="file"
id="if1"
valueChangeListener="#{ResultMatching.valueChangeListener}"
binding="#{ResultMatching.bindingInputFileCSV}"/>
<af:button text="upload"
id="b1"
action="#{ResultMatching.readFile}"
binding="#{ResultMatching.bindingButtonReadFile}"/>
<af:table var="row"
rowBandingInterval="0"
id="t3"
value="#{ResultMatching.Results}"
binding="#{ResultMatching.bindingTableResults}"
fetchSize="32"
width="800"
partialTriggers="::b1 ::b21">
<!--column definitions-->
</af:table>
<af:button id="b21"
text="save"
action="#{ResultMatching.saveResults}"/>
这些是支持 bean 中的连贯方法 ResultMatching.java
:
private ArrayList<Result> Results= new ArrayList<Result>();
private RichTable bindingTableResults;
public void readFile() {
Results.clear();
if (fileInputStream != null) {
for (HashMap<String, String> dataSet : FileHandler.parseCFFile(fileInputStream)) {
Result r = new Result("values from the dataSet");
Results.add(r);
}
matchResults();
} else {
String msg = "No file was selected for upload";
FacesContext.getCurrentInstance().addMessage(
null, new FacesMessage(FacesMessage.SEVERITY_INFO,msg, msg));
}
}
private void matchResults() {
//very complicated matching logic
bindingTableResults.setValue(Results);
AdfFacesContextImpl.getCurrentInstance().addPartialTarget(bindingTableResults);
System.out.println(Results.size()); //prints the correct amount of results like in the file
}
public void saveResults() {
if (Results.isEmpty()) {
System.out.println("Results is empty!");
return;
//here is where I end up
//i did no other GUI interaction meanwhile
//neither has the getter nor setter of Results been called
//why is it suddenly empty? Where are my objects???
}
for(Result r : Results){
//as Results is empty this loop always gets skipped
//and so does the actual update statement
}
}
bean 是视图范围的。有什么想法我的结果去了哪里?提前致谢!
从您的代码来看,ResultMatching bean 似乎在请求范围内:
value="#{ResultMatching.Results}"
如果你想访问视图范围内的 bean,EL 应该是
value="#{viewScope.ResultMatching.Results}"
您必须为所有 EL 中每次出现的 ResultMatching 更改 EL。确保在任务流的 viewScope 中定义了该 bean。
我正在使用 JDeveloper 开发一个在 Weblogic Server 12c 上运行的 ADF 融合应用程序。
该应用程序包含一个 JSF 页面,其中包括一个文件输入、一个 table 和一个按钮。用户上传文件后,它会被解析,结果按预期显示在 table 中。用户的下一步是单击按钮将解析结果保存到数据库(通过支持 bean)。
这些是 JSF 文档中提到的元素:
<af:inputFile label="file"
id="if1"
valueChangeListener="#{ResultMatching.valueChangeListener}"
binding="#{ResultMatching.bindingInputFileCSV}"/>
<af:button text="upload"
id="b1"
action="#{ResultMatching.readFile}"
binding="#{ResultMatching.bindingButtonReadFile}"/>
<af:table var="row"
rowBandingInterval="0"
id="t3"
value="#{ResultMatching.Results}"
binding="#{ResultMatching.bindingTableResults}"
fetchSize="32"
width="800"
partialTriggers="::b1 ::b21">
<!--column definitions-->
</af:table>
<af:button id="b21"
text="save"
action="#{ResultMatching.saveResults}"/>
这些是支持 bean 中的连贯方法 ResultMatching.java
:
private ArrayList<Result> Results= new ArrayList<Result>();
private RichTable bindingTableResults;
public void readFile() {
Results.clear();
if (fileInputStream != null) {
for (HashMap<String, String> dataSet : FileHandler.parseCFFile(fileInputStream)) {
Result r = new Result("values from the dataSet");
Results.add(r);
}
matchResults();
} else {
String msg = "No file was selected for upload";
FacesContext.getCurrentInstance().addMessage(
null, new FacesMessage(FacesMessage.SEVERITY_INFO,msg, msg));
}
}
private void matchResults() {
//very complicated matching logic
bindingTableResults.setValue(Results);
AdfFacesContextImpl.getCurrentInstance().addPartialTarget(bindingTableResults);
System.out.println(Results.size()); //prints the correct amount of results like in the file
}
public void saveResults() {
if (Results.isEmpty()) {
System.out.println("Results is empty!");
return;
//here is where I end up
//i did no other GUI interaction meanwhile
//neither has the getter nor setter of Results been called
//why is it suddenly empty? Where are my objects???
}
for(Result r : Results){
//as Results is empty this loop always gets skipped
//and so does the actual update statement
}
}
bean 是视图范围的。有什么想法我的结果去了哪里?提前致谢!
从您的代码来看,ResultMatching bean 似乎在请求范围内:
value="#{ResultMatching.Results}"
如果你想访问视图范围内的 bean,EL 应该是
value="#{viewScope.ResultMatching.Results}"
您必须为所有 EL 中每次出现的 ResultMatching 更改 EL。确保在任务流的 viewScope 中定义了该 bean。