gpt4 book ai didi

jsf-2 - JSF ViewScope - 在操作上返回 null 不会更新 View

转载 作者:行者123 更新时间:2023-12-04 17:37:31 25 4
gpt4 key购买 nike

我有一个处于 ViewScope 模式的托管 Bean。因此,当我从这个托管 Bean 调用某些操作时,我的页面不会更新。我看到我的操作被很好地调用并返回 null(viewscope 工作流程正常)。

那么,我做错了什么?

如果我使用 Ajax 重新渲染页面,它工作正常。

编辑:

我的版本是:

JSF 2.1.14 与 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>

在我的屏幕上,状态变量不会改变。而且,是的.. 该操作称为 OK。一些提示?

最佳答案

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

  • 告诉它不要使用ajax。
    <p:commandButton ... ajax="false" />
  • 告诉它更新表单。
    <p:commandButton ... update="@form" />
  • 替换为标准 JSF 组件,默认情况下不使用 ajax。
    <h:commandButton ... />

  • 请注意,这个具体问题与 View 范围本身无关。使用其他范围(包括请求范围)时,您会遇到完全相同的问题(使用完全相同的解决方案)。

    关于jsf-2 - JSF ViewScope - 在操作上返回 null 不会更新 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13914594/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com