1
public class MyBean {
    private Integer [] myField;


    public Integer [] getMyField() {
        return myField;
    }
    public void setMyField(Integer [] myField) {
        this.myField = myField;
    }

And I initialize this same bean in faces-config.xml in this way

<managed-bean-name>myBean</managed-bean-name>
        <managed-bean-class>com.path.bean.MyBean</managed-bean-class>
        <managed-bean-scope>request</managed-bean-scope>
        <managed-property>
            <property-name>myField</property-name>
            <list-entries>
                    <value>6</value>
                    <value>12</value>
                    <value>24</value>       
            </list-entries>
        </managed-property>        
    </managed-bean>

Then, in the application I want to change these values. To do it:

        MyBean myBean = new MyBean();
        Integer [] results = myBean.getMyfield();
        //Change the value of this array
        visualizationBean.setResultsPerPage(results);

But this is not possible, Integer [] results = myBean.getMyfield() gives me a null. Anyway, in the interface of my application, I can see that the bean is correctly initialize, because it holds the values 6, 12 and 24.
Any kind of help?? Thanks in advance

Blanca Hdez
  • 3,513
  • 19
  • 71
  • 93

2 Answers2

3

As you instantiate MyBean using new, it won't look for the Faces-Config. and it will simply create an object using constructor.

If you are using jsf2.0 make bean to initialize when your context is being initialized using

@ManagedBean(eager=true)

and retrieve the Bean instance from scoped map. if its application scoped bean the.

MyBean mb = (MyBean)FacesContext.getCurrentInstance().getExternalContext().getApplicationMap().get("beanName");

Update:

your managed bean should look like ,

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean(eager="true")
@RequestScoped
public class MyBean {

if you just want to use your bean in another bean then as BalusC suggested simply inject it and get the filled value , without using new

for example: if you want your MyBean poppulated in SomeOtherBean then

@ManagedBean()
@RequestScoped
public class SomeOtherBean {
   @ManagedProperty(value="#{myBean}") 
   private MyBean myBean;
   //getters & setters of myBean

Update

for jsf 1.2 , there is no annotations, you need to configure your faces-config.xml as shown below

<managed-bean>
    <managed-bean-name>myBean</managed-bean-name>
    <managed-bean-class>com.example.my.MyBean</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
</managed-bean>

<managed-bean>
    <managed-bean-name>someAnotherBean</managed-bean-name>
    <managed-bean-class>com.example.some.AnotherBean</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
    <managed-property>
        <property-name>myBean</property-name>
        <value>#{myBean}</value>
    </managed-property>
</managed-bean>
jmj
  • 237,923
  • 42
  • 401
  • 438
  • I am using jsf2.0, but where to include tha tag @ManagedBean(eager=true) ??? In the bean declaration??There I'm getting error on my IDE – Blanca Hdez Mar 04 '11 at 09:26
  • We are migrating from jsf1 to jsf2, having some problems. There would be no other solution in jsf1??? @Jigar – Blanca Hdez Mar 07 '11 at 08:49
  • Just if anyone wonders - to retrieve SessionScoped bean you must use `getSessionMap` instead of `getApplicationMap`. – Nux May 01 '12 at 21:47
1

You are creating object using new like MyBean myBean = new MyBean();

So you are sure to get null.

niksvp
  • 5,545
  • 2
  • 24
  • 41