填充子jSF selectOneMenu - 隐式值验证错误

时间:2013-02-15 16:06:18

标签: ajax jsf java-ee primefaces selectonemenu

正如您在屏幕截图中看到的,我对属性“member.city”有一个 JSF隐式值验证错误。 目的是通过ajax - 在上一个Country Select one菜单上选择一个国家后填充城市选择一个菜单。 这是通过使用 selectOneMenu更改侦听器 ajax 来更新组件显示城市列表来实现的。但是,我收到验证错误,表单未提交。

Validation error screenshot

我尝试使用实体使用和不使用(hascode和equals方法),但是徒劳无功。 我使用分隔的Managed Bean ViewScoped 来检索已过滤的列表,但是徒劳无功。 **

  

如果我用检索全部的列表替换已过滤的城市列表   城市(在xhtml表单中,将<f:selectItems value="#{cityBean.citiesByCountry}" />替换为<f:selectItems value="#{cityBean.all}" />,没有验证错误,表单已成功获取   提交。

** 这是代码:

形式:

<p:outputLabel for="country" value="#{i18n.country}" />
                    <p:selectOneMenu valueChangeListener="#{cityBean.selectCountry}" rendered="true" id="country" value="#{newMember.country}" converter="#{countryBean.converter}" 
                        style="width:160px;text-align:left;border:thin solid gray;"
                        required="true" requiredMessage="#{i18n.required}">                     
                        <f:selectItems value="#{countryBean.all}" var="countryname" itemLabel="#{countryname.name}"/>
                        <p:ajax event="change" update="city" />
                    </p:selectOneMenu>
                    <p:message for="country" />

                    <p:outputLabel for="city" value="#{i18n.city}" />
                    <p:selectOneMenu rendered="true" id="city" value="#{newMember.city}" converter="#{cityBean.converter}" converterMessage="Conversion error!"
                        style="width:160px;text-align:left;border:thin solid gray;"
                        required="true" requiredMessage="#{i18n.required}">                                         
                        <f:selectItems value="#{cityBean.citiesByCountry}" var="cityname" itemLabel="#{cityname.cityname}"/>                        
                    </p:selectOneMenu>
                    <p:message for="city" />

城市实体负责人:

@XmlRootElement
@Entity
@Table(name = "city", schema = "public")
@NamedQueries({
        @NamedQuery(name = "City.findAll", query = "SELECT c FROM City c"),
        @NamedQuery(name = "City.findById", query = "SELECT c FROM City c WHERE c.id = :id"),
        @NamedQuery(name = "City.findByCountry", query = "SELECT c FROM City c WHERE c.country = :country"),
        @NamedQuery(name = "City.findByCountryOrderedByName", query = "SELECT c FROM City c WHERE c.country = :country ORDER BY c.cityname ASC") })
@NamedNativeQueries({ @NamedNativeQuery(name = "City.findCountryNativeSQL", query = "select * from city c where c.countrybeta = :country.betacode", resultClass = City.class) })
public class City implements java.io.Serializable {

    private static final long serialVersionUID = 3364735938266980295L;
    private int id;
    private Integer version;
    private Country country;
    private String cityname;
    private String district;
    private int population;
    private Set<Member> members = new HashSet<Member>(0);

    public City() {
    }
// ...remainder of entity code (contructors and fields..)

The Bean:

@Named
@Stateful
@ConversationScoped
public class CityBean implements Serializable {

    private static final long serialVersionUID = 1L;
    private City city;
    private List<City> cityList;

    public City getCity() {
        return this.city;
    }

    private Country selectedCountry;

    @Inject
    private Conversation conversation;

    @PersistenceContext(type = PersistenceContextType.EXTENDED)
    private EntityManager entityManager;
// remainder of code....

// The List that works fine with Select one Menu
public List<City> getAll() {

        CriteriaQuery<City> criteria = this.entityManager.getCriteriaBuilder()
                .createQuery(City.class);
        return this.entityManager.createQuery(
                criteria.select(criteria.from(City.class))).getResultList();
    }

    @PostConstruct
    public void init() {
        setCityList(getCitiesByCountry());
    }

    public List<City> getCityList() {
        return cityList;
    }

    public void setCityList(List<City> cityList) {
        this.cityList = cityList;
    }

    /**
     * The List That triggers a jsf value validation error
     * @return List of Cities by Country ordered by city name
     */
    public List<City> getCitiesByCountry() {
        TypedQuery<City> query = this.entityManager.createNamedQuery(
                "City.findByCountryOrderedByName", City.class);
        query.setParameter("country", selectedCountry);
        return query.getResultList();
    }

    // The Select One Menu Listener
    public void selectCountry(ValueChangeEvent event) {

        if (event != null) {
            selectedCountry = (Country) event.getNewValue();
        }
        }

        // getters and setters....

ENV: - JSF 21 - 休眠 - JBoss AS 7.1 - PrimeFaces 3.5 - Linux 3.5 / Firefox 18.0.2

1 个答案:

答案 0 :(得分:4)

您的具体问题是由于您没有在@PostConstruct中启动对话而引起的,因此bean的行为类似于请求作用域。这样,在处理表单提交期间,城市列表将不相容地变化并变为空(因此在可用项目列表中找不到所选项目,因此验证错误)。

您需要在@PostConstruct

中开始对话
conversation.begin();

另见:


无关具体问题,使用valueChangeListener并在getter方法中执行业务逻辑是错误的方法。您根本不应该在getter方法中执行业务逻辑。这显然效率低下。他们应该只返回已经准备好的bean属性。

重写您的逻辑如下:

<p:selectOneMenu value="#{bean.country}" ...>
    <f:selectItems value="#{bean.countries}" />
    <p:ajax listener="#{bean.updateCities}" update="city" />
</p:selectOneMenu>

<p:selectOneMenu id="city" value="#{bean.city}" ...>                                         
    <f:selectItems value="#{bean.cities}" />
</p:selectOneMenu>

private Country country;
private List<Country> countries;
private City city;
private List<City> cities;

@PostConstruct
public void init() {
    countries = service.listCountries();

public void updateCities() {
    cities = service.listCities(country);
}

// Add/generate normal!! getters/setters.

另见: