本文介绍了使用CDI @Named注释时,Bean属性变为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Tomcat 7容器中使用JSF2 + CDI.我发现了一个奇怪的问题,我有一个@RequestScoped托管Bean,其使用@Named注释进行了注释,Iam在Facelet页面上使用了其属性.在该页面中,安排了三个连续的下拉列表.当页面加载时,我已经使用其getter方法初始化了第一个下拉列表.用相同的方法,我初始化了第二个和第三个下拉框的属性.但是第二个和第三个属性在页面中变为空.我检查了第二个和第三个属性的getter方法,发现所有这些属性均为null,甚至第一个由getter方法初始化的第一个属性也显示为null.但是,当我将@Named批注更改为@ManagedBean时,我可以正确地初始化值.

I am using JSF2+CDI in a Tomcat 7 Container. I found a strange problem, I have got one @RequestScoped managed bean annotated with @Named annotation, Iam using its properties on a Facelet page. In that page three consecutive dropdown list is arranged. when the page loads i have initialized the first dropdown list using its getter method. In that same method i have initialized the properties of second and third dropdown boxes. But the second and third properties becomes null in the page. I have checked the getter methods of the second and third properties, I found that all those properties are null and even the first property which has been initialized by the first getter method also displayed as null. But when I changed the @Named annotation to @ManagedBean i could initialize the values correctly.

推荐答案

请确保您导入了正确的程序包.如果将CDI与@Named一起使用,则需要导入javax.enterprise.context.RequestScoped;.但是,如果使用@ManagedBean,则需要导入javax.faces.bean.RequestScoped;

Make sure you import the correct package. If you use CDI with @Named, you need to import javax.enterprise.context.RequestScoped;. However, if you use @ManagedBean, you need to import javax.faces.bean.RequestScoped;

这篇关于使用CDI @Named注释时,Bean属性变为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 13:17