本文介绍了JSF2 GAE和ViewScoped ManagedBean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法通过。现在我有一个奇怪的行为与ViewScoped ManagedBean:

  @ManagedBean @ViewScoped 
public class TestBean implements Serializable
{
私人字符串文本; // getter / setter
private List< String>文本; // 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页面:

 < h:body id =body> 
< f:view contentType =text / html>
< h:form id =frm>
< p:面板>
< h:panelGrid columns =2id =grid>
< p:inputText value =#{testBean.text}/>
actionListener =#{testBean.save}/>
< / h:panelGrid>
< / p:面板>
< p:outputPanel id =op>
< p:dataTable var =vvalue =#{testBean.texts}>
< h:outputText value =#{v}/>< / p:列>
< / p:dataTable>
< / p:outputPanel>
< / h:表格>
< / f:view>
< / h:body>

这适用于本地部署(使用GAE的Eclipse工具),但如果我部署到GAE,如果我点击添加 - 按钮,什么都不会发生。点击添加后,范围(在GAE)的附加测试显示以下内容:


  • @RequestScoped :输入文本不会消失,不会添加到dataTable中
  • @ViewScoped :输入文本不会消失,不会添加到dataTable中。
  • @SessionScoped :输入文本消失,dataTable始终有两个条目:Test和最后输入的文本


我从教程中获得了相同的设置

 <的context-param> //web.xml 
< param-name> javax.faces.STATE_SAVING_METHOD< / param-name>
< param-value>服务器< /参数值>
< / context-param>

//appengine-web.xml
<会话启用>真实< /会话启用>

更新1

以下是使用 @ManagedBean @ViewScoped 注释进行额外测试的结果:

在第一次请求或手动刷新页面),调用 @PostConstruct init()方法。如果我点击按钮什么都没有发生,那么对 test.jsf 的请求会记录在应用程序引擎日志中,但是我的 save()方法。 Firebug向我展示了一个POST请求到 test.jsf 和以下答案:

 <?xml version ='1.0'encoding ='UTF-8'?> 
<部分响应><错误>
< error-name>类javax.faces.application.ViewExpiredException< / error-name>
<错误讯息>
<![CDATA [viewId:/test.jsf - View /test.jsf无法恢复。]]>
< / error-message>
< / error>
< extension primefacesCallbackParam =validationFailed>
{validationFailed:false}
< / extension>
< / partial-response>

更新2

我已经使用mojarra-2.0.4,但现在更新到2.0.6。同样的问题,但一个新的观察:如果我清除所有的Firefox缓存, ViewExpiredException 没有出现,但我只能将1个元素添加到列表与LT;字符串> @PostConstruct 只会被调用一次,而不是每次点击按钮。

然后我尝试了myfaces-2.0 .7,但得到这个异常:

 从servlet未捕获的异常
java.lang.NoClassDefFoundError:无法初始化类
com.google.apphosting.runtime.security.shared.stub.javax.naming.InitialContext

我不确定是否应该尝试让我的表单工作,因为google在他们的教程中明确提到了mojarra(2.0.4)。


$ b

strong>





  • 通常我不会不回答我的问题,我只评价此答案作为解决方法而不是正确答案。事件如果我不喜欢客户状态保存,这似乎解决了奇怪的行为。但我必须详细检查:

     < context-param> 
    < param-name> javax.faces.STATE_SAVING_METHOD< / param-name>
    < param-value>客户端< /参数值>
    < / context-param>

    也许我们应该等到已解决。


    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();
      }
    }
    

    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>
    

    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: 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

    I have the same setting from the tutorial

    <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>
    

    Update 1

    Here are the results of additional tests with @ManagedBean @ViewScoped annotation:

    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>
    

    Update 2

    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.

    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
    

    I'm not sure if I should try to get myfaces working since google explicitly mentions mojarra (2.0.4) in their tutorial.

    References

    解决方案

    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>
    

    Perhaps we should wait until JAVASERVERFACES-1886 is resolved.

    这篇关于JSF2 GAE和ViewScoped ManagedBean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 23:13