0

Here's the SCCE I've written. I'm completely new to java web application programming which means it's completely possible that I'm missing something. So any advice for a newbie moving forward is really appreciated. I've tried referencing the package name as well with no luck. Is there anything I should be referencing for the Session bean in web.xml? because these files are in the WAR file inside the deployed EAR file. I should note that I'm using java server faces, and EE7.

GlassFish4 error
/index.xhtml @10,78 binding="#{serverSessionBean.time}": Target Unreachable, identifier 'ServerSessionBean'

Index Welcome file

<?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);
    }    
}
Richard Barker
  • 1,161
  • 2
  • 12
  • 30

1 Answers1

0

The EJB's aren't visible for expression language (EL) expressions in JSF. You can use a JSF Manage Bean and inject the EJB or annotating the EJB with the @Named annotation makes the EJB immediately visible for expression language (EL) expressions in JSF. This @Named annotation takes the simple name of the annotated class, puts the first character in lowercase, and exposes it directly to the JSF pages. The EJB can be accessed directly, without any backed or managed beans, by the JSF pages.

Second, you are using two notations for specifying different types of session beans in the same ejb (@Singleton/@Stateless).

Eg.

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 file:

<?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>

Note the name is serverSessionBean with the first character in lowercase.

See also:

Federico Sierra
  • 5,118
  • 2
  • 23
  • 36
  • Adding the Named annotation failed, even after removing singleton / stateless annotations. I'm still getting a null reference. I edited my post to point out I'm using java EE 7 -- I'm not sure how different 6 and 7 are either -- though I gather it is different enough. – Richard Barker Nov 08 '14 at 19:37
  • @RichardBarker There are differences between Java EE 7 and 6, but in this case applied the answer, see my update maybe help you solve the problem. – Federico Sierra Nov 08 '14 at 20:15
  • I edited my code to match what I currently have. But it's still failing. – Richard Barker Nov 08 '14 at 21:50
  • @RichardBarker To enable cdi you must include the beans.xml file, this is included in the documentation that I recommended (https://docs.oracle.com/javaee/6/tutorial/doc/gjbnz.html). – Federico Sierra Nov 08 '14 at 22:30
  • @Ferderico I did that too. But still the same error is occurring. the session bean is resolving to null. The beans file is in the web-inf directory. I even checked an example I have and found two other files (sun-web.xml, faces.config) and added those. But Its still failing. – Richard Barker Nov 09 '14 at 17:29
  • @RichardBarker take a look in this example http://www.adam-bien.com/roller/abien/entry/simplest_possible_jsf_2_ejb, in this not access directly from the jsf page to ejb. I'm not have experience with glassfish, i don't know how glassfish handles the ejbs in the web container. – Federico Sierra Nov 09 '14 at 18:18
  • Thank you for that link! It helped me fix it. turns out it has to be a managed bean. – Richard Barker Nov 09 '14 at 22:03