ui 中动态添加的输入字段:表单提交期间不会处理重复
我试图在我的应用程序中为答案创建一个输入表单,并从四个“空”答案开始,视图循环遍历这些答案并为其创建输入字段。我有一个添加答案按钮,我将一个问题添加到答案数组中,然后视图再次呈现答案,但现在有一个额外的输入字段。支持 bean 是 viewscoped 的。但是,如果我提交表单而不按“添加答案”按钮,那么一切都会正常。数据保存在数据库中。但是,如果我在填写四个答案后添加答案,则最后一个答案不会从输入字段(answer.description)获取数据。如果我先按“添加答案”(不填写任何输入字段),则根本不会捕获字段中的数据,将所有 5 个字段留空,因此数据库中不会保存任何数据。
我的形式如下:
<ui:repeat var="answer" value="#{bean.answers}">
<div class="field">
<h:outputLabel for="answerAlternative-#{answer.serialNumber}"
value="Svaralternativ #{answer.serialNumber}" />
<h:inputText id="answerAlternative-#{answer.serialNumber}"
value="#{answer.description}" size="40" />
</div>
</ui:repeat>
这是创建新输入字段的方法:
public String addAnswer() {
if (answers.size() + 1 < 6) {
Answer answer = new Answer();
answer.setSerialNumber(answerSerialNumber + "");
answerSerialNumber++;
answers.add(answer);
}
return null;
}
用于用四个空输入字段初始化答案数组:
@PostConstruct
public void initBean() {
answers = new ArrayList<Answer>();
for (int i = 0; i < 4; i++) {
addAnswer();
}
}
I am trying to make a input form for answers in my application and I start with four "empty" answers which the view loops over and make input fields for. I have an add answer button which I add one question to the array of answers and then the view render the answers again, but now with an additional input field. The backing bean is viewscoped. However if I submit the form without pressing the add answer button it all works. The data is saved in the database. But if I add an answer after the four is filled out the last one does not get the data from the inputfield (answer.description). If I press the add answer first (without filling out any input fields) the data from the fields are not captured at all leaving all 5 empty so no data is saved in the database.
I have this in the form:
<ui:repeat var="answer" value="#{bean.answers}">
<div class="field">
<h:outputLabel for="answerAlternative-#{answer.serialNumber}"
value="Svaralternativ #{answer.serialNumber}" />
<h:inputText id="answerAlternative-#{answer.serialNumber}"
value="#{answer.description}" size="40" />
</div>
</ui:repeat>
This is the method for creating a new input field:
public String addAnswer() {
if (answers.size() + 1 < 6) {
Answer answer = new Answer();
answer.setSerialNumber(answerSerialNumber + "");
answerSerialNumber++;
answers.add(answer);
}
return null;
}
Used for initializing the answers array with four empty input fields:
@PostConstruct
public void initBean() {
answers = new ArrayList<Answer>();
for (int i = 0; i < 4; i++) {
addAnswer();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这看起来与 Mojarra 中
当前的问题相符。是Mojarra 完全崩溃了。您基本上有 2 个选择:
实现
。UIData
组件而不是
,例如
,Tomahawk 的
,PrimeFaces '
等This look to match the current problems of
<ui:repeat>
in Mojarra. It is totally broken in Mojarra.You have basically 2 options:
<ui:repeat>
.UIData
component instead of<ui:repeat>
, e.g.<h:dataTable>
, Tomahawk's<t:dataList>
, PrimeFaces'<p:dataList>
, etc.