2013-10-04 3 views
0

저는 JSF를 처음 사용하고있어서 문제가 있습니다.JSF 다른 하나를 기반으로 selectOneMenu 업데이트

나는 국가 목록을 표시하기위한 selectOneMenu가 있습니다. 사용자가 국가 중 하나를 선택하면 도시가있는 다른 selectOneMenu (내 경우 지역)가 자동으로 채워 져야합니다.

동일한 작업을 수행하기 위해 여러 번의 반복을 시도했지만 도움이되지 않았습니다. 나를 도울 다른 것이 필요한지 알려주십시오. 정말 고맙겠습니다.

는 UPDATE : 여기

리스너 추가 한 후 코드입니다 :

 <p:outputLabel for="countryRegistration">#{msg['country']}:</p:outputLabel> 
    <p:selectOneMenu id="countryRegistration" value="#{mbcActor.geoData.country}" style="width:120px;" > 
     <f:attribute name="country" value="java.util.List" /> 
     <f:converter converterId="ViewScopedObjectConverter"/> 
     <f:selectItem itemLabel="#{msg['selectCountry']}" itemValue=""/> 
     <f:selectItems value="#{geoLists.countryList}" var="country" itemLabel="#{country.name}"/> 
     <p:ajax listener="#{mbcActor.geoData.updateRegions}" render="cityRegistration"/> 
    </p:selectOneMenu> 

    <p:message for="countryRegistration"/> 

    <p:outputLabel for="cityRegistration">#{msg['city']}:</p:outputLabel> 

    <p:selectOneMenu id="cityRegistration" value="#{mbcActor.geoData.region}" style="width:120px;"> 
     <f:attribute name="regions" value="java.util.List" /> 
     <f:converter converterId="ViewScopedObjectConverter"/> 
     <f:selectItem itemLabel="#{msg['selectCity']}" itemValue=""/> 
     <!--<f:selectItems value="#{geoLists.regionForCountry}" var="region" itemLabel="#{region.name}"/>--> 
     <f:selectItems value="#{geoLists.getRegionForCountry(country)}" var="region" itemLabel="#{region.description}"/> 
    </p:selectOneMenu> 
    <p:message for="cityRegistration"/> 

을하지만 말한다 :

 Oct 05, 2013 2:04:53 AM com.sun.faces.lifecycle.InvokeApplicationPhase execute 
WARNING: Target Unreachable, identifier 'mbcActor' resolved to null 
javax.el.PropertyNotFoundException: Target Unreachable, identifier 'mbcActor' resolved to null 
    at org.apache.el.parser.AstValue.getTarget(AstValue.java:98) 
    at org.apache.el.parser.AstValue.invoke(AstValue.java:259) 
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:274) 
    at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:39) 
    at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50) 
    at org.primefaces.component.behavior.ajax.AjaxBehaviorListenerImpl.processCustomListener(AjaxBehaviorListenerImpl.java:70) 
    at org.primefaces.component.behavior.ajax.AjaxBehaviorListenerImpl.processArgListener(AjaxBehaviorListenerImpl.java:59) 
    at org.primefaces.component.behavior.ajax.AjaxBehaviorListenerImpl.processAjaxBehavior(AjaxBehaviorListenerImpl.java:47) 

콩 :

package xxx.core.entities; 

// Generated Oct 20, 2009 11:07:18 AM by Hibernate Tools 3.2.2.GA 

import javax.faces.event.ValueChangeEvent; 
import javax.inject.Inject; 
import javax.persistence.*; 

import java.util.ArrayList; 
import java.util.List; 

import static javax.persistence.GenerationType.IDENTITY; 

/** 
* DataActorGeo 
*/ 
@Entity 
@Table(name = "dataActorGeo") 
// @org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE) 
@Access(AccessType.PROPERTY) 
public class DataActorGeo implements java.io.Serializable { 

    private Integer id; 
    private Country country; 
    private Region region; 
    private String city; 
    private String province; 
    private String address; 
    private String addressLine2; 
    private String postCode; 
    private String nationality; 
    private String phone1; 
    private String phone2; 

    private Double latitude; 
    private Double longitude; 

    private Actor actor; 
    private List<Region> regions; 

    @Inject 
    EntityManager entityManager; 

    public void updateRegions() { 
     //Country ctry = (Country) event.getNewValue(); 
     try{ 
      System.out.println("bbbbbbbbbbbbbbbbbbbbbbb" + country.toString()); 
     // if (regions == null) { 
       this.regions = entityManager.createQuery("select r From Region r Where r.country.id = :countryId order by r.description ") 
         .setParameter("countryId", country.getId()) 
         .getResultList(); 
       //countryIdCache = country.getId(); 
      // } 
      System.out.println("aaaaaaaaaaa" + regions.toString()); 
     } catch(Exception e){System.out.println("cccccccccc"); }//return new ArrayList<Region>(); } 
     //return regions; 
    } 

