JSFJavabean-初始化并继续
我是JavaBeans的新手,我需要一些帮助来保持我的第一个小JSF项目继续进行 我正在编写一个小的web应用程序,用户可以使用特定的标准搜索建筑物。因此,用户在搜索表单中输入“位置”、“物业类型”、“要价”、“房间数量”和“居住空间” 我的托管bean使用setter/getter接受请求,现在数据将被传输到一个SQL类,在那里处理数据并返回匹配的搜索结果。这听起来很简单,但我找不到解决办法 我的托管bean现在看起来像这样:JSFJavabean-初始化并继续,java,jsf,jsf-2,javabeans,managed-bean,Java,Jsf,Jsf 2,Javabeans,Managed Bean,我是JavaBeans的新手,我需要一些帮助来保持我的第一个小JSF项目继续进行 我正在编写一个小的web应用程序,用户可以使用特定的标准搜索建筑物。因此,用户在搜索表单中输入“位置”、“物业类型”、“要价”、“房间数量”和“居住空间” 我的托管bean使用setter/getter接受请求,现在数据将被传输到一个SQL类,在那里处理数据并返回匹配的搜索结果。这听起来很简单,但我找不到解决办法 我的托管bean现在看起来像这样: package beans //import statement
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;
}
}
包bean
//导入语句
...
@ManagedBean
@请求范围
公共类PropertySearchBean{
私人字符串位置
私有字符串\u属性类型
私人双价
私人国际会议室
私人双人生活空间
public ArrayList results=new ArrayList();
//空构造函数
...
//这5个用户输入的getter和setter
...
public void initializeSearchResults(){
//执行SQL查询,接收搜索结果
//将其添加为“SearchResults”的新对象
SQLPropertySearch search=新建SQLPropertySearch(\u place,\u propertyType,
_询问价格、房间、生活空间);
ArrayList idResults=search.getPropertyIDlist();
SQLProperty属性;
如果(!idResults.isEmpty()){
对于(int i=0;i您已经从示例中删除了getter和setter,以提高可读性。我将在这里提供一个实现,以确保共识(特别是关于前导下划线)
属性“place”可以通过值绑定{propertySearchBean.place}
在视图中访问(见下文)
您的代码用于执行搜索。因此,您必须从XHTML文件(视图)传输用户输入到您的托管bean。为此,您需要向视图中添加一个表单。每个搜索查询参数都使用特定的值绑定绑定到您的bean。此外,表单包含一个
标记,该标记最终触发结果列表的初始化
<h:form>
<h:outputLabel for="place" value="Place:" />
<h:inputText id="place" value="#{propertySearchBean.place}" />
<!-- Additional fields -->
<h:commandButton action="#{propertySearchBean.initializeSearchResults}"
value="Search"/>
</h:form>
你想用什么来初始化它?你可以用@PostConstruct注释的方法来做初始工作。这对我很有帮助。谢谢!
public String getPlace() {
return _place;
}
public void setPlace(String place) {
this._place = place;
}
<h:form>
<h:outputLabel for="place" value="Place:" />
<h:inputText id="place" value="#{propertySearchBean.place}" />
<!-- Additional fields -->
<h:commandButton action="#{propertySearchBean.initializeSearchResults}"
value="Search"/>
</h:form>
<ui:repeat var="res" value="#{PropertySearchBean.results}">
<ui:repeat var="res" value="#{propertySearchBean.results}">