vineri, 2 octombrie 2015

JSF Tip: Passing objects to managed beans

Let suppose that we have the following two beans:

@Named
@RequestScoped
public class BeanA {      
}

@Named
@RequestScoped
public class BeanB {
   
 private BeanA beanA;

 public BeanA getBeanA() {
  return beanA;
 }

 public void setBeanA(BeanA beanA) {
  this.beanA = beanA;
 }
   
 public void beanBAction(){
  System.out.println(beanA);
 }  
}

And, we need a command button that invoke BeanB#beanBAction() method and pass an instance of BeanA also. Well, you can quickly achieve this via <f:setPropertyActionListener/>, as below:

<h:form>
 <h:commandButton value="Action BeanB" action="#{beanB.beanBAction()}">              
  <f:setPropertyActionListener target="#{beanB.beanA}" value="#{beanA}" />
 </h:commandButton>
</h:form>   

Via this tip, you can easy shape the state design pattern (see JSF and State design pattern - part I

Niciun comentariu:

Trimiteți un comentariu