    public DataActorGeo() { 
    } 

    public DataActorGeo(Country country, String city) { 
     this.country = country; 
     this.city = city; 
    } 

    public DataActorGeo(Country country, Region region, String city, String address, String postCode, String phone1, String phone2, Actor actor) { 
     this.country = country; 
     this.region = region; 
     this.city = city; 
     this.address = address; 
     this.postCode = postCode; 
     this.phone1 = phone1; 
     this.phone2 = phone2; 
     this.actor = actor; 
    } 

    @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 = "Country") 
    // @IndexedEmbedded(prefix = "country.") 
    public Country getCountry() { 
     return this.country; 
    } 

    public void setCountry(Country country) { 
     this.country = country; 
    } 

    @ManyToOne(fetch = FetchType.LAZY) 
    @JoinColumn(name = "Region") 
    // @IndexedEmbedded(prefix = "region.") 
    public Region getRegion() { 
     return this.region; 
    } 

    public void setRegion(Region region) { 
     this.region = region; 
    } 

    public List<Region> getRegions() 
    { 
     return this.regions; 
    } 

    public void setRegions(List<Region> regions) 
    { 
     this.regions=regions; 
    } 

    @Column(name = "City") 
    // //@Field(index = Index.UN_TOKENIZED) 
    public String getCity() { 
     return this.city; 
    } 

    public void setCity(String city) { 
     this.city = city; 
    } 

    @Column(name = "province") 
    public String getProvince() { 
     return province; 
    } 

    public void setProvince(String province) { 
     this.province = province; 
    } 

    @Column(name = "address") 
    // //@Field(index = Index.TOKENIZED) 
    public String getAddress() { 
     return this.address; 
    } 

    public void setAddress(String address) { 
     this.address = address; 
    } 

    @Column(name = "addressLine2") 
    // //@Field(index = Index.TOKENIZED) 
    public String getAddressLine2() { 
     return addressLine2; 
    } 

    public void setAddressLine2(String addressLine2) { 
     this.addressLine2 = addressLine2; 
    } 

    @Column(name = "postCode") 
    // //@Field(index = Index.UN_TOKENIZED) 
    public String getPostCode() { 
     return this.postCode; 
    } 

    public void setPostCode(String postCode) { 
     this.postCode = postCode; 
    } 

    @Column(name = "nationality") 
    // //@Field(index = Index.UN_TOKENIZED) 
    public String getNationality() { 
     return nationality; 
    } 

    public void setNationality(String nationality) { 
     this.nationality = nationality; 
    } 

    @Column(name = "phone1") 
    // //@Field(index = Index.UN_TOKENIZED) 
    public String getPhone1() { 
     return phone1; 
    } 

    public void setPhone1(String phone1) { 
     this.phone1 = phone1; 
    } 

    @Column(name = "phone2") 
    // //@Field(index = Index.UN_TOKENIZED) 
    public String getPhone2() { 
     return phone2; 
    } 

    public void setPhone2(String phone2) { 
     this.phone2 = phone2; 
    } 

    @OneToOne(mappedBy = "geoData") 
    public Actor getActor() { 
     return actor; 
    } 

    public void setActor(Actor actor) { 
     this.actor = actor; 
    } 

    @Column(name = "latitude") 
    public Double getLatitude() { 
     return latitude; 
    } 

    @Column(name = "longitude") 
    public Double getLongitude() { 
     return longitude; 
    } 

    public void setLatitude(Double latitude) { 
     this.latitude = latitude; 
    } 

    public void setLongitude(Double longitude) { 
     this.longitude = longitude; 
    } 
} 









package xxx.lists; 

import xxx.application.configuration.ISNetApp; 
import xxx.core.entities.Country; 
import xxx.core.entities.Region; 

import javax.faces.bean.ViewScoped; 
import javax.inject.Inject; 
import javax.inject.Named; 
import javax.persistence.EntityManager; 
import javax.persistence.NoResultException; 
import java.io.Serializable; 
import java.util.ArrayList; 
import java.util.List; 

@Named 
@ViewScoped 
public class GeoLists implements Serializable { 

    @Inject 
    EntityManager entityManager; 

    @Inject 
    ISNetApp sNetApp; 

    private List<Country> countryList; 

    public List<Country> getCountryList() { 
     if (countryList == null) { 
      String query = "select c from Country c order by c.ordering, c.name"; 
      countryList = (List<Country>) entityManager.createQuery(query).getResultList(); 
     } 
     return countryList; 
    } 

