I am building a table using a <ui:repeat> tag, and its length is dynamic. Each row has a <h:inputText> field. When the form is submitted, it sets the values from the form into a hashmap. That all works great, except for the validation. I need to tell the <h:message> which input it belongs to using the "for" attribute. I've tried to create a unique ID per row, based on the name of the item being used to create the row. But the <h:message> tag remains empty when I submit an invalid input, and I get the following output on the servers log (JBoss 7.1):
我正在使用
[javax.enterprise.resource.webcontainer.jsf.renderkit]
Unable to find component with ID nTAS in view.
Here is the XHTML:
这是XHTML:
<ui:repeat var="item" ...>
...
<h:inputText value="#{bean.chosenItems[item.name]}" id="n#{item.name}" >
<f:validateLongRange minimum="0" maximum="10" />
</h:inputText>
<h:message for="n#{item.name}" />
...
</ui:repeat>
In order to at least get some kind of error message in the browser, I also added this near the top of my page, and it works:
为了至少在浏览器中获得某种错误消息,我还在页面顶部添加了这个消息,它可以工作:
<h:messages styleClass="error" />
It displays this message:
它显示这个消息:
j_idt13:j_idt17:1:n: Validation Error: Value is not of the correct type.
And that kind of shows part of the problem, since the ID is that strange code at the start of the message, and it starts with "n", but doesn't contain the item's name. If I look at the source in the browser, the ID is actually: id="j_idt13:j_idt17:1:nTAS"
这是问题的一部分,因为ID是消息开始时的奇怪代码,它以“n”开头,但不包含该项目的名称。如果我查看浏览器中的源代码,ID实际上是:ID ="j_idt13:j_idt17:1:nTAS"
If I look at other components, outside of the table, they also have cryptic IDs, apparently generated by JSF.
如果我查看表之外的其他组件,它们也会有神秘的id,显然是由JSF生成的。
And what is really weird is that when I input "asdf" a second time, and re-submit the form, it then calls the action method on the bean, instead of again failing during validation phase!! How can that be?!
真正奇怪的是,当我第二次输入“asdf”并重新提交表单时,它会调用bean上的action方法,而不是在验证阶段再次失败!!这怎么可能? !
Thanks for any hints, John
谢谢你的暗示,约翰。
1 个解决方案
#1
5
You cannot create IDs dynamically with el expressions. And the "cryptic" IDs are indeed generated by jsf if you don't assign an ID to a component.
不能用el表达式动态创建id。如果不将ID分配给组件,那么“神秘的”ID确实是由jsf生成的。
But you don't need to care for the uniqueness of your ids in ui:repeat
. JSF does it for you (the "1" in the generated id string is the counter for your repeated component). Just give your input field a "fixed" id and reference it in your h:message:
但是您不需要关心ui中id的惟一性:重复。JSF为您执行它(生成的id字符串中的“1”是重复组件的计数器)。只要给你的输入框一个“固定”的id并在你的h中引用它:
<h:inputText value="#{bean.chosenItems[item.name]}" id="myID" >
<f:validateLongRange minimum="0" maximum="10" />
</h:inputText>
<h:message for="myID" />
#1
5
You cannot create IDs dynamically with el expressions. And the "cryptic" IDs are indeed generated by jsf if you don't assign an ID to a component.
不能用el表达式动态创建id。如果不将ID分配给组件,那么“神秘的”ID确实是由jsf生成的。
But you don't need to care for the uniqueness of your ids in ui:repeat
. JSF does it for you (the "1" in the generated id string is the counter for your repeated component). Just give your input field a "fixed" id and reference it in your h:message:
但是您不需要关心ui中id的惟一性:重复。JSF为您执行它(生成的id字符串中的“1”是重复组件的计数器)。只要给你的输入框一个“固定”的id并在你的h中引用它:
<h:inputText value="#{bean.chosenItems[item.name]}" id="myID" >
<f:validateLongRange minimum="0" maximum="10" />
</h:inputText>
<h:message for="myID" />