简体   繁体   中英

<p:inputText> value not updated in model on change

I have a form that lets me edit a list of beans (one at a time), using buttons I can switch between the beans.

Keeping it simple :

public class MyBean {
   private String text;
}

public class MyController {
   private List<MyBean> availableBeans = new ArrayList<MyBean>(); // has five MyBeans with random text
   private MyBean selectedBean; // initialized with first element of list

   private int index = 0;

   public void nextBean() { index++; }
   public void previousBean() { index--; }

   private void refreshBean() { selectedBean = availableBeans.get(index); }
}

For the html part I have something like

<h:form id="someForm">
   <!-- stuff -->

    <p:inputText value="#{myController.selectedBean.text}" />

    <p:inplace editor="true" label="#{myController.selectedBean.text}" >
        <p:inputText value="#{myController.selectedBean.text}" />
    </p:inplace>

   <!-- more stuff-->
</h:form>

If I change the text inside the inplace tag, the variable in myBean will be updated just fine, but If I only use inputText the bean will still have the old value, even if I change it on the webpage. Why is that?

Its because the p:inplace editor="true" implicitly submits the value to the server while <p:inputText does not do it implicitly,

You can solve it in several ways

1) add submit button like <p:commandButton to submit the value from p:inputText

2) use p:ajax event="keyup" or event="change" ,inside p:inputText

also take a look at the showcase p:ajax enables ajax features on supported components.

ps , remove the value attribute from the p:inplace (there is no such attribute in p:inplace )

Lets give your components id s:

<h:form id="someForm">
  <p:inputText id="first" value="#{myController.selectedBean.text}" />
  <p:inplace id="second" editor="true" value="#{myController.selectedBean.text}">
    <p:inputText id="third" value="#{myController.selectedBean.text}" />
  </p:inplace>
</h:form>
  1. According to the Primefaces Documentation 3.5 the component p:inplace has no attribute called value .

  2. Do you submit the form someForm when changing the value of first ? Otherwise the updated values from first won't be passed to MyController and MyBean . p:inplace submits the values automatically whereby you have to do it yourself it you use the standard p:inputText .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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