Hibernate Primefaces自动完成文本
早上好 我试图创建一个简单的页面,在下拉列表中列出所有国家,然后,在我选择一个国家后,州的自动完成显示,并在键入3个字符后执行自动完成。这似乎是一个简单的问题 这是我的状态模型Hibernate Primefaces自动完成文本,hibernate,jsf-2,primefaces,autocomplete,Hibernate,Jsf 2,Primefaces,Autocomplete,早上好 我试图创建一个简单的页面,在下拉列表中列出所有国家,然后,在我选择一个国家后,州的自动完成显示,并在键入3个字符后执行自动完成。这似乎是一个简单的问题 这是我的状态模型 package br.com.danielfc.model; // Generated 27/08/2015 23:59:43 by Hibernate Tools 4.3.1 import static javax.persistence.GenerationType.IDENTITY; import java.
package br.com.danielfc.model;
// Generated 27/08/2015 23:59:43 by Hibernate Tools 4.3.1
import static javax.persistence.GenerationType.IDENTITY;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
/**
* State generated by hbm2java
*/
@Entity
@Table(name = "State", catalog = "daniel")
@NamedQuery(name = "State.findStateByName", query = "select s from State s where s.country.iso = :iso and s.name like :name ")
public class State implements java.io.Serializable {
/**
*
*/
private static final long serialVersionUID = 4498876370214791673L;
private Integer id;
private Country country;
private String name;
/*Stands for Federative Unit*/
private String fu;
private Set<City> cities = new HashSet<City>(0);
public State() {
}
public State(Country country, String name, String fu) {
this.country = country;
this.name = name;
this.fu = fu;
}
public State(Country country, String name, String fu, Set<City> cities) {
this.country = country;
this.name = name;
this.fu = fu;
this.cities = cities;
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "countryIso", nullable = false, referencedColumnName = "iso")
public Country getCountry() {
return this.country;
}
public void setCountry(Country country) {
this.country = country;
}
@Column(name = "name", nullable = false, length = 45)
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@Column(name = "fu", nullable = false, length = 2)
public String getFu() {
return this.fu;
}
public void setFu(String fu) {
this.fu = fu;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "state")
public Set<City> getCities() {
return this.cities;
}
public void setCities(Set<City> cities) {
this.cities = cities;
}
}
这是我的jsf页面的一部分,我想在前面解释一下这个行为
<h:outputLabel for="addressBeanExampleCountry" value="Country:" />
<h:panelGroup>
<!-- <h:inputText id="addressBeanExampleCountry" maxLength="45" value="#{addressBean.example.country}"/> -->
<h:selectOneMenu id="addressBeanExampleCountry" value="#{addressBean.example.country}">
<f:selectItem />
<f:selectItems itemLabel="#{forgeview:display(_item.name)}" itemValue="#{_item}" value="#{countryBean.all}" var="_item" />
</h:selectOneMenu>
<h:message for="addressBeanExampleCountry" styleClass="error" />
</h:panelGroup>
<h:outputText />
<h:outputLabel for="addressBeanExampleState" value="State:" rendered="#{not empty addressBean.example.country}"/>
<h:panelGroup rendered="#{not empty addressBean.example.country}">
<p:autoComplete id="state" value="#{stateBean.state}" completeMethod="#{stateBean.getAllByCountry()}" minQueryLength="3"/>
<h:message for="addressBeanExampleState" styleClass="error" />
</h:panelGroup>
问题是,在我选择国家后,该方法没有被调用,自动完成也没有显示
怎么了?
实现自动完成逻辑的最佳/正确方法是什么?不要这样混合使用CDI和EJB。创建CDIBean并@Inject或@EJB将您的EJB注入其中。getAllByCountry、String name方法有2个参数,但在EL中调用它时没有任何{stateBean.getAllByCountry}。可能您想从其他bean{addressBean.example.country}获取它们是的,但这是使用forge脚手架过程创建的。尚未调用getAllByCountry,因为未呈现panelgroup。这是上述问题的一部分。为什么在我选择了国家之后,这个小组就不出现了?当你选择了一些在BackingBean中还看不到的东西时,只有当你提交表格的时候。若您想立即看到值,那个么您可以进行部分处理,并更新您的面板,但首先用一些元素将其包装起来。要添加的内容包括:对于和例如对于您现有的。可能还有更多的问题需要解决。创建一个mcve。太吵了。@Kukeltje,什么是MCVE?有什么好文章推荐我吗?我用谷歌搜索了一下,但到目前为止还没有结果。无论如何,谢谢你的提示。
package br.com.danielfc.model;
// Generated 27/08/2015 23:59:43 by Hibernate Tools 4.3.1
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
/**
* Address generated by hbm2java
*/
@Entity
@Table(name = "Address", catalog = "daniel")
public class Address implements java.io.Serializable {
/**
*
*/
private static final long serialVersionUID = -7374915404433933232L;
private Integer id;
private AddressType addressType;
private int typeSite;
private String streetName;
private String city;
private String country;
private String zipcode;
public Address() {
}
public Address(int typeSite) {
this.typeSite = typeSite;
}
public Address(AddressType addressType, int typeSite, String streetName,
String city, String country, String zipcode) {
this.addressType = addressType;
this.typeSite = typeSite;
this.streetName = streetName;
this.city = city;
this.country = country;
this.zipcode = zipcode;
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "addressType")
public AddressType getAddressType() {
return this.addressType;
}
public void setAddressType(AddressType addressType) {
this.addressType = addressType;
}
@Column(name = "typeSite", nullable = false)
public int getTypeSite() {
return this.typeSite;
}
public void setTypeSite(int typeSite) {
this.typeSite = typeSite;
}
@Column(name = "streetName", length = 45)
public String getStreetName() {
return this.streetName;
}
public void setStreetName(String streetName) {
this.streetName = streetName;
}
@Column(name = "city", length = 45)
public String getCity() {
return this.city;
}
public void setCity(String city) {
this.city = city;
}
@Column(name = "country", length = 45)
public String getCountry() {
return this.country;
}
public void setCountry(String country) {
this.country = country;
}
@Column(name = "zipcode", length = 45)
public String getZipcode() {
return this.zipcode;
}
public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}
}
<h:outputLabel for="addressBeanExampleCountry" value="Country:" />
<h:panelGroup>
<!-- <h:inputText id="addressBeanExampleCountry" maxLength="45" value="#{addressBean.example.country}"/> -->
<h:selectOneMenu id="addressBeanExampleCountry" value="#{addressBean.example.country}">
<f:selectItem />
<f:selectItems itemLabel="#{forgeview:display(_item.name)}" itemValue="#{_item}" value="#{countryBean.all}" var="_item" />
</h:selectOneMenu>
<h:message for="addressBeanExampleCountry" styleClass="error" />
</h:panelGroup>
<h:outputText />
<h:outputLabel for="addressBeanExampleState" value="State:" rendered="#{not empty addressBean.example.country}"/>
<h:panelGroup rendered="#{not empty addressBean.example.country}">
<p:autoComplete id="state" value="#{stateBean.state}" completeMethod="#{stateBean.getAllByCountry()}" minQueryLength="3"/>
<h:message for="addressBeanExampleState" styleClass="error" />
</h:panelGroup>