1

Two days ago I wrote this question: How can I retrieve an object on @WindowScoped? and BalusC answered with some suggestions, now I have some problem to understand if my problem is that the object in WindowScoped is stored properly or my code to retrieve it is wrong!

Well, as I said, I have an object that I stored in @WindowScoped annotation but I can retrive this object only the first time! Why?

I just have a doubt: the CODI extension of MyFaces could be configured in some manner? Or I can use it simple adding the jar files to my project?

However, these are parts of my code because I don't know where is the problem:

LogicBean.java (the object that I should retrive):

@ManagedBean (name="logicBean" )
@WindowScoped
public class LogicBean implements Serializable 
{
    String pageIncluded;
    // getter and setter methods

    public String action(String value)
    {
        setPageIncluded(value);

        return "include";
    }
}

include.xhtml:

<ui:include src="#{logicBean.pageIncluded}"/> 

ProgettiController.java

@ManagedBean(name = "progettiController")
@SessionScoped
public class ProgettiController implements Serializable {

    private FacesContext context = FacesContext.getCurrentInstance();
    private LogicBean logicBean = context.getApplication().evaluateExpressionGet(context, "#{logicBean}", LogicBean.class);
    //getter and setter methods

    public void testMethod()
    {
        logicBean.action("WEB-INF/jsf/page1.xhtml");
    }
}

I tried also using @ManagedProperty("#{logicBean}") and setting the scope as WindowScoped but nothing change...


EDIT: after some new trials I found a strange problem, on my include.xhtml I added #{progettiController.logicBean.getPageIncluded()} and #{logicBean.getPageIncluded()} for check these two fields o?

Well, when I load the application for the first time the variables are correctly set and I see what I want, the second time the first variable is setted with the new value but the second is empty and I don't see anything, but now is coming the strange thing... if I should try again the app I should open index.xhtml where I had some forms like this:

<h:form>
    <h:commandLink action="#{logicBean.action('/WEB-INF/jsf/progetti/List.xhtml')}" value="Show All Progetti Items"/>
</h:form>

and which is the result? The first variable remains set with the old value (wrong) but the second is setted correctly so I can review the page like I would! If someone can help me I will thank him/her forever!

Community
  • 1
  • 1
Filippo1980
  • 2,745
  • 5
  • 30
  • 44

1 Answers1

1

CODI is an extension to CDI, so you should manage your beans by CDI @Named annotation instead of the JSF @ManagedBean annotation. Then you can inject the other bean by CDI @Inject annotation. The following example should work:

import javax.inject.Named;
import org.apache.myfaces.extensions.cdi.core.api.scope.conversation.WindowScoped;

@Named
@WindowScoped
public class LogicBean implements Serializable {
    // ...
}

and

import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;

@Named
@SessionScoped
public class ProgettiController implements Serializable {

    @Inject
    private LogicBean logicBean;

    // ...
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • @ BalusC : the problem is that I'm using LogicBean in many methods so I tried to add only:
    context = FacesContext.getCurrentInstance();
    logicBean = context.getApplication().evaluateExpressionGet(context, "#{logicBean}", LogicBean.class);
    in the methods that use it but nothing change... however the problem is also that the xhtml page didn't retreive the logicBean!
    – Filippo1980 Jan 20 '12 at 13:06
  • You are right. I completely overlooked that CODI is an extension to CDI. You should manage your beans by CDI annotations instead of JSF annotations. Then you can use CDI `@Inject` to inject the other bean. This is regardless of the scoping difference. I have edited my answer. – BalusC Jan 20 '12 at 13:18
  • @ BalusC : ok, you are right, obviously... now it works... the error in controller caused also the problem in xhtml page... thanks again! – Filippo1980 Jan 20 '12 at 13:48
  • emh... I didn't read your last comment... the "problem" is that it worked properly after that I correct the controller class... I will do others trial but I think that if it works I will not use @inject :) – Filippo1980 Jan 20 '12 at 13:53
  • You can indeed keep using `Application#evaluateExpressionGet()`, but using `@Inject` is cleaner and simpler if you're using CDI beans. When using JSF beans, `@ManagedProperty` would not be possible because it doesn't allow injecting an instance in a narrower scope. Since CDI works with proxies, it works with `@Inject`. – BalusC Jan 20 '12 at 13:56
  • Ok, after some tests I saw that I could not open another window because the new page didn't load a new LogicBean and it can't set its parameter.. however I change the code which what you write and now when I launch the application the server response: – Filippo1980 Jan 20 '12 at 14:50
  • WELD-001303 No active contexts for scope type org.apache.myfaces.extensions.cdi.core.api.scope.conversation.WindowScoped - Stack Trace org.jboss.weld.context.ContextNotActiveException: WELD-001303 No active contexts for scope type org.apache.myfaces.extensions.cdi.core.api.scope.conversation.WindowScoped – Filippo1980 Jan 20 '12 at 14:51
  • I want underline that the error speak about jBoss but I'm using Glassfish! – Filippo1980 Jan 20 '12 at 14:53
  • @ BalusC : P.S.: which is the difference between javax.enterprise.SessionScoped and javax.faces.bean.SessionScoped? – Filippo1980 Jan 20 '12 at 14:57
  • This seems related to this issue report: https://issues.apache.org/jira/browse/EXTCDI-176 It would be a Mojarra bug. Perhaps you're using Mojarra? Glassfish indeed ships by default with Mojarra. Give MyFaces a try. Download and drop [MyFaces bundle JAR](http://myfaces.apache.org/download.html) in `/WEB-INF/lib` and add `` to `glassfish-web.xml`. As to the appearance of "org.jboss", that's just Weld which is the CDI implementation. It's from JBoss company. It must not be confused with JBoss Application Server which is indeed from same company. – BalusC Jan 20 '12 at 15:02
  • The difference between those two annotations is that `javax.enterprise.context.SessionScoped` is from CDI and should be used together with `@Named` and that `javax.faces.bean.SessionScoped` is from JSF and should be used together with `@ManagedBean`. There are in Java EE a lot of ways of manage beans by annotations. JSF has its own set of annotations and CDI has its own set of annotations. They cannot be used together, it's the one or the other. – BalusC Jan 20 '12 at 15:03
  • @ BalusC : No thanks, I tried the chat, for few minutes, and it didn't seem very friendly for me, however thank you for all your help! – Filippo1980 Jan 20 '12 at 15:18
  • CODI autom. replaces all JSF annotations with the corresponding CDI annotations during bootstrapping. We used that during the migration to CDI/CODI. You only have to change the injection to std. CDI injection but not the Scope-Annotations and also not @ManagedBean. – Dar Whi Feb 10 '12 at 01:12
  • Who was not friendly and in which chat? – Dar Whi Feb 10 '12 at 01:14
  • @DarWhi: He means the [Stack Overflow chat](http://chat.stackoverflow.com/) in general, that it was not friendly for the blind like Flippo1980. He didn't mean any person in particular. – BalusC Feb 10 '12 at 01:41