2014-11-08 63 views
0

這是我寫的SCCE。我完全不熟悉java web應用程序編程,這意味着我完全有可能缺少某些東西。所以,對於新手前進的任何建議都非常感謝。我已經嘗試引用包名稱,但沒有運氣。有什麼我應該引用的web.xml中的會話bean?因爲這些文件位於已部署EAR文件內的WAR文件中。我應該注意到我正在使用java服務器端和EE7。Java EE應用程序以秒爲單位顯示當前服務器時間

GlassFish4錯誤
/index.xhtml @ 10,78結合= 「#{serverSessionBean.time}」:目標不可達,識別符 'ServerSessionBean'

指數歡迎文件

<?xml version='1.0' encoding='UTF-8' ?> 
<!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:h="http://xmlns.jcp.org/jsf/html"> 
    <h:head> 
     <title></title> 
    </h:head> 
    <h:body> 
     The current server time is: 
     <h:outputText binding="#{serverSessionBean.time}"/> 
    </h:body> 
</html> 

ServerSessionBean

package com.incredibleifs; 

import java.util.Calendar; 
import javax.ejb.Stateless; 
import javax.inject.Named; 

@Stateless(description = "A stateless session bean to hold server generic business methods such as getting the date and time") 
@Named() 
public class ServerSessionBean implements ServerSessionBeanLocal { 
    @Override 
    public int getTime() { 
     return Calendar.getInstance().get(Calendar.SECOND); 
    }  
} 

回答

0

對於表達式語言(EL)表達式JSF,EJB不可見。 您可以使用JSF管理豆和注入EJB或與@Named註解該EJB使得EJB立即表達式語言(EL)表達式JSF可見。 此@Named註釋採用帶註釋的類的簡單名稱,將第一個字符設置爲小寫,並將其直接公開給頁面上的JSF頁面。可以通過頁面直接訪問EJB,而不需要任何支持或受管理的Bean。

其次,您在同一ejb中使用了兩種符號來指定不同類型的會話bean(@Singleton/@Stateless)。

例如,

EJB:

package com.incredibleifs; 

import javax.ejb.Stateless; 
import java.util.Calendar; 
import javax.ejb.Singleton; 

@Stateless 
@Named("serverSessionBean") 
public class ServerSessionBean implements ServerSessionBeanLocal { 
    @Override 
    public int getTime() { 
     return Calendar.getInstance().get(Calendar.SECOND); 
    }  
} 

XHTML文件:

<?xml version='1.0' encoding='UTF-8' ?> 
<!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:h="http://xmlns.jcp.org/jsf/html"> 
    <h:head> 
     <title></title> 
    </h:head> 
    <h:body> 
     The current server time is: 
     <h:outputText binding="#{serverSessionBean.time}"/> 
    </h:body> 
</html> 

注意名稱是serverSessionBean與小寫的第一個字符。

參見:

+0

添加註釋命名失敗,甚至消除單/無國籍註釋之後。我仍然收到空引用。我編輯了我的帖子,指出我正在使用java EE 7 - 我不確定6和7是多麼不同 - 儘管我收集的內容已經不一樣了。 – 2014-11-08 19:37:18

+0

@RichardBarker Java EE 7和6之間存在差異,但在這種情況下應用了答案,請參閱我的更新可能會幫助您解決問題。 – 2014-11-08 20:15:44

+0

我編輯了我的代碼以匹配我目前擁有的代碼。但它仍然失敗。 – 2014-11-08 21:50:13

相關問題