带有 GAE 和 ViewScoped ManagedBean 的 JSF2 [英] JSF2 with GAE and ViewScoped ManagedBean
问题描述
我设法在 Google AppEngine 中使用 JSF2 获得了一个原型,遵循此教程.现在我对 ViewScoped ManagedBean 有一些奇怪的行为:
I managed to get a prototype with JSF2 get working at Googles AppEngine following this tutorial. Now I have some odd behavior with a ViewScoped ManagedBean:
@ManagedBean @ViewScoped
public class TestBean implements Serializable
{
private String text; //getter/setter
private List<String> texts; //getter
@PostConstruct public void init()
{
texts = new ArrayList<String>();
texts.add("Test");
text = new String();
}
public void save(ActionEvent ae)
{
texts.add(text);
text = new String();
}
}
这是我的 .xhtml 页面:
This is my .xhtml page:
<h:body id="body">
<f:view contentType="text/html">
<h:form id="frm">
<p:panel>
<h:panelGrid columns="2" id="grid">
<p:inputText value="#{testBean.text}"/>
<p:commandButton value="Add" update=":frm:op @parent"
actionListener="#{testBean.save}" />
</h:panelGrid>
</p:panel>
<p:outputPanel id="op">
<p:dataTable var="v" value="#{testBean.texts}">
<p:column><h:outputText value="#{v}" /></p:column>
</p:dataTable>
</p:outputPanel>
</h:form>
</f:view>
</h:body>
这适用于本地部署(使用适用于 GAE 的 Eclipse 工具),但如果我将其部署到 GAE,则单击添加按钮不会发生任何事情.单击添加后,具有范围的其他测试(在 GAE)显示以下内容:
This works fine with a local deployment (Using the Eclipse tools for GAE), but if I deploy this to GAE, nothing happens if I click on the Add-Button. Additional Tests with the scope (at GAE) show the following after clicking on Add:
@RequestScoped
:输入的文本没有消失,没有添加到dataTable@ViewScoped
:输入的文本没有消失,没有添加到dataTable@SessionScoped
:输入的文本消失,dataTable总是有两个条目:测试"和最后输入的文本
@RequestScoped
: Entered Text does not disappear, not added to dataTable@ViewScoped
: Entered Text does not disappear, not added to dataTable@SessionScoped
: Entered Text disappear, dataTable always has two entries: "Test" and the last entered Text
我有与教程相同的设置
<context-param> //web.xml
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>server</param-value>
</context-param>
//appengine-web.xml
<sessions-enabled>true</sessions-enabled>
更新 1
以下是带有 @ManagedBean @ViewScoped
注释的附加测试的结果:
Here are the results of additional tests with @ManagedBean @ViewScoped
annotation:
在第一次请求(或手动刷新页面)期间,@PostConstruct init()
方法被调用.如果我点击按钮 nothing happes,对 test.jsf
的请求会记录在应用引擎日志中,但我的 save()<中没有日志/代码>方法.Firebug 向我展示了对
test.jsf
的 POST 请求和以下答案:
During the first request (or manual refreh of the page), the @PostConstruct init()
method is called. If I click on the button nothing happes, the request to test.jsf
is logged in app engines log, but there is no log in my save()
method. Firebug shows me a POST request to test.jsf
and the following answer:
<?xml version='1.0' encoding='UTF-8'?>
<partial-response><error>
<error-name>class javax.faces.application.ViewExpiredException</error-name>
<error-message>
<![CDATA[viewId:/test.jsf - View /test.jsf could not be restored.]]>
</error-message>
</error>
<extension primefacesCallbackParam="validationFailed">
{"validationFailed":false}
</extension>
</partial-response>
更新 2
我使用过 mojarra-2.0.4 但现在更新到 2.0.6.同样的问题,但有一个新的观察:如果我清除所有 Firefox 缓存,ViewExpiredException
不会出现,但我只能向 List
@PostConstruct
只调用一次,而不是每次点击按钮时调用.
I have used mojarra-2.0.4 but now updated to 2.0.6. The same issue, but a new observation: If I clear all Firefox caches, the ViewExpiredException
did not appear, but I'm only able to add 1 Element to the List<String>
. The @PostConstruct
is only invoked once and not for each click on the button.
然后我尝试了 myfaces-2.0.7,但得到了这个异常:
Then I tried myfaces-2.0.7, but got this exception:
Uncaught exception from servlet
java.lang.NoClassDefFoundError: Could not initialize class
com.google.apphosting.runtime.security.shared.stub.javax.naming.InitialContext
我不确定我是否应该尝试让 myfaces 工作,因为谷歌在他们的教程中明确提到了 mojarra (2.0.4).
I'm not sure if I should try to get myfaces working since google explicitly mentions mojarra (2.0.4) in their tutorial.
参考资料
推荐答案
通常我不回答我的问题,我只将此答案评为解决方法,而不是正确答案.如果我不喜欢保存客户端状态,这似乎解决了奇怪的行为.但我必须详细检查一下:
Normally I don't answer my questions and I only rate this answer as a workaround and not as the correct answer. Event if I don't like client state saving, this seems to fix the odd behavior. But I have to check this in detail:
<context-param>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
也许我们应该等到 JAVASERVERFACES-1886 解决.
Perhaps we should wait until JAVASERVERFACES-1886 is resolved.
这篇关于带有 GAE 和 ViewScoped ManagedBean 的 JSF2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!