Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?

like image 564
andolffer.joseph Avatar asked Dec 17 '12 12:12

andolffer.joseph


1 Answers

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.

like image 57
BalusC Avatar answered Nov 15 '22 10:11

BalusC