更改之前在 faces-config.xml 中初始化的 beans

发布于 2024-10-20 05:49:38 字数 1308 浏览 2 评论 0原文

public class MyBean {
    private Integer [] myField;


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

我以这种方式在 faces-config.xml 中初始化同一个 bean

<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>

然后,在应用程序中我想更改这些值。要做到这一点:

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

但这是不可能的,Integer [] results = myBean.getMyfield() 给了我一个null。不管怎样,在我的应用程序的界面中,我可以看到 bean 已正确初始化,因为它保存了值 6、12 和 24。
有什么帮助吗? 提前致谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

治碍 2024-10-27 05:49:38

当您使用 new 实例化 MyBean 时,它不会查找 Faces-Config。它只会使用构造函数创建一个对象。

如果您使用jsf2.0,则在初始化上下文时使用 Bean 进行初始化

@ManagedBean(eager=true)

,并从作用域映射中检索 Bean 实例。如果它的应用范围是bean的话。

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

更新:

你的托管bean应该看起来像这样,

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

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

如果你只想在另一个bean中使用你的bean,那么正如BalusC建议的那样,只需注入它并获取填充值,而不使用new

例如:如果您希望将 MyBean 填充到 SomeOtherBean 中,然后

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

为 jsf 1.2 Update

,没有注释,您需要配置您的 faces-config.xml 如下所示

<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>

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>
伤感在游骋 2024-10-27 05:49:38

您正在使用 new 创建对象,如 MyBean myBean = new MyBean();

所以您肯定会得到 null。

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

So you are sure to get null.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文