在制作 Java EE 的firstcup-war 教程时 http://docs.oracle.com/javaee/7/firstcup/doc/web-application005.htm由于无法选择正确的 html 元素,因此未显示错误消息。我有以下代码:
<fc:inputDate id="userBirthday" date="#{dukesBDay.yourBD}" />
<h:outputText value=" #{bundle.Pattern}"/>
<p/>
<h:commandButton value="#{bundle.Submit}" action="#{dukesBDay.processBirthday}"/>
<p/>
<h:message for="userBirthday" style="color:red"/>
在浏览器中查看源代码时,我发现名称标签实际上是“j_idt6:userBirthday:j_idt16
”。因此,如果我将上面发布的代码的最后一行更改为:
<h:message for="j_idt6:userBirthday:j_idt16" style="color:red"/>
它又可以工作了。为什么 Java EE 在名称标签的开头和结尾附加 j_idt6
和 j_idt16
。为什么教程中的代码不起作用。我该怎么做才能让它工作,而不必检查源代码,实际的标签名称是什么样的。
最佳答案
inputDate 的最终 ID 将以以下格式生成:formID:ComponentId:InternalComponentId
您的表单没有ID,因此将生成一个唯一的ID。
您的 inputDate 组件的 ID 为“userBirthday”。
您的内部inputText没有ID,因此将生成一个唯一的ID。
但是您可以通过为每个组件定义 ID 来避免这种情况。
inputDate.xhtml:
<h:inputText id="inputDateId" value="#{cc.attrs.date}">
greeting.xhtml:
<h:form id="greetingFormId"> ... <fc:inputDate id="userBirthdayId" date="#{dukesBDay.yourBD}" /> ... <h:message for="greetingFormId:userBirthdayId:inputDateId" style="color:red"/>
然后您将收到如下错误消息:
greetingFormId:userBirthdayId:inputDateId: '23.12.2001' konnte nicht als Datum interpretiert werden. Beispiel: 05/06/2014
关于Java EE教程,无法访问 "userBirthday"输入日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22622462/