    public void setCountryList(List<Country> countryList) { 
     this.countryList = countryList; 
    } 

    private List<Country> deliveryCountryList; 

    public List<Country> getDeliveryCountryList() { 
     if (deliveryCountryList == null) { 
      String query = "select c from Country c where c.groups LIKE '%D,%' order by c.ordering, c.name"; 
      deliveryCountryList = (List<Country>) entityManager.createQuery(query).getResultList(); 
     } 
     return deliveryCountryList; 
    } 

    public void setDeliveryCountryList(List<Country> deliveryCountryList) { 
     this.deliveryCountryList = deliveryCountryList; 
    } 

    public Country getCountryByIsoCode2(String isoCode2) { 
     String query = "select c from Country c where c.isoCode2=:ic"; 
     try { 
      return (Country) entityManager.createQuery(query).setParameter("ic", isoCode2).getSingleResult(); 
     } catch (NoResultException e) { 
      return null; 
     } 
    } 

    private List<Region> regionForCountry; 
    private int countryIdCache; 

    private List<Region> regions; 

    public List<Region> getRegions(){ 
     return this.regions; 
    } 

    public void setRegions(List<Region> regions){ 
     this.regions = regions; 

    } 

    public List<Region> getRegionForCountryById(Long countryId) { 
     Country country = entityManager.find(Country.class, countryId.intValue()); 
     return getRegionForCountry(country); 
    } 

    public List<Region> getRegionForCountry(Country country) { 
     try{ 
      System.out.println("bbbbbbbbbbbbbbbbbbbbbbb" + country.toString()); 
      //if (regionForCountry == null || countryIdCache != country.getId()) { 
      regionForCountry = entityManager.createQuery("select r From Region r Where r.country.id = :countryId order by r.description ") 
        .setParameter("countryId", 1)//country.getId()) 
        .getResultList(); 
      // countryIdCache = country.getId(); 
      //} 
      // System.out.println("aaaaaaaaaaa" + regionForCountry.toString()); 
     } catch(Exception e){System.out.println("cccccccccc"); return new ArrayList<Region>(); } 
     return regionForCountry; 
    } 

    public void setRegionForCountry(List<Region> regionForCountry) { 
     this.regionForCountry = regionForCountry; 
    } 

    private Country mainCountry; 

    private Country country; 
    public void setCountry(Country country) { 
     this.country=country; 
    } 

    public Country getCountry(){ 
     return this.country; 
    } 

    public Country getMainCountry() { 
     if (mainCountry == null) { 
      mainCountry = entityManager.find(Country.class, sNetApp.getMainCountryId()); 
     } 
     return mainCountry; 
    } 

    public void setMainCountry(Country mainCountry) { 
     this.mainCountry = mainCountry; 
    } 

    private List<Country> specificGroupCountryList; 
    private String groupCache; 

    public List<Country> getSpecificGroupCountryList(String group) { 
     if (specificGroupCountryList == null || groupCache == null || groupCache.compareTo(group) != 0) { 
      String query = "from Country where groups LIKE '%" + group + ",%' order by ordering, name"; 
      specificGroupCountryList = (List<Country>) entityManager.createQuery(query).getResultList(); 
     } 
     return specificGroupCountryList; 
    } 

    public void setSpecificGroupCountryList(List<Country> specificGroupCountryList) { 
     this.specificGroupCountryList = specificGroupCountryList; 
    } 

} 

mbcActor과 같이 정의 PARAM입니다 :

<ui:param name="mbcActor" value="#{registrationHelper.newActor}"/> 

AbstractActor는 :

@MappedSuperclass 
public class AbstractActor extends AbstractGenericElement implements java.io.Serializable, IGenericElement, EmailContact { 

    private static Logger log = LoggerFactory.getLogger(AbstractActor.class); 

    public static enum Gender { 
     Male, 
     Female 
    } 

    /** 
    * Transient 
    */ 
    private boolean selected; 

    private Integer id; 
    private Long version; 
    private byte type; 
    private String language; 
    private byte status; 
    private byte publicStatus; 
    private String permalink; 
    private Boolean customPermalink; 
    private String displayName; 
    private String forename; 
    private String surname; 

    private Gender gender; 
    private Date birthday; 

    private String mobile; 

    private Byte subType; 

    private String stringValue1; 
    private String stringValue2; 
    private String stringValue3; 

    @Transient 
    @XmlTransient 
    private Integer age; 

    private String profileMessage; 
    private String statusMessage; 
    private String email; 
    private String username; 

    private String passwordHash; 
    private int passwordHashIterations; 
    private String salt; 

    private String timezone; 

    private String activationCode; 
    private Boolean activationEmailSent; 

    private Integer completitionPercentage; 

    private Integer ordering; 

    // Stats 
    private ActorStats stats; 

    // Extensions 
    private DataActorGeo geoData; 
    private DataActorExtended dataActorExtended; 

    private Boolean acceptNewsletter; 
    private Boolean emailAlertsEnabled; 

    private Set<ActorTag> actorTags = new HashSet<ActorTag>(0); 

    private List<Role> roles = new ArrayList<Role>(0); 

    private Actor newDataToBeModerated; 

    private Date expireDate; 

    private Integer points; 
    private BigDecimal wallet; 

    private Byte emailAlertsType; 
    private Byte emailOffersType; 

    private String emailHash; 

    // Data blob 
    private byte[] data; 

    private Set<ActorInCategory> actorInCategories = new HashSet<ActorInCategory>(
      0); 

    private List<ActorAttribute> actorAttributes = new ArrayList<ActorAttribute>(0); 


    public AbstractActor() { 
     this.version = 1l; 
     this.activationEmailSent = false; 
     this.acceptNewsletter = true; 
     this.emailAlertsEnabled = true; 
     this.completitionPercentage = 0; 
     this.customPermalink = false; 
     this.wallet = new BigDecimal(0); 
     this.points = 0; 
     this.emailAlertsType = ActorConstants.RECURRING_EMAIL_TYPE_NONE; 
     this.emailOffersType = ActorConstants.RECURRING_EMAIL_TYPE_NONE; 
    } 

    public AbstractActor(byte type, byte status) { 
     this.version = 1l; 
     this.type = type; 
     this.status = status; 
     this.publicStatus = ActorConstants.PUBLIC_STATUS_OFFLINE; 
     this.activationEmailSent = false; 
     this.acceptNewsletter = true; 
     this.emailAlertsEnabled = true; 
     this.completitionPercentage = 0; 
     this.customPermalink = false; 
     this.wallet = new BigDecimal(0); 
     this.points = 0; 
     this.emailAlertsType = ActorConstants.RECURRING_EMAIL_TYPE_NONE; 
     this.emailOffersType = ActorConstants.RECURRING_EMAIL_TYPE_NONE; 
    } 

    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL) 
    @JoinColumn(name = "geoData", nullable = true) 
    @org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE) 
    public DataActorGeo getGeoData() { 
     return this.geoData; 
    } 

    public void setGeoData(DataActorGeo geoData) { 
     this.geoData = geoData; 
    } 


    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "actor") 
    public Set<ActorInCategory> getActorInCategories() { 
     return this.actorInCategories; 
    } 

    public void setActorInCategories(Set<ActorInCategory> actorInCategories) { 
     this.actorInCategories = actorInCategories; 
    } 

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "actor") 
    public List<ActorAttribute> getActorAttributes() { 
     return this.actorAttributes; 
    } 

    public void setActorAttributes(List<ActorAttribute> actorAttributes) { 
     this.actorAttributes = actorAttributes; 
    } 
} 
+0

같은 방법으로 그것을 얻을. 지역}'필드. –

+0

가능한 복제 http://stackoverflow.com/questions/12533945/selectonemenu-updates-other-selectonemenu – Omar

+0

내가 말했듯이, 나는 JSF를 처음 사용한다. 당신의 대답을 자세히 설명해 주시겠습니까? 감사합니다. – neeagl

답변

1

귀하의 <p:ajax> 태그는 미리 정의되어 있지 않은 render 속성을 사용합니다. 원하는 속성은 update입니다.

+0

그 중 하나에서 작동하지 않았습니다. – neeagl

+1

글쎄, 나는 정말로 당신을 다르게 도울 수 없다. 오류 메시지에 따르면, 문제는'mbcActor'가 정의되어 있지 않다는 것입니다. mbcActor가''{{registrationHelper.newActor} ''}를 정의했지만, 'registrationHelper' 또는'newActor'를 정의하는 다른 코드를 보여주지 않았습니다. 그 둘 중 하나가 정의되지 않은 것 같습니다. 거기에서 거꾸로 작업 해보십시오. – patstuart

+0

'AbstractActor'는'mbcActor'를 나타내는 빈입니다. 나는 그 질문에 그 정의를 주었다. – neeagl

0

registrationHelper.newActor이 Managed Bean의 인스턴스라고 가정 할 때, 아마도 초기화되지 않았을 것입니다. 왜 RegistrationHelper의 PostConstruct의 그것을 만들어,이 있는지 확인하지 않습니다, 또는 당신은 당신의`# {mbcActor.geoData의 데이터를 채울 리스너 방법이 부족

public AbstractActor createNewActor() { 
     newActor = new AbstractActor(); 
    return newActor; 
    } 

<ui:param name="mbcActor" value="#{registrationHelper.createNewActor}"/> 
관련 문제