Jsf 调用action方法后,设置另一个bean的属性
莫哈拉2.2 我有两颗豆子Jsf 调用action方法后,设置另一个bean的属性,jsf,jsf-2.2,mojarra,Jsf,Jsf 2.2,Mojarra,莫哈拉2.2 我有两颗豆子 public class MyBean1{ private String myProperty1; //GET, SET, CTOR public void doAction(){ //assign something to myProperty1 } } public class MyBean2{ private String myProperty2; //GET, SET, CTOR }
public class MyBean1{
private String myProperty1;
//GET, SET, CTOR
public void doAction(){
//assign something to myProperty1
}
}
public class MyBean2{
private String myProperty2;
//GET, SET, CTOR
}
现在,我需要在doAction()
方法调用之后,通过单击按钮将属性MyBean1::myProperty1
的值分配给MyBean2::myProperty2
:
<h:commandButton action="#{myBean1.doAction}">
<f:setPropertyActionListener target="#{myBean2.myProperty2}"
value="#{myBean1.myProperty1}" />
</h:commandButton>
我注意到通过点击按钮进行的ActionEvent
广播由两个侦听器处理,其中一个来自super.broadcast(event)
调用//1
是com.sun.faces.facelets.tag.jsf.core.SetPropertyActionListenerHandler.SetPropertyListener
处理在调用动作方法之前执行
当然,我可以将action方法调用嵌入到
MyBean1:getMyProperty1()
getter中,因此myProperty1
字段将被正确初始化,但对我来说这似乎很奇怪。实现这一点的正确方法是什么?单击按钮时,myBean2.myProperty2
的值可能不可用。可能存在与作用域相关的问题,或者在单击按钮之前未显式设置或初始化该属性的值。@很抱歉,这是一个输入错误。相关检查:
public void broadcast(FacesEvent event) throws AbortProcessingException {
super.broadcast(event); //1 <-------------------- HERE
if (event instanceof ActionEvent) {
FacesContext context = getFacesContext();
MethodBinding mb = getActionListener();
if (mb != null) {
mb.invoke(context, new Object[] { event });
}
ActionListener listener =
context.getApplication().getActionListener();
if (listener != null) {
listener.processAction((ActionEvent) event);
}
}
}