使用AJAX渲染其父元素之一时,ViewScoped bean会重建

时间:2014-09-11 21:09:28

标签: ajax jsf

我有一个ViewScoped CDI bean(javax.faces.view.ViewScoped)。当ajax呈现其父元素时,它会重构(调用@PostConstruct方法)。我该怎么办?

豆:

import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

@Named("test")
@ViewScoped
public class TestBean implements Serializable {
    private List<Integer> items;

    @PostConstruct
    private void postConstruct() {
        Logger.getLogger("asd").log(Level.INFO, "construct");
        items = new ArrayList<Integer>();
        items.add(0);
        items.add(1);
        items.add(3);
        items.add(4);
        items.add(7);
        items.add(10);
    }

    public List<Integer> getItems() {
        return items;
    }

    public void setItems(List<Integer> items) {
        this.items = items;
    }
}

查看:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
        PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html" xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:pt="http://xmlns.jcp.org/jsf/passthrough">

<h:head>
    <title>Simple JSF Facelets page</title>
</h:head>

<h:body>
    <h:form>
        <h:commandButton value="No reconstructing">
            <f:ajax render=":panel" />
        </h:commandButton>
    </h:form>
    <h:panelGrid id="panel">
        <h:panelGroup>
            <h:form>
                <h:commandButton value="Strange reconstructing occurs">
                    <f:ajax render=":panel" />
                </h:commandButton>
            </h:form>
            <ui:repeat value="#{test.items}" var="i">
                #{i}
            </ui:repeat>
        </h:panelGroup>
    </h:panelGrid>
</h:body>

</html>

首先点击第一个按钮。

0 个答案:

没有答案