日期:2014-05-16  浏览次数:20455 次

JSF 2.0阅读笔记:视图状态 (四)
五. 实例验证

下面使用GlassFish v3来测试JSF2.0 RI的ViewState情况。

JSF2.0规范中提供了三个上下文参数来指定应用的ViewState策略,可在web.xml文件中使用context-param元素来配置。第一个是从JSF1.2就流传下来的javax.faces.STATE_SAVING_METHOD,取值为server或client,用于配置把视图状态数据放在服务器端还是传递到客户端,默认为server。第二个是javax.faces.PARTIAL_STATE_SAVING,取值为true或false,用于配置是否在应用范围内启用或禁用增量视图状态,默认为true。第三个是javax.faces.FULL_STATE_SAVING_VIEW_IDS,取值为一系列用逗号分隔的ViewId,用于配置当增量视图状态开启时,需要强制保存完全视图状态的页面,默认为空。

首先使用一个简单的页面测试:
<?xml version="1.0" encoding="UTF-8" ?>

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html">

    <h:body>
        <h:form>
            <h:outputLabel>name</h:outputLabel>
            <h:inputText/>
            <h:commandButton value="submit"/>
        </h:form>
    </h:body>
</html>  


修改web.xml为
    <context-param>
        <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
        <param-value>client</param-value>
    </context-param>

    <context-param>
        <param-name>javax.faces.PARTIAL_STATE_SAVING</param-name>
        <param-value>false</param-value>
    </context-param>


表示把视图状态传递到客户端,并且使用完全视图状态。访问该页面后,在浏览器中使用“查看源代码”功能来查看响应内容。可以发现其中包括这样的视图状态隐藏域:
<input type="hidden" name="javax.faces.ViewState" id="javax.faces.ViewState" value="H4sIAAAAAAAAAL1WTWwbRRSerJ3/pDRJVYSgURERpJBsYjl2nKa0cdKELHF+VLuBwMEdryfxhv1jdtZZ9xDRCxy4VGo5IAXBgQOHcuLGAQQckJCKBBIXTghxBC6A+LnAm1mvf4TdVXroSDua2Zn33rz3vm/e3PkFdboUjbyc2cdlLOvY3JM3C/tEZfM3v37x3ZPOOV1CyLMRQl0ORXHVMmTHNeVdrBJHxrataypmmmXKWYYZWccm3iNUMWx9LEcJ2bCK5Pf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op09uKMrwqAWXOtaMMXw+/Gf3rhk8fD9ISE9N4AwV89PA+1TC+UENVNzFVBjd4w+JFYeKS+JP+X/olP/28X4LpUODjcvtkxXabA5ioRSdyH5fPUGPURH1u9jwiPI+EeH5sRx+Dry/E0WSi2dFk/fSohX/R+7tw+Zvigbj8KHz9IS7PxJtdTjUev8O3zJqfaVCLtCKGi0ZehDq1HczqUveOWmt+DTeVJPFoeiAxkuEbCIPFtAdsb/PgEwWSv/TCCO/XcIiz4xYMjTVFfY7PDv8DUovXIu8LAAA=" autocomplete="off" />


其中视图状态信息是1636字节(1.6k)。现在把web.xml中的javax.faces.PARTIAL_STATE_SAVING上下文参数修改为true,重启应用后刷新页面。可发现视图状态隐藏域变为:
<input type="hidden" name="javax.faces.ViewState" id="javax.faces.ViewState" value="H4sIAAAAAAAAAJVSQWsTQRR+3SS1DVFqDL3ZUxEEnVAwUAxigmlocKuBrWDxoJPNtDtxdnecebvZ9FDwHwiehIpXD976C8SDICjo0f/gTe/ObGLTgx4cmLezb+a9933vfe9+QElqBRdHNKUkQS7INtXBDpWlc9/ff1h98rUAThfKIqbDLvUxVj1YxkAxHcRimMnbLbCrMl4ydsXsAsLi6DEf4o1EwaVHbp5X0OiA3B+MmI/NF18evlnRV4UDkEkT4CTP4AgK5lSSZs3/jhQQG52RfeozTfw4lHHEIiQPenf+nNf7KpZM4eQum2iYrapJreDCvPRWlIRnLyVCmSIqPkiQaW1LwozJYs7E7gWENVOT6CSaIbBWMNSk3e+7va2O6VvjHy94KAXpsH2aCOxOnettKcVkN37Kol9vr+0dt0atisUyXoNaPeVs7CFF1mFhTLIA