0

这是我写的 SCCE。我对 Java Web 应用程序编程完全陌生,这意味着我完全有可能遗漏了一些东西。因此,对于新手前进的任何建议都非常感谢。我也尝试过引用包名称,但没有成功。我应该为 web.xml 中的 Session bean 引用什么吗?因为这些文件是在 WAR 文件里面部署的 EAR 文件。我应该注意,我使用的是 java server faces 和 EE7。

GlassFish4 错误
/index.xhtml @10,78 binding="#{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>

服务器会话Bean

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);
    }    
}
4

1 回答 1

0

EJB 对JSF中的表达式语言 (EL)表达式不可见。您可以使用JSF Manage Bean 并注入EJB或使用注解对EJB进行注解,从而使EJB对于JSF中的表达式语言 (EL)表达式立即可见。此注释采用被注释类的简单名称,将第一个字符设为小写,并将其直接公开给JSF页面。JSF页面可以直接访问 EJB,无需任何支持或托管的 bean 。@Named@Named

其次,您使用两种表示法在同一个 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的第一个字符是小写的。

也可以看看:

于 2014-11-08T19:21:13.497 回答