ber*_*tie 4 forms validation jsf messages prefix
我正在尝试使用required ="true"进行简单验证
<h:form>
<h:messages globalOnly="true"/>
<h:panelGrid>
<h:panelGrid columns="3">
<f:facet name="header">
Login Application Sample
</f:facet>
<h:outputLabel for="UserId" value="User Id" />
<h:inputText id="UserId" value="#{userBean.userId}" required="true" />
<h:message for="UserId"/>
<h:outputLabel for="Password" value="Password" />
<h:inputSecret id="Password" value="#{userBean.password}" required="true" />
<h:message for="Password" />
<f:facet name="footer">
<h:commandButton value="Login" action="#{userBean.login}"/>
<h:commandButton type="reset" value="Reset"/>
</f:facet>
</h:panelGrid>
</h:panelGrid>
</h:form>
Run Code Online (Sandbox Code Playgroud)
将字段留空,然后单击登录按钮,这些错误消息将显示在每个字段的右侧:
j_idt7:UserId:验证错误:值是必需的.
j_idt7:密码:验证错误:值是必需的.
这是我的预期,但我不想显示'j_idt7:'的表单id前缀.我读过书中的例子,他们不输出表格id前缀.我想要的是:
UserId:验证错误:值是必需的.
密码:验证错误:值是必需的.
如何跳过在组件特定消息中显示表单ID前缀?
我目前正在使用glassfish v3测试JSF 2.
Bal*_*usC 13
消息标签默认为组件的客户端ID,正好可以通过右键单击View Source在生成的HTML输出中看到.这j_id7是在此特定情况下的父的客户端ID <form>元件.如果你给JSF组件固定ID喜欢<h:form id="login">那么标签将成为login:UserId和login:Password分别.
但是,您可以使用输入组件的label属性完全覆盖它,以便消息标签将完全按照您的意图显示.
<h:inputText ... label="User ID" />
<h:inputSecret ... label="Password" />
Run Code Online (Sandbox Code Playgroud)
如果输入组件的label属性存在,则将使用它而不是客户端ID.使用prependId="false"其他答案的建议有缺点.不要那样做.
一个完全不同的替代方法是使用requiredMessage(或converterMessage或validatorMessage)属性,但这不允许参数化消息,因此您必须对标签进行硬编码等.
<h:inputText ... label="User ID is required." />
<h:inputSecret ... label="Password is required." />
Run Code Online (Sandbox Code Playgroud)
值得注意的是,像这样复制标签确实很尴尬:
<h:outputLabel for="userId" value="User ID" ... />
<h:inputText id="userId" ... label="User ID" />
<h:outputLabel for="password" value="Password" ... />
<h:inputSecret id="password" ... label="Password" />
Run Code Online (Sandbox Code Playgroud)
如果您碰巧使用JSF实用程序库OmniFaces,那么您可以使用<o:outputLabel>让JSF透明地设置label关联组件的属性:
<o:outputLabel for="userId" value="User ID" ... />
<h:inputText id="userId" ... />
<o:outputLabel for="password" value="Password" ... />
<h:inputSecret id="password" ... />
Run Code Online (Sandbox Code Playgroud)
小智 5
如果您使用MyFaces和bean验证框架(JSR303)尝试使用密钥javax.faces.validator.BeanValidator.MESSAGE定义MessageBundle
faces-config.xml中
<application>
<message-bundle>ApplicationMessages</message-bundle>
</application>
Run Code Online (Sandbox Code Playgroud)
ApplicationMessages.properties
#javax.faces.validator.BeanValidator.MESSAGE={1}: {0}
javax.faces.validator.BeanValidator.MESSAGE={0}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10171 次 |
| 最近记录: |