0

更新:

Any@ViewScoped@SessionScopedbean 的生命周期不是预期的。也就是说,它们的结束时间比应有的要早。例如,当 ajax 回发完成时,方法返回 void@ViewScoped@SessionScopedbean 不应该结束,但它们确实结束了。

在将它部署到我们的开发环境和其他人对其进行测试后,它似乎只发生在我的机器上并且在本地运行时发生。不知道为什么。tc Server 3.1 是正在使用的。

我会留下原件,以防有人遇到类似的问题。

原来的:

我有一棵由 ViewScoped 填充的树,它从 XML 文件中读取数据并为其创建 TreeNode 对象。性能不是超级快,因此在视图中创建一次是理想的。(下面省略了一些代码,但功能确实有效)

@ManagedBean
@ViewScoped
public class FundsXmlTreeBuilder implements Serializable {

    private TreeNode root;

    public FundsXmlTreeBuilder() throws SAXException, IOException, ParserConfigurationException {
        root = new DefaultTreeNode("Root", null);
        buildTree();
    }

    public void buildTree() throws SAXException, IOException, ParserConfigurationException {
        //reads xml file and adds TreeNodes
    }
}

还有另一个 RequestScoped bean,它有一个方法来处理在 onNodeSelect 方法中选择的树节点的 ajax 事件。它基本上决定了页面的另一部分将如何显示。

@ManagedBean
@RequestScoped
public class FundsXmlDisplay implements Serializable{

    private Fund fund;
    private FundFamily fundFamily;
    private FocusedPortfolioModel model;

    public TreeNode selectedRoot;

    public FundsXmlDisplay() {
        fund = null;
        fundFamily = null;
        model = null;

        selectedRoot = null;
    }

    public void onNodeSelect(NodeSelectEvent event) {
        Object data = event.getTreeNode().getData();

        fund = null;
        fundFamily = null;
        model = null;
        if (data instanceof Fund) {
            fund = (Fund) data;
        } else if (data instanceof FundFamily) {
            fundFamily = (FundFamily) data;
        } else if (data instanceof FocusedPortfolioModel) {
            model = (FocusedPortfolioModel) data;
            model.createPieModel();
        }
    }
}

以下是标记的主要部分。

<h:body>

    <h:form styleClass="form" id="form1">
        *** For Home Office Use Only
        <br />

        <p:layout id="xmlArea"
            style="min-width:400px;min-height:600px;width:95%;">
            <p:layoutUnit id="xmlTree" position="west" resizable="false"
                closable="false" scrollable="true" size="25%" minSize="40"
                maxSize="400" style="padding:.25em;" header="Funds XML Elements">
                    <p:tree value="#{fundsXmlTreeBuilder.root}" var="node"
                        dynamic="false" selectionMode="single">
                        <p:ajax event="select" update=":form1:xmlDisplay"
                            listener="#{fundsXmlDisplay.onNodeSelect}" />
                        <p:treeNode>
                            <h:outputText value="#{node}" style="font-size:small" />
                        </p:treeNode>
                    </p:tree>
            </p:layoutUnit>

            <p:layoutUnit position="center" header="Element Detail" size="75%">
                <p:scrollPanel id="xmlDisplay" mode="native"
                    style="height:100%;">
                    ...
                </p:scrollPanel>
            </p:layoutUnit>
        </p:layout>
    </h:form>

</h:body>

我遇到的问题是 FundsXmlTreeBuilder 在触发 ajax 事件之后但在侦听器之前重新创建。我希望它根本不会被重新创建,因为据我了解,视图没有改变。

xmlDisplay 区域中有不少呈现的属性。不确定这是否相关。我知道当操作方法返回 null 或 void 时视图存在,但我不认为正在使用的呈现方法算作“操作方法”。

同样有趣的是,如果我将 FundsXmlTreebuilder 更改为 SessionScoped,它也会在选择一个节点后重新创建,这确实让我感到难过。

希望一切都清楚,并提供足够的信息。谢谢!

4

1 回答 1

0

在 中web.xml<secure>标签需要被注释掉。所以相应的部分看起来像:

<session-config>
    <session-timeout>60</session-timeout>
    <cookie-config>
        <http-only>true</http-only>
<!--        <secure>true</secure> --> <-- Not be set locally -->
    </cookie-config>
    <tracking-mode>COOKIE</tracking-mode>
</session-config>

这个标签告诉浏览器只在 HTTPS 传输时发送 cookie,我不相信在本地运行时会出现这种情况。

这是我找到解释的地方。一位队友发现了这个问题。

于 2016-01-21T13:52:28.907 回答