JSF2.0 Tomcat7中的@Named和@ManagedBean注释之间的区别 [英] Difference between @Named and @ManagedBean annotations in JSF2.0 Tomcat7

查看:1341
本文介绍了JSF2.0 Tomcat7中的@Named和@ManagedBean注释之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个noob问题,但是在很多教程和示例中,我看到这些注释的使用就好像他们做了同样的事情。



然而,我遇到了一些限制使用 @Named 一(特别是依赖注入等)我找不到一个来源的差异被解释,我会非常感谢,如果有人可以粗略概述何时使用一个或另一个。

解决方案

@Named 给予CDI托管bean一个EL名称用于查看技术,如JSF或JSP。请注意,在CDI应用程序中,您不要需要 @Named 注释来创建由CDI管理的bean(感谢@Karl的评论)



@ManagedBean 使由JSF管理的bean可以:




  • 将其注入其他@ManagedBean注释的bean(但不是@Named beans!)

  • 通过表达式语言从您的视图中访问它



看到这个相关的问题,以获取有关注射在两种类型的bean之间如何工作的更多信息。



请注意,与bean的范围不同。它们来自不同的包,但命名相同(JSF: javax.faces.bean ,CDI: javax.enterprise.context ,所以如果你包含错误的类,它通常是一个错误和混乱的根源。



从我的经验来看:尽可能使用CDI bean,因为它们更灵活比JSF管理的bean只有缺点是CDI不知道一个视图范围,所以你需要回到@ManagedBean或使用像Seam这样的第三方扩展。


This might be a noob question, however in a lot of tutorials and examples I saw these annotations used as if they did the same thing.

However I ran into some limitations using the @Named one (especially with dependency injection etc.) I couldn't find a source where the difference is explained and I'd be very thankful if someone can give a rough overview on when to use one or the other.

解决方案

@Named gives a CDI managed bean an EL name to be used in view technologies like JSF or JSP. Note that in a CDI application you don't need the @Named annotation to make a bean managed by CDI (thanks to @Karl for his comment).

@ManagedBean makes the bean managed by JSF and you can:

  • inject it into other @ManagedBean annotated beans (but not into @Named beans!)
  • access it from your views via expression language

See this related question for further information how injection works among both kind of beans.

Note that there is also a difference with the scope of the beans. They come from different packages but are named identically (JSF: javax.faces.bean, CDI: javax.enterprise.context , so it is often a source of error and confusion if you include the wrong class.

From my experience: You should use CDI beans whenever possible since they are more flexible than JSF managed beans. Only drawback is that CDI doesn't know a view scope, so you either need to fall back to @ManagedBean or use some third party extension like Seam.

这篇关于JSF2.0 Tomcat7中的@Named和@ManagedBean注释之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