簡體   English   中英

在Glassfish上使用@ApplicationScoped(CDI)的@Named bean“ hello world”

[英]@Named bean “hello world” with @ApplicationScoped (CDI) on Glassfish

如何從使用CDI的NextClient bean到facelet中獲取輸出?

我正在嘗試使用CDI導入:

package dur.beans;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Named;


@Named("nextClient")
@ApplicationScoped
public class NextClient implements NextClientLocal {

    private int next = 1009;

    @Override
    public int getNext() {
        next = next + 1;
        return next;
    }

}

帶有小面的示例:

<!DOCTYPE    html  PUBLIC "-//W3C//DTD XHTML 1.0  Transitional//EN"  
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      >

    <h:head></h:head>
    <h:body>
        This and everything before will be ignored
        <ui:composition template="template.xhtml">
            <ui:define name="navigation">
                <ui:include src="menu.xhtml"/>
            </ui:define>
            <ui:define name="main">
                <h1>bird</h1>
                <p>
                    next   #{nextClient.next}
                </p>
            </ui:define>
        </ui:composition>
        This and everything after will be ignored
    </h:body>
</html>

但似乎沒有任何輸出來自Bean:

thufir@dur:~$ 
thufir@dur:~$ lynx -dump http://localhost:8080/EntAppWeb-war/next.xhtml                                    birds...

crud ops
     __________________________________________________________________

   [1]Home
   [2]Parrot
   [3]Eagle
   [4]Falcon
   [5]next

                                      bird

   next

References

   1. http://localhost:8080/EntAppWeb-war/next.xhtml
   2. http://localhost:8080/EntAppWeb-war/next.xhtml
   3. http://localhost:8080/EntAppWeb-war/next.xhtml
   4. http://localhost:8080/EntAppWeb-war/next.xhtml
   5. http://localhost:8080/EntAppWeb-war/next.xhtml
thufir@dur:~$ 

該示例改編自Facelets Essentials 我想使用CDI。

beans.xml什么問題? 有些地方說這是可選的,而其他地方則要求beans.xml 似乎是可選的:

23.13配置CDI應用程序

當使用范圍類型對bean進行注釋時,服務器會將應用程序識別為bean歸檔文件,並且不需要其他配置。 使用范圍中列出了CDI bean的可能范圍類型。 CDI使用名為bean.xml的可選部署描述符。 與其他Java EE部署描述符一樣,beans.xml中的配置設置也用於CDI類中的注釋設置。 如果存在沖突,beans.xml中的設置將覆蓋注釋設置。 僅在某些特定情況下,歸檔文件必須包含beans.xml部署描述符。

適用於Java EE平台的Java EE 7教程版本7 p 404

從我閱讀的內容來看,似乎推薦使用CDI而不是@ManagedBean 我沒有找到比這更簡單的示例了。

也可以看看:

https://stackoverflow.com/a/4397444/262852

https://stackoverflow.com/questions/26110888/what-is-the-alternative-to-managedbean

源代碼:

https://github.com/THUFIR/EntAppWeb

支持bean是正確的。 您需要檢查您的服務器是否支持CDI,或者需要使用一些額外的庫來使其工作(例如Apache Tomcat)。

我認為帶CDI的bean.xml是必需的,因為容器需要掃描所有帶有CDI注釋的bean。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
       bean-discovery-mode="annotated">
</beans>

這個bean-discovery-mode =“ annotated”是掃描您的類的內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM