4

我目前正在尝试使用 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>

将字段留空,然后单击登录按钮,这些错误消息将显示在每个字段的右侧:

j_idt7:UserId:验证错误:需要值。
j_idt7:密码:验证错误:需要值。

这是我所期望的,但我不想显示“j_idt7:”的表单 ID 前缀。我阅读书籍示例,它们不输出表单 id 前缀。我想要的是:

UserId:验证错误:值是必需的。
密码:验证错误:需要值。

我应该怎么做才能跳过在组件特定消息中显示表单 ID 前缀?

我目前正在 glassfish v3 中测试 JSF 2。

4

5 回答 5

13

The message label defaults to component's client ID, exactly the one as you can see in generated HTML output via rightclick, View Source. That j_id7 is in this particular case the client ID of the parent <form> element. If you give the JSF component a fixed ID like <h:form id="login"> then the labels will become login:UserId and login:Password respectively.

You can however use the input component's label attribute to override it altogether so that the message label will be shown exactly as you intented.

<h:inputText ... label="User ID" />
<h:inputSecret ... label="Password" />

If the input component's label attribute is present, then it will be used instead of the client ID. Using prependId="false" as suggested by other answers has disadvantages. Don't do that.

A completely different alternative is to use requiredMessage (or converterMessage or validatorMessage) attribute for this, but this doesn't allow parameterizing messages and thus you'd have to hardcode the labels and such.

<h:inputText ... label="User ID is required." />
<h:inputSecret ... label="Password is required." />

See also:


Noted should be that it's indeed awkward to have labels duplicated like this:

<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" />

If you happen to use JSF utility library OmniFaces, then you can use <o:outputLabel> to let JSF transparently set the label attribute of the associated component:

<o:outputLabel for="userId" value="User ID" ... />
<h:inputText id="userId" ... />

<o:outputLabel for="password" value="Password" ... />
<h:inputSecret id="password" ... />
于 2010-12-08T17:10:43.270 回答
5

如果您使用 MyFaces 和 bean 验证框架 (JSR303),请尝试使用键 javax.faces.validator.BeanValidator.MESSAGE 定义 MessageBundle

面孔-config.xml

<application>
    <message-bundle>ApplicationMessages</message-bundle>
</application>

ApplicationMessages.properties

#javax.faces.validator.BeanValidator.MESSAGE={1}: {0}
javax.faces.validator.BeanValidator.MESSAGE={0}

细节

于 2011-10-27T17:05:02.470 回答
2

您需要覆盖来自 JSF 的这些消息。

您可以在类路径中有一个 messages.properties 文件

消息属性

javax.faces.component.UIInput.REQUIRED=Field must be entered.  

Faces-config.xml

<application>
      <message-bundle>Messages</message-bundle>
      <locale-config>
          <default-locale>en</default-locale>

      </locale-config>
  </application>    

看看这篇文章

于 2010-12-08T07:34:29.513 回答
1

你在哪里写required = true,你可以定义requiredMessage = "My Message"任何输入类型。

例如

<h:inputText autocomplete="false" id = "CellNumber" label="Cell Number" required="true" requiredMessage="Cell Number Required" maxlength="10" value="#{userManagedBean.cellNumber}" >
于 2012-07-19T15:17:46.130 回答
1

如果您想从浏览器中查看 HTML 源代码,您会发现id您的input字段的 是<form-id>+":"+<input-field-id>,在您的情况下,j_idt7:UserId:。试着给你<h:form>一些有意义id的东西,以便从中理解。您可以在此处阅读有关JSF ID 的信息form如果你不喜欢它,你可以通过将你的标签修改为这样的东西来关闭它,

<h:form prependId = false> // its true by default.

但这可能会产生问题,正如BalusC在这里指出的那样。

此外,您似乎从未自己配置过任何验证消息。这反过来又以这条消息结束。因此,message.properties需要一个文件来控制消息并显示更合适的内容。即使这样,字段名称也不应该是消息的一部分,以使这些验证消息通用以避免重复。请参阅BalusC关于label<h:inputText>.

于 2010-12-08T07:44:41.860 回答