单击commandLink后ViewScoped bean失去其状态

时间:2013-07-25 06:10:49

标签: jsf-2 facelets icefaces icefaces-3

我正在使用Icefaces 3.2和JSF 2.1.8。我在主xhtml页面中包含以下视图。在点击链接myViewBean后,@ViewScoped看起来myLink正在丢失其状态。 actionListener具有获取myViewBean的逻辑并设置所需的项目列表。在呈现响应阶段,myViewBean似乎为空empList。如果@ViewScoped更改为@SessionScoped,它会以预期的方式工作,但我有10个其他bean,所有这些都需要更改为SessionScoped。是什么让豆子失去了它的状态?或者有没有其他方法可以使用ViewScoped?

main.xhtml

<!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:f="http://java.sun.com/jsf/core"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ice="http://www.icesoft.com/icefaces/component"
xmlns:ui="http://java.sun.com/jsf/facelets" >
<f:view contentType="text/html">
        <h:head>
        </h:head>
        <h:body id="bodyId" bgcolor="white">
            <table border="0" cellpadding="0" cellspacing="0">
                <tr>
                    <td>
                    <ice:panelGroup>
                        <ice:form id="mainForm" name="mainForm">
                            <table>
                                <tbody>
                                    <tr>
                                        <td>
                                            <ui:include src="myTabs.xhtml"/>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </ice:form>
                    </ice:panelGroup>
                    </td>
                </tr>
            </table>
        </h:body>
</f:view>
</html>

myTabs.xhtml

<f:subview 
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ice="http://www.icesoft.com/icefaces/component"
xmlns:ui="http://java.sun.com/jsf/facelets">
<div>
    <table class="icePnlTbSet" id="myTabSet"
        border="0" cellpadding="0" cellspacing="0">
        <tbody>
            <tr>
                <td>
                <table cellpadding="0" cellspacing="0">
                    <tbody>
                        <tr>
                            <!-- My Tab 1 -->
                            <td class="icePnlTb" style="vertical-align: bottom;" title="tab1">
                            <table cellpadding="0" cellspacing="0">
                                <tbody>
                                    <tr>
                                        <td class="LftTop"><img alt="" src="/images/spacer.gif "
                                            width="4" height="1" /></td>
                                        <td class="MidTop"><img alt="" src="/images/spacer.gif "
                                            width="4" height="1" /></td>
                                        <td class="RtTop"><img alt="" src="/images/spacer.gif "
                                            width="4" height="1" /></td>
                                    </tr>
                                    <tr>
                                        <td class="LftMid"><img alt="" src="/images/spacer.gif "
                                            width="4" height="1" /></td>
                                        <td class="MidMid">
                                            <ice:commandLink id="myLink" value="MyTab 1" title=""
                                                actionListener="#{mySessionBean.tabChanged}">
                                                <f:param value="0" name="tab" />
                                            </ice:commandLink>
                                        </td>
                                        <td class="RtMid"><img alt="" src="/images/spacer.gif "
                                            width="4" height="1" /></td>
                                    </tr>
                                    <tr>
                                        <td class="LftBtm"><img alt="" src="/images/spacer.gif "
                                            width="4" height="1" /></td>
                                        <td class="MidBtm"><img alt="" src="/images/spacer.gif "
                                            width="4" height="1" /></td>
                                        <td class="RtBtm"><img alt="" src="/images/spacer.gif "
                                            width="4" height="1" /></td>
                                    </tr>
                                </tbody>
                            </table>
                            </td>
                            <!-- Repeat for n tabs -->
                            </tr>
                        </tbody>
                    </table>
                </td>
            </tr>
        </tbody>
    </table>
</div>
<ui:include src="tabcontent1.xhtml" />
 </f:subview>

tabcontent1.xhtml

<f:subview 
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ice="http://www.icesoft.com/icefaces/component"
xmlns:ui="http://java.sun.com/jsf/facelets">
<ice:panelGroup>
    <ice:dataTable  id="empTable" value="#{myViewBean.empList}"
            var="emp">
            ...
    </ice:dataTable>
</ice:panelGroup>
</f:subview>

1 个答案:

答案 0 :(得分:0)

如果从操作方法返回null或void(将导航回同一页面),@ ViewScoped bean将只保存数据,一旦离开视图,数据就会丢失。

可能你可以试试@RequestScoped。