【发布时间】:2014-09-01 21:43:26
【问题描述】:
我是 JavaBeans 的新手,我需要一些帮助来保持我的第一个小型 JSF 项目的运行。
我正在编写一个小型 Web 应用程序,用户可以在其中使用特定条件搜索建筑物。所以用户在搜索表单中输入“位置”、“物业类型”、“要价”、“房间数量”和“居住空间”。
我的托管 bean 使用 setter/getter 接受请求,现在数据将被传输到 SQL 类,在该类中处理它们并返回匹配的搜索结果。听起来很简单,但我找不到解决方案。
我的托管 bean 现在看起来像这样:
package beans
//import statements
...
@ManagedBean
@RequestScoped
public class PropertySearchBean {
private String _place
private String _propertyType
private double _askingPrice
private int _rooms
private double _livingSpace
public ArrayList<SearchResults> results = new ArrayList<SearchResults>();
// empty constructor
...
// getter and setter for these 5 user inputs
...
public void initializeSearchResults() {
// do the SQL query, recieve search results
// add it as a new object of 'SearchResults'
SQLPropertySearch search = new SQLPropertySearch(_place, _propertyType,
_askingPrice, _rooms, _livingSpace);
ArrayList<Integer> idResults = search.getPropertyIDlist();
SQLProperty property;
if(!idResults.isEmpty()) {
for(int i=0; i<idResults.size(); i++) {
property = new SQLProperty(idResults.get(i));
results.add(new SearchResults(
property.getPropertyID(),
property.getPropertyName(),
// and so on..
));
}
}
}
public static class SearchResults {
int propertyID;
String propertyName;
// and so on..
public SearchResults(int propertyID, String propertyName) {
this.propertyID = propertyID;
this.propertyName = propertyName;
// and so on..
}
// getter and setter
public int getPropertyID() {
return propertyID;
}
public void setPropertyID(int propertyID) {
this.propertyID = propertyID;
}
// and so on..
}
public ArrayList<SearchResults> getResults() {
return results;
}
}
在我的 XHTML 文件中,我检查了 ArrayList 结果的每个条目。
看起来像这样:
<ui:repeat var="res" value="#{PropertySearchBean.results}">
<p>#{res.propertyID}</p>
<p>#{res.propertyName}</p>
</ui:repeat>
我不知道如何初始化 ArrayList,因为首先要做的是搜索本身,以及用户输入。
感谢您的任何帮助!
【问题讨论】:
-
你想用什么初始化它?您可以使用 @PostConstruct 注释在方法中进行初始工作
标签: java jsf jsf-2 javabeans managed-bean