简体   繁体   English

jsf Instant =“ true”关于绑定到会话bean

[英]jsf immediate=“true” regarding binding to session bean

I have a listing page that goes to an add page . 我有一个列表页面 ,可以转到添加页面 The add page has a name textbox whose value is bound to a session scoped bean . 添加页面具有一个名称文本框,其值绑定到会话作用域的bean

The listing page has an add button that goes via an action method to the add page. 列表页面上有一个添加按钮 ,该按钮通过操作方法进入添加页面。 This action method clears the object that the name textbox is bound to. 此操作方法清除名称文本框绑定到的对象。

I also have a cancel button on the add page, which is bound to an action method that again clears the value that the name textbox is bound to. 我在添加页面上还有一个“ 取消”按钮 ,该按钮已绑定到一个操作方法,该方法再次清除了名称文本框绑定到的值。

If nothing is set to immediate, this all works fine. 如果没有设置立即执行,则一切正常。

However, if I set the cancel button to immediate, if I enter values in the name field, and then click cancel, the action method is fired and clears the object in the backing bean and goes to the listing page. 但是,如果我将“取消”按钮设置为“即时”,如果我在“名称”字段中输入值,然后单击“取消”,则将触发操作方法并清除后备bean中的对象,然后转到列表页面。 If I then click add, the action method clears the object again (ignore if it's best method or not) and then goes to the add page. 如果然后单击添加,则操作方法将再次清除对象(忽略它是否是最佳方法),然后转到添加页面。 I would now expect the add page's name textbox to be empty, but it's not?! 现在,我希望添加页面的名称文本框为空,但是不是吗? Surely, since the add button is not immediate, the values should be re-bound and empty? 当然,由于添加按钮不是立即生效的,因此值应该重新绑定并为空吗?

Below is the relevant XHTML for the add button on the listing page 以下是列表页面上添加按钮的相关XHTML。

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

Below is the relevant XHTML for the input box on the add page (myBean is session scoped), followed by that of the cancel button on the add page.: 以下是添加页面(myBean在会话范围内)上输入框的相关XHTML,然后是添加页面上的“取消”按钮的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?');"/>

I almost never use the binding property of tags, except for when I need to identify which item of a list has had an action fired on it, so I am not particularly well-informed about its uses. 我几乎从不使用标签的binding属性,除非我需要确定列表中的哪一项对它触发了动作,所以我对它的用法不是特别了解。 But I know that without using binding your code would most likely work as you expected, so my expectation is that whatever javax.faces.component.UIxxx object you are binding to isn't getting reset correctly. 但是我知道,不使用binding您的代码很可能会按预期工作,因此我的期望是,绑定到的javax.faces.component.UIxxx对象都无法正确重置。

I'm having very similar problems right now. 我现在有非常相似的问题。

Besides removing the binding and/or immediate attribute, try calling setSubmittedValue() on component with binding from action called upon click on 'add' button. 除了删除绑定和/或即时属性之外,还可以尝试在组件上调用setSubmittedValue(),并通过单击“添加”按钮时调用的动作进行绑定。

Alas, even if it helps you, you would still have to do it in any action that can lead to displaying same component after cancel. las,即使它可以为您提供帮助,您仍然必须执行任何可能导致取消后显示相同组件的操作。

That's why I'm still trying to figure out some better solution... 这就是为什么我仍在尝试找出更好的解决方案的原因...

If you use immediate="true" then the value will be kept, this is how the parameter works. 如果您使用Instant =“ true”,则将保留该值,这就是参数的工作方式。 You should take a look at the following links: 您应该查看以下链接:

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

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

Ok, here's an example that I did from scratch. 好的,这是我从头开始做的一个例子。 I have two cancel buttons, one that is immediate, and one that isn't. 我有两个取消按钮,一个是立即的,另一个不是。 Example of steps to reproduce: 重现步骤的示例:

  • Go to james-list page and click Add 转到james-list页面,然后单击Add
  • The add page displays with empty fields. 添加页面显示为空字段。 Enter values for all fields and click Add. 输入所有字段的值,然后单击添加。
  • The listing page displays and is updated to include the new person. 列表页面显示并更新为包括新人员。 Click Add. 单击添加。
  • The add page displays with empty fields. 添加页面显示为空字段。 Enter values for all fields and Click Cancel (Immediate) 输入所有字段的值,然后单击“取消”(立即)
  • The listing page displays and is unchanged. 列表页面显示且未更改。 Click Add. 单击添加。
  • The add page displays however the fields are not empty as I would expect. 添加页显示,但是字段不是空的,正如我期望的那样。 Click Cancel. 单击取消。
  • The listing page displays and is unchanged. 列表页面显示且未更改。 Click Add. 单击添加。
  • The add page displays and NOW the fields are not empty. 此时将显示添加页面,并且现在字段不为空。

James.java: James.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();
  }
}

james-list.xhtml: 詹姆斯 - list.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>

james-add.xhtml: 詹姆斯 - 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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM