JSF ViewScope - returning null on actions do not update the view

烈酒焚心 提交于 2019-12-12 10:56:36

问题


I have a Managed Bean in ViewScope mode. So, when I call some action from this Managed Bean my page do not update. I see that my action is invoked well and returning null (viewscope work flow is OK).

So, what am I doing wrong?

If I rerender the page using Ajax it works fine.

EDIT:

My Versions are:

JSF 2.1.14 with Primefaces 3.4.1

My Code:

@ManagedBean(name = "test")
@ViewScoped
public class TestMB implements Serializable {
   private String status;

   public String getStatus() { return this.status; }
   public void setStatus(String status) { this.status = status; } 

   public String changeStatus() {
      this.status = "ViewScope Works!";
      return null;
   }

}

My page:

<!DOCTYPE HTML>
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:p="http://primefaces.org/ui"
                template="/template/ui.xhtml">
    <ui:define name="head">
    </ui:define>
    <ui:define id="teste" name="content">
        <h:form id="form">  
            <h:outputText id="status" value="OK?: #{test.status}" />
            <p:commandButton id="myAction" value="Do it!" action="#{test.changeStatus}"  />
        </h:form>  
    </ui:define>
</ui:composition>

On my screen, the status variable dont change. And, yes.. the action is called OK. Some tip ?


回答1:


You were using <p:commandButton> to submit the form. It sends by default an ajax request. It updates by default nothing. The behaviour you're observing is thus fully expected. There are several ways to solve this "problem" (quoted, as it's actually not a problem, but just a conceptual misunderstanding):

  1. Tell it to not use ajax.

    <p:commandButton ... ajax="false" />
    
  2. Tell it to update the form.

    <p:commandButton ... update="@form" />
    
  3. Replace by standard JSF component, which doesn't use ajax by default.

    <h:commandButton ... />
    

Note that this concrete problem is unrelated to the view scope itself. You'd have exactly the same problem (with exactly the same solutions) when using another scope, including the request scope.



来源:https://stackoverflow.com/questions/13914594/jsf-viewscope-returning-null-on-actions-do-not-update-the-view

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!