使用 CDI @Named 注解时 Bean 属性变为 null
我在 Tomcat 7 容器中使用 JSF2+CDI。我发现一个奇怪的问题,我有一个用 @Named 注释注释的 @RequestScoped 托管 bean,我在 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
确保导入正确的包。如果将 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 importjavax.enterprise.context.RequestScoped;
. However, if you use@ManagedBean
, you need to importjavax.faces.bean.RequestScoped;