它可以很好地加载stateList,但是当我更改状态值时,它将调用backbean,但addressBO为null。
我该如何解决?还有其他方法吗?

提前致谢。

<h:selectOneMenu id="stateList" value="#{newUserController.address.stateID}">
 <f:selectItems value="#{addressController.stateList}" />
 <f:valueChangeListener type="controller.address.AddressController"/>
 <f:ajax event="change" render="cidadeList"/>
</h:selectOneMenu>

<h:selectOneMenu id="cityList" value="#{newUserController.address.cityID}">
 <f:selectItems value="#{addressController.cityList}" />
</h:selectOneMenu>


BackBean

package controller.address;

@ManagedBean
@RequestScoped
public class AddressController implements ValueChangeListener {

 @EJB
 private AddressBO addressBO;

 @PostConstruct
 public void firstThingToDo() {
  stateList = enderecoBO.loadStateListSelectItem();
 }

 @Override
 public void processValueChange(ValueChangeEvent event) throws AbortProcessingException {
  String state = (String) event.getNewValue();
  cityList = addressBO.loadCityList(state);
 }

}


设置地址和用户的值的另一个backbean在这种情况下不会出现。

package controller.user;

@ManagedBean
@RequestScoped
public class NewUserController {

 private Address address;
 private User user;

 @EJB
 private NewUserBO newUserBO;

 public void registerNewUser() {
  newUserBO.register(user, address);
 }

 public Address getAddress() {
  return this.address;
 }

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

 public User getUser() {
  return this.user;
 }

 public void setUser(User user) {
  this.user = user;
 }

}

最佳答案

第一个问题是您要在<f:valueChangeListener>标记中创建该类的全新实例。此实例与@ManagedBean引用的实例不同,并且托管Bean相关的注释也不会在该实例上调用。您希望改为在valueChangeListener="#{addressController.processValueChange}"上使用<h:selectOneMenu>。但这并不能立即解决所有问题。所做的更改很大,以至于第二个列表的行为不符合预期。

第二个问题是在Validation阶段调用了值更改侦听器方法。这是更改/加载模型值的错误时刻。您应该在“调用操作”阶段执行此操作。使用listener<f:ajax>属性。

所以:

<h:selectOneMenu id="stateList" value="#{newUserController.address.stateID}">
    <f:selectItems value="#{addressController.stateList}" />
    <f:ajax listener="#{addressController.changeState}" render="cidadeList" />
</h:selectOneMenu>




@ManagedBean
@RequestScoped
public class AddressController {

    @EJB
    private AddressBO addressBO;

    @PostConstruct
    public void init() {
        stateList = enderecoBO.loadStateListSelectItem();
    }

    public void changeState(AjaxBehaviorEvent event) {
        String state = (String) ((UIInput) event.getComponent()).getValue();
        cityList = addressBO.loadCityList(state);
    }

    // ...
}

关于java - f:ValueChangeListener使EJB注入(inject)无效,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6611644/

10-09 00:27