我有一个 列表页面 这将转到 添加页面. 。添加页面有一个 名称文本框 其值绑定到 会话范围 bean.

列表页面有一个 添加按钮 它通过操作方法到达添加页面。此操作方法清除名称文本框绑定到的对象。

我也有一个 取消按钮 在添加页面上,它绑定到一个操作方法,该方法再次清除名称文本框绑定到的值。

如果没有立即设置任何内容,则一切正常。

但是,如果我将取消按钮设置为立即,并且在名称字段中输入值,然后单击取消,则会触发操作方法并清除支持 bean 中的对象并转到列表页面。如果我然后单击添加,操作方法会再次清除对象(忽略它是否是最佳方法),然后转到添加页面。我现在希望添加页面的名称文本框为空,但事实并非如此?!当然,由于添加按钮不是立即的,因此值应该重新绑定并为空?

下面是列表页面上添加按钮的相关 XHTML

<h:commandButton id="addButton"
                 value="Add"
                 action="#{myBean.gotoAdd}"/>

下面是添加页面上输入框的相关 XHTML(myBean 是会话范围的),后面是添加页面上取消按钮的 XHTML:

<h:inputText id="newName"
             value="#{myBean.newObject.name}"
             binding="#{myBean.newNameInput}"
             styleClass="name" />

<h:commandButton id="cancelButton"
                 value="Cancel" immediate="true"
                 action="#{myBean.cancelAdd}"
                 onclick="return confirm('You sure?');"/>
有帮助吗?

解决方案

我几乎从来不使用的标签binding财产,除了我需要确定名单的项目已经有一个动作就可以开枪,所以我不是特别充分了解它的用途。但我知道,如果没有使用binding您的代码会如你所料,所以我的期望是要绑定到未得到正确复位,无论javax.faces.component.UIxxx对象最有可能的工作。

其他提示

我有现在非常类似的问题。

除了去除所述结合和/或立即的属性,尝试调用有关组件setSubmittedValue()与来自“添加”按钮呼吁点击动作结合。

唉,即使它可以帮助你,你仍然必须这样做,在不采取任何可能导致取消后显示相同的组件。

这就是为什么我仍然在试图找出一些更好的解决方案...

如果您使用即时=“真”,则该值将被保留,这是怎样的参数工作。你应该看看下面的链接:

http://wiki.apache.org/myfaces/How_The_Immediate_Attribute_Works

http://wiki.apache.org/myfaces/ClearInputComponents

好的,这是我从头开始做的一个例子。我有两个取消按钮,一个是立即取消按钮,一个不是。重现步骤示例:

  • 转到 james-list 页面并单击“添加”
  • 添加页面显示为空字段。输入所有字段的值,然后单击“添加”。
  • 列表页面将显示并更新以包含新人员。单击添加。
  • 添加页面显示为空字段。输入所有字段的值并单击取消(立即)
  • 列表页面显示且未更改。单击添加。
  • 添加页面显示,但字段并不像我预期的那样为空。单击“取消”。
  • 列表页面显示且未更改。单击添加。
  • 将显示添加页面,现在字段不为空。

詹姆斯.java:

package com.jamiebarrow;

import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;

@ManagedBean
@SessionScoped
public class James {

  private UIComponent idComponent;
  private UIComponent firstNameComponent;
  private UIComponent lastNameComponent;

  public UIComponent getIdComponent() {
    return idComponent;
  }

  public void setIdComponent(UIComponent idComponent) {
    this.idComponent = idComponent;
  }

  public UIComponent getFirstNameComponent() {
    return firstNameComponent;
  }

  public void setFirstNameComponent(UIComponent firstNameComponent) {
    this.firstNameComponent = firstNameComponent;
  }

  public UIComponent getLastNameComponent() {
    return lastNameComponent;
  }

  public void setLastNameComponent(UIComponent lastNameComponent) {
    this.lastNameComponent = lastNameComponent;
  }

  private List<Person> personResults;

  private Person person;

  public James() {
    personResults = new ArrayList();
    personResults.add(new PersonBuilder(1, "Bob", "Uncle").build());
    personResults.add(new PersonBuilder(2, "Jack", "Black").build());
  }

  public List<Person> getPersonResults() {
    return personResults;
  }

  public void setPersonResults(List<Person> personResults) {
    this.personResults = personResults;
  }

  public Person getPerson() {
    return person;
  }

  public void setPerson(Person person) {
    this.person = person;
  }

  private void clearPerson() {
    person = new PersonBuilder().build();
  }

  public String gotoList() {
    return "james-list";
  }

  public String gotoAdd() {
    clearPerson();
    return "james-add";
  }

  public String cancelAdd() {
    clearPerson();
    return gotoList();
  }

  public String addPerson() {
    personResults.add(person);
    return gotoList();
  }
}

詹姆斯列表.xhtml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html">
<h:head>
  <title>list page</title>
</h:head>

<body>
<div class="container">
  <div class="content">
    <h:messages showSummary="true" showDetail="false" errorClass="error" infoClass="info"
                warnClass="warn"/>
    <h:form>
      <h:dataTable value="#{james.personResults}" var="person">
        <h:column>
          <f:facet name="header">Id</f:facet>
          <h:outputText value="#{person.id}"/>
        </h:column>
        <h:column>
          <f:facet name="header">Name</f:facet>
          <h:outputText value="#{person.firstName}"/>
        </h:column>
        <h:column>
          <f:facet name="header">Surname</f:facet>
          <h:outputText value="#{person.lastName}"/>
        </h:column>
      </h:dataTable>
      <h:panelGroup layout="block">
        <h:commandButton value="Add" action="#{james.gotoAdd}"/>
      </h:panelGroup>
    </h:form>
  </div>
</div>
<ui:debug hotkey="L" rendered="true"/>
</body>
</html>

詹姆斯-add.xhtml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html">
<h:head>
  <title>add page</title>
</h:head>

<body>
<div class="container">
  <div class="content">
    <h:messages showSummary="true" showDetail="false" errorClass="error" infoClass="info"
                warnClass="warn"/>
    <h:form>
      <fieldset>
        <legend>Add Person</legend>
        <h:panelGrid columns="2">
          <h:outputLabel for="PersonId" value="Id:"/>
          <h:inputText id="PersonId" value="#{james.person.id}" binding="#{james.idComponent}"/>
          <h:outputLabel for="PersonFirstName" value="First Name:"/>
          <h:inputText id="PersonFirstName" value="#{james.person.firstName}" binding="#{james.firstNameComponent}"/>
          <h:outputLabel for="PersonLastName" value="Last Name:"/>
          <h:inputText id="PersonLastName" value="#{james.person.lastName}" binding="#{james.lastNameComponent}"/>
        </h:panelGrid>
        <h:panelGroup layout="block">
          <h:commandButton value="Add" action="#{james.addPerson}"/>
          <h:commandButton value="Cancel (immediate)" action="#{james.cancelAdd}" immediate="true"/>
          <h:commandButton value="Cancel" action="#{james.cancelAdd}"/>
        </h:panelGroup>
      </fieldset>
    </h:form>
  </div>
</div>
<ui:debug hotkey="L" rendered="true"/>
</body>
</html>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top