【问题标题】:ViewScoped CDI bean recreated after some AJAX requests一些 AJAX 请求后重新创建 ViewScoped CDI bean
【发布时间】:2014-09-26 16:51:06
【问题描述】:

我有一个 CDI ViewScoped bean 和一个 JSF 页面。在几次 AJAX 请求之后,bean 以某种方式重新创建。 这是应用程序的完整代码:

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>

这是支持 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;
    }

}

【问题讨论】:

    标签: ajax jsf jsf-2.2 view-scope uirepeat


    【解决方案1】:

    好吧,我在尝试时遇到了同样的问题——Netbeans 8 和 JSF 2.2.0 也是如此。

    在我更新 Glassfish 后为我工作 - 从 here 获取 javax.faces.jar - 我采用了最新的 2.2.8-02,并替换了 glassfish/glassfish/modules 中的那个。

    不知道错误是什么或修复的确切时间。

    希望它也对你有用。

    【讨论】:

    • 感谢您的宝贵时间。
    猜你喜欢
    • 2015-01-14
    • 2016-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-13
    • 2014-01-07
    • 2015-04-15
    相关资源
    最近更新 更多