Skip to main content

@EJB in JSF-managed beans in GlassFish v2?

3 replies [Last post]
dxxvi
Offline
Joined: 2006-07-24
Points: 0

My local interface:

@javax.ejb.Local
public interface Calculator {
Integer add(Integer x, Integer y);
}

My EJB stateless session bean:

@javax.ejb.Stateless
public class CalculatorImpl implements Calculator {
public Integer add(Integer x, Integer y) {
return (x == null || y == null) ? null : x+ y;
}
}

My JSF-managed bean:

public class Index {
@EJB
private Calculator calculator;

private Integer x;
private Integer y;
private Integer result;

public void action() {
if (calculator == null) {
final String error = "You don't know how to use @EJB :)";
FacesMessage fm = new FacesMessage(FacesMessage.SEVERITY_FATAL, error, error);
FacesContext.getCurrentInstance().addMessage("form:result", fm);
result = null;
}
else {
result = calculator.add(x, y);
}
}
....

My web.xml has nothing related to ejb.

When I run the application, the calculator is null. Could anybody tell me what my problem is and how to fix it?

Thank you very much.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
dxxvi
Offline
Joined: 2006-07-24
Points: 0

I was about to upload my ear file and all Java source files to rapidshare so that somebody can drop my ear to their glassfish, then I realized that my jsf-managed bean was not in the war file but in the ejb jar file instead. And that is my problem.

Thank you all.

arveds
Offline
Joined: 2008-12-31
Points: 0

I don't see anything in your code snippets that isn't OK as far as this problem is concerned.

I assume that you know that 'calculator' is null because you're getting your message. What we haven't see so far is your faces-config.xml (as you say, you've got nothing about EJBs in your web.xml nor do you need it); perhaps you could post the relevant parts of that.

Arved

dxxvi
Offline
Joined: 2006-07-24
Points: 0

First, thank you for your reply on a Sunday.

My faces-config.xml (nothing related to ejb also)


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd"
version="1.2">

com.sun.facelets.FaceletViewHandler


index
home.bean.jsf.Index
request

My web.xml:


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">

org.richfaces.SKIN blueSky

org.richfaces.CONTROL_SKINNING disable

org.richfaces.CONTROL_SKINNING_CLASSES disable

org.richfaces.LoadStyleStrategy ALL

org.richfaces.LoadScriptStrategy ALL

facelets.DEVELOPMENT true

facelets.SKIP_COMMENTS true

javax.faces.DEFAULT_SUFFIX .xhtml


Faces Servlet
javax.faces.webapp.FacesServlet
1


Faces Servlet
*.jsf


Restrict raw XHTML Documents

XHTML
*.xhtml



My JSF page:



xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich">














I hope with everything I post here, you (or somebody) can tell me what's wrong with me.

Thanks