f:ValueChangeListener取消EJB注入



它加载stateList很好,但当我改变一个状态的值,它调用backbean,但addressBO是空的。我该怎么修理它?还有别的方法吗?

提前感谢。

<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相关的注释也不会在其上调用。您宁愿在<h:selectOneMenu>上使用valueChangeListener="#{addressController.processValueChange}"。但这并不能立即解决所有问题。变化很大,第二个列表的行为不像预期的那样。

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

:

<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);
    }
    // ... 
}

相关内容

  • 没有找到相关文章