JSF ViewScope-在操作上返回null不会更新视图 [英] JSF ViewScope - returning null on actions do not update the view

查看:124
本文介绍了JSF ViewScope-在操作上返回null不会更新视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ViewScope模式下有一个Managed Bean.因此,当我从此托管Bean调用某些操作时,我的页面不会更新.我看到我的动作被很好地调用并返回null(viewscope工作流程是可以的).

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?

如果我使用Ajax重新呈现页面,则效果很好.

If I rerender the page using Ajax it works fine.

我的版本是:

带有Primefaces 3.4.1的JSF 2.1.14

JSF 2.1.14 with Primefaces 3.4.1

我的代码:

@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;
   }

}

我的页面:

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

推荐答案

您正在使用<p:commandButton>提交表单.默认情况下,它发送一个ajax请求.默认情况下,它不进行任何更新.因此,完全可以预期您正在观察的行为.有几种方法可以解决此问题"(引用,因为这实际上不是问题,而只是概念上的误解):

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. 告诉它不使用ajax.

  1. Tell it to not use ajax.

<p:commandButton ... ajax="false" />

  • 告诉它更新表单.

  • Tell it to update the form.

    <p:commandButton ... update="@form" />
    

  • 由标准JSF组件替换,该组件默认情况下不使用ajax.

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

    这篇关于JSF ViewScope-在操作上返回null不会更新视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