简体   繁体   中英

Changing beans initialized before in faces-config.xml

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

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>

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

So you are sure to get null.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM