问题
I have a CDI ViewScoped bean and a JSF page. After a couple of AJAX requests the bean somehow recreated. Here is the full code of the application:
index.xhtml
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
Hello from Facelets
<h:form>
<h:inputText value="#{questionBean.newTag}"></h:inputText>
<h:commandButton value="Add Tag">
<f:ajax listener="#{questionBean.addTag()}" execute="@form" render="@form"></f:ajax>
</h:commandButton>
<ui:repeat value="#{questionBean.tagsOfQuestion}" var="tag">
<h:outputLabel value="#{tag}"></h:outputLabel>
<h:commandButton value="X">
<f:ajax event="click" listener="#{questionBean.removeTag(tag)}" render="@form" execute="@form"/>
</h:commandButton>
</ui:repeat>
</h:form>
</h:body>
</html>
Here is the backing CDI bean QuestionBean.java
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
@Named
@ViewScoped
public class QuestionBean {
private String newTag;
private List<String> tagsOfQuestion = new ArrayList<String>();
private Date dateCreated = new Date();
public QuestionBean(){
System.out.println("Date Created : "+dateCreated);
}
public void addTag(){
if(!newTag.isEmpty()){
getTagsOfQuestion().add(newTag);
}
newTag = "";
}
public void removeTag(String tagToRemove){
getTagsOfQuestion().remove(tagToRemove);
}
public String getNewTag() {
return newTag;
}
public void setNewTag(String newTag) {
this.newTag = newTag;
}
public List<String> getTagsOfQuestion() {
return tagsOfQuestion;
}
public void setTagsOfQuestion(List<String> tagsOfQuestion) {
this.tagsOfQuestion = tagsOfQuestion;
}
}
回答1:
Well I had the same problem when I tried it - Netbeans 8 and JSF 2.2.0 too.
Works for me after I updated Glassfish - grab javax.faces.jar from here - I took the very newest, 2.2.8-02, and replaced the one in glassfish/glassfish/modules.
Don't know what the error was or exactly when it was fixed though.
Hope it works for you too.
来源:https://stackoverflow.com/questions/26064509/viewscoped-cdi-bean-recreated-after-some-ajax-requests