一些AJAX请求后ViewScoped CDI豆重建AJAX、ViewScoped、CDI

2023-09-10 14:05:24 作者:北海旧梦碎人心

我有一个CDI ViewScoped豆和JSF页面。经过几次的AJAX请求豆莫名其妙地重新创建。 下面是应用程序的完整code:

的index.xhtml

< HTML的xmlns =htt​​p://www.w3.org/1999/xhtml       的xmlns:H =htt​​p://xmlns.jcp.org/jsf/html       的xmlns:F =htt​​p://java.sun.com/jsf/core       的xmlns:UI =htt​​p://xmlns.jcp.org/jsf/facelets>     < H:头>         <冠军>的facelet标题< /标题>     < / H:头>     < H:身体GT;         你好,从Facelets的         < H:形式GT;             < H:inputText的值=#{questionBean.newTag}>< /小时:inputText的>             < H:的commandButton值=添加标签>                 < F:AJAX监听=#{questionBean.addTag()}执行=@形式呈现=@形与GT;< / F:AJAX>             < / H:的commandButton>             < UI:重复值=#{questionBean.tagsOfQuestion}VAR =标签>                 < H:outputLabel值=#{}标签>< / H:outputLabel>                 < H:的commandButton值=X>                     < F:Ajax事件=点击监听器=#{questionBean.removeTag(标签)}渲染=@表执行=@形式/>                 < / H:的commandButton>             < / UI:重复>         < /小时:形式GT;     < / H:身体GT; < / HTML>

下面是后盾CDI豆 QuestionBean.java

 进口的java.util.ArrayList;
    进口java.util.Date;
    进口的java.util.List;
    进口javax.faces.view.ViewScoped;
    进口javax.inject.Named;

    @Named
    @ViewScoped
    公共类QuestionBean {
        私人字符串newTag;
        私人列表<字符串> tagsOfQuestion =新的ArrayList<字符串>();
        私人日期dateCreated会=新的日期();

    公共QuestionBean(){
        的System.out.println(创建日期:+ dateCreated会);
    }

    公共无效addTag(){
        如果(!newTag.isEmpty()){
            getTagsOfQuestion()加(newTag)。
        }
        newTag =;
    }

    公共无效removeTag(字符串tagToRemove){
        。getTagsOfQuestion()删除(tagToRemove);
    }

    公共字符串getNewTag(){
        返回newTag;
    }

    公共无效setNewTag(字符串newTag){
        this.newTag = newTag;
    }

    公开名单<字符串> getTagsOfQuestion(){
        返回tagsOfQuestion;
    }

    公共无效setTagsOfQuestion(名单<字符串> tagsOfQuestion){
        this.tagsOfQuestion = tagsOfQuestion;
    }

}
 

解决方案

嗯,我有同样的问题,当我尝试了 - Netbeans的8和JSF 2.2.0了。

我的作品后,我更新的Glassfish的 - 抢javax.faces.jar从这里 - 我花了很最新的,2.2.8-02,换成一个在GlassFish中/ GlassFish中/模块

不知道是什么错误或者什么时候它虽然固定的。

希望它能为你工作了。

Spring MVC请求处理流程分析

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;
    }

}

解决方案

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.