June 16 2016

JSF managed property

JSF managed property is used to access one managed bean in other managed bean (Dependency Injection).Managed property can be configured using @ManagedProperty annotation or by adding <managed-property> to <managed-bean> in faces-config.xml

In the below example, ‘requestManagedBean’ managed bean have managed property for ‘verificationManagedBean’ managed bean

Note

  • ‘requestManagedBean’ managed bean should have same or shorter scope than the ‘verificationManagedBean’ managed bean
  • ‘requestManagedBean’ managed bean should have setter method for ‘verificationManagedBean’ managed property
package com.answersz.jsf;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;

@ManagedBean(name = "requestManagedBean")
@SessionScoped
public class RequestManagedBean {

 @ManagedProperty(value = "#{verificationManagedBean}")
 private VerificationManagedBean verificationManagedBean;

 public void submit() {
 System.out.println("Submit");
 verificationManagedBean.verify();
 }

 public VerificationManagedBean getVerificationManagedBean() {
 return verificationManagedBean;
 }

 public void setVerificationManagedBean(VerificationManagedBean verificationManagedBean) {
 this.verificationManagedBean = verificationManagedBean;
 }

}
package com.answersz.jsf;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean(name = "verificationManagedBean")
@SessionScoped
public class VerificationManagedBean {

 private boolean verified = false;

 public void verify() {
 // Verification code
 System.out.println("Verified :" + verified);
 }

 public boolean isVerified() {
 return verified;
 }

 public void setVerified(boolean verified) {
 this.verified = verified;
 }

}


Copyright © 2014. All rights reserved.

Posted June 16, 2016 by answersz in category Jsf

Leave a Comment

Your email address will not be published. Required fields are marked *

*