Jsf 基于p:selectOneMenu值的p:autoComplete查询使用AJAX返回空值
我是JSF2新手。我正在使用Selectonemenu组件获取表中的国家列表,根据用户在下拉列表中的选择,autocomplete组件将执行查询另一个数据库表的完整方法,ajax方法当我在Selectonemenu上更改值时,它会正确打印所选的值,但当我尝试在autocomplete函数中获取它时,它会返回null。 这是我的密码:Jsf 基于p:selectOneMenu值的p:autoComplete查询使用AJAX返回空值,jsf,jsf-2,primefaces,autocomplete,selectonemenu,Jsf,Jsf 2,Primefaces,Autocomplete,Selectonemenu,我是JSF2新手。我正在使用Selectonemenu组件获取表中的国家列表,根据用户在下拉列表中的选择,autocomplete组件将执行查询另一个数据库表的完整方法,ajax方法当我在Selectonemenu上更改值时,它会正确打印所选的值,但当我尝试在autocomplete函数中获取它时,它会返回null。 这是我的密码: String country; // Getter and setter public String set_country_name(){ Sys
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>
是的,很抱歉,我在代码中更新了这一点,同样的问题是,当我开始在自动完成中键入时,它开始为null,但当我更改selectonemenu项时,它返回正确的值。OK,我正在更新我的xhtml代码,我想我放置了未修改的代码。当我更改selectonemenu选项时,System.out.printlncountry;内部公共字符串set\u country\u name正在正确打印,但当我开始在自动完成中键入System.out.printlset\u country\u name时;在公共列表completePlaceString查询中,正在打印2 null,这意味着它没有更新,或者什么?bean的作用域是什么?如果是请求范围,则您选择的国家/地区的值将不会保留在后续的post请求回发中。它至少应该是一个视图范围的bean,业务逻辑应该放在服务层自己的位置,但在这种情况下,它至少应该是一个用@PostConstruct修饰的方法。@Tiny是的,我正在使用一个请求范围的bean,我现在将它改为视图范围的bean,它工作得很好,但它在控制台中抛出了以下信息:CDI@ViewScoped manager unavailable java.lang.NoClassDefFoundError:javax/enterprise/context/spi/context,原因是:java.lang.ClassNotFoundException:javax.enterprise.context.spi.context