Java EE应用程序以秒为单位显示当前服务器时间

时间:2014-11-08 18:20:28

标签: java-ee session-bean

这是我写过的SCCE。我对java web应用程序编程完全陌生,这意味着我完全有可能错过一些东西。因此,对新手前进的任何建议都非常感谢。我也尝试过引用包名,但没有运气。我应该在web.xml中引用Session bean吗?因为这些文件位于已部署的EAR文件内的WAR文件中。我应该注意到我使用的是java服务器面和EE7。

GlassFish4错误
/index.xhtml @ 10,78 binding ="#{serverSessionBean.time}":Target Unreachable,identifier' 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);
    }    
}

1 个答案:

答案 0 :(得分:0)

对于 JSF 表达式语言(EL)表达式,EJB是不可见的。 您可以使用 JSF 管理Bean并注入 EJB 或使用@Named注释注释 EJB 来生成 EJB JSF 表达式语言(EL)表达式立即可见。 此@Named注释采用带注释的类的简单名称,将第一个字符置为小写,并将其直接公开给 JSF 页面。可以通过 JSF 页面直接访问EJB,无需任何支持或托管的bean。

其次,您使用两种表示法在同一ejb(@Singleton / @Stateless)中指定不同类型的会话bean。

<强>例如

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,第一个字符为小写。

另见: