简体   繁体   English

<p:inputText>更改后模型中的值未更新

[英]<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. 我有一个表格,可让我使用可以在各个bean之间切换的按钮来编辑一个bean列表(一次一个)。

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 对于html部分,我有类似

<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. 如果我更改了inplace标记内的文本,则可以很好地更新myBean中的变量,但是,如果我仅使用inputText,即使我在网页上进行了更改,bean仍将具有旧值。 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, 这是因为p:inplace editor="true"隐式将值提交给服务器,而<p:inputText不隐式地将值提交给服务器,

You can solve it in several ways 您可以通过多种方式解决它

1) add submit button like <p:commandButton to submit the value from p:inputText 1)添加<p:commandButton类的提交按钮,以从p:inputText提交值

2) use p:ajax event="keyup" or event="change" ,inside p:inputText 2)在p:inputText内部使用p:ajax event="keyup"event="change"

also take a look at the showcase p:ajax enables ajax features on supported components. 还可以看一下展示柜p:ajax在支持的组件上启用ajax功能。

ps , remove the value attribute from the p:inplace (there is no such attribute in p:inplace ) PS,删除value从属性p:inplace (存在没有这样的属性p:inplace

Lets give your components id s: 让我们给您的组件id

<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 . 根据Primefaces文档3.5 ,组件p:inplace inplace没有名为value属性。

  2. Do you submit the form someForm when changing the value of first ? 更改first的值时,是否提交someForm表单? Otherwise the updated values from first won't be passed to MyController and MyBean . 否则,从first的更新值将不会传递给MyControllerMyBean p:inplace submits the values automatically whereby you have to do it yourself it you use the standard p:inputText . p:inplace自动提交值,因此您必须使用标准p:inputText自己进行操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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