6

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 ?

andolffer.joseph
  • 613
  • 2
  • 10
  • 24
  • To avoid the obvious, is this action invoked by ajax or not? Thus, you're invoking the action by ajax, but not updating the page by ajax? – BalusC Dec 17 '12 at 13:21
  • Not. I´v try two ways: Normal and By Ajax. Normal doesnt work. By ajax/rerender the component view, works fine. I see on firebug that the HTML response is backing OK (by normal call) but the VIEW do not refresh/update as well. – andolffer.joseph Dec 17 '12 at 13:26
  • Show the code in flavor of an SSCCE. How to prepare that for JSF, see our JSF wiki page http://stackoverflow.com/tags/jsf/info – BalusC Dec 17 '12 at 13:31
  • @BalusC, I´ve edited my post with my code. Thank you. – andolffer.joseph Dec 17 '12 at 15:47
  • 1
    The `` sends by default an ajax request. You are not using `ajax="false"` anywhere in your example. Hence the code does not fit with the problem description at all. Or were you not aware about this at all? Anyway, I posted an answer based on the information posted so far. – BalusC Dec 17 '12 at 15:58

1 Answers1

11

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555