我是JSF2的新手。我使用Selectonemenu组件来获取表中的国家/地区列表,并根据用户在下拉列表中选择的内容,自动完成组件将执行查询另一个数据库表的完整方法,当我更改Selectonemenu上的值时,它是ajax方法打印所选的值,但当我尝试在自动完成功能中获取它时,它返回null。 这是我的代码:
String country;
// Getter and setter
public String set_country_name(){
System.out.println(country); ==>this is working
return country;
}
public List<Place> completePlace(String query) {
List<Place> filteredPlaceNames = new ArrayList<Place>();
System.out.println(set_country_name()); // this is returning null
ResultSet result;
try {
PreparedStatement ps = con.prepareStatement("SELECT * FROM mytable where country_name = ? AND place_name = ? ");
ps.setString(1, country);
ps.setString(2, query);
result = ps.executeQuery();
while (result.next()) {
Place p = new Place();
//s.setId(result.getInt("id"));
p.setPlace_id(result.getInt("place_id"));
//s.setActive(result.getInt("active"));
p.setPlace_name(result.getString("place_name"));
filteredPlaceNames.add(p);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return filteredPlaceNames;
}
我的XHTML :(我已经尝试过执行和事件代码我得到了相同的结果)
<h:form>
<h:outputText value="Country :" />
<p:spacer width="100" height="10" />
<p:selectOneMenu id="country" value="#{autoCompletePlace.country}" >
<f:selectItems value="#{countryBean.placesName}" />
<p:ajax process="@this" listener="#{autoCompletePlace.set_country_name}" />
</p:selectOneMenu>
<p:autoComplete id="place_name"
value="#{autoCompletePlace.place}"
completeMethod="#{autoCompletePlace.completePlace}"
size="40" />
</h:form>