gpt4 book ai didi

java - :ValueChangeListener nullifying EJB injection

转载 作者:太空宇宙 更新时间:2023-11-04 08:34:59 26 4
gpt4 key购买 nike

它可以很好地加载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 相关的注释也不会被调用。您更愿意使用valueChangeListener="#{addressController.processValueChange}"关于<h:selectOneMenu>反而。但这并不能立即解决所有问题。变化很大,第二个列表的行为不符合预期。

第二个问题是在验证阶段调用值更改监听器方法。现在是更改/加载模型值的错误时机。您应该在调用操作阶段执行此操作。使用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 - :ValueChangeListener nullifying EJB injection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6611644/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com