神奇好望角 The Magical Cape of Good Hope

          庸人不必自擾,智者何需千慮?
          posts - 26, comments - 50, trackbacks - 0, articles - 11
            BlogJava :: 首頁 ::  :: 聯(lián)系 :: 聚合  :: 管理
          JSF 2.0 中定義受管 Bean 的三種途徑的比較 Comparison of Three Ways to Define Managed Beans in JSF 2.0
          JSF 2.0 大量采用標(biāo)注,從而使 web/WEB-INF/faces-config.xml 不再必需。本文介紹并比較了三種途徑來定義可從頁面上的 EL 表達(dá)式中引用的受管 Bean。 JSF 2.0 heavily adopts annotations, so web/WEB-INF/faces-config.xml becomes optional. This article introduces and compare three ways to define a managed bean that can be referred in an EL expression on the page.
          現(xiàn)在假設(shè)你有下面這個(gè) Bean 類,并想讓它受管于會(huì)話范圍: Now assum you have a bean class like this and want it to be managed in session scope:
          1. public class MyManagedBean implements Serializable {...}
          在 JSF 2.0 之前,你需要將它配置到 web/WEB-INF/faces-config.xml 中: Prior to JSF 2.0, you need to configure it in web/WEB-INF/faces-config.xml:
          1. <managed-bean>
          2.     <managed-bean-name>mmb</managed-bean-name>
          3.     <managed-bean-class>com.mycom.MyManagedBean</managed-bean-class>
          4.     <managed-bean-scope>session</managed-bean-scope>
          5. </managed-bean>
          這不僅枯燥,而且難以管理、容易出錯(cuò)。faces-config.xml 不僅用于配置受管 Bean,還用于導(dǎo)航規(guī)則。如果你在做一個(gè)大的互聯(lián)網(wǎng)項(xiàng)目,該文件可能會(huì)增大到上千行。你還可能會(huì)犯拼寫錯(cuò)誤,例如寫錯(cuò) Bean 類名,使你直至運(yùn)行時(shí)才意識(shí)到問題。顯然在 JSF 2.0 中,這不是優(yōu)先的途徑。 This is not only tedious but also hard to manage and error prone. faces-config.xml is not only used for configuring managed beans, but also for navigation rules. If you're working with a big web project, this file can grow up to one thousand lines. It's also possible for you to misspell some words, for example the bean class name, that makes you unaware the problem until at runtime. Obviously this way shouldn't be preferred in JSF 2.0.
          第二種途徑由 JSR 314,也就是 JavaServer Faces 2.0 本身提供。你只需將兩個(gè)標(biāo)注應(yīng)用到類定義上: The second way is provided by JSR 314, that is JavaServer Faces 2.0 itself. You just apply two annotations onto the class definition:
          1. @javax.faces.bean.ManagedBean(name = "mmb")
          2. @javax.faces.bean.SessionScoped
          3. public class MyManagedBean implements Serializable {...}
          沒什么特別的,但直截了當(dāng)!用這些標(biāo)注,你就可以從此避免 XML 配置的枯燥和不小心拼錯(cuò)字。然而,這并不是最佳途徑,因?yàn)?JSR 299 中定義的 CDI(用于 Java™ EE 平臺(tái)的上下文和依賴注入)提供了類似且強(qiáng)大得多的標(biāo)注: Nothing special but just nice and straightforward! With these annotations, you're set free from the tedium of XML configuration and carelessness of typo. However, this is still not the best way, because CDI specified in JSR 299 (Contexts and Dependency Injection for the Java™ EE platform) offers similar and far more powerful annotations:
          1. @javax.inject.Named("mmb")
          2. @javax.enterprise.context.SessionScoped
          3. public class MyManagedBean implements Serializable {...}
          為什么要優(yōu)先采用 CDI?因?yàn)檎缙涿怯糜谝蕾囎⑷氲耐ㄓ每蚣埽簿褪钦f它不但可用于 JSF,還能用于任何其他的 Java EE 6 技術(shù),而且我聽說 Spring 3.0 也將支持它。詳細(xì)的文檔可從 Weld 主頁找到,這里 Weld 是 JSR 299 的參考實(shí)現(xiàn)。 Why should we prefer CDI? Beacuse as named it's a general purposed framework for dependency injection, which means it can not only used with JSF, but also with any other Java EE 6 tenologies, and I've heard Spring 3.0 will support it too. Detailed documentations can be found at Weld Home, where Weld is the reference implementation for JSR 299.
          主站蜘蛛池模板: 塔河县| 隆林| 塘沽区| 花垣县| 镇赉县| 遂川县| 湄潭县| 岳普湖县| 孟津县| 英德市| 阿勒泰市| 海安县| 海门市| 瑞安市| 尚志市| 馆陶县| 格尔木市| 靖远县| 通化县| 尚义县| 屯门区| 天柱县| 吉水县| 哈密市| 徐闻县| 广平县| 林州市| 涡阳县| 和静县| 云霄县| 田阳县| 民和| 贵港市| 永定县| 陆河县| 芒康县| 南陵县| 余江县| 诏安县| 樟树市| 抚松县|