问题
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!
回答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;
// ...
}
来源:https://stackoverflow.com/questions/8939037/how-can-i-check-if-an-object-stored-with-windowscoped-is-stored-correctly