【问题标题】:JSF strange message from <h:message for="someID">来自 <h:message for="someID"> 的 JSF 奇怪消息
【发布时间】:2011-08-28 11:10:57
【问题描述】:

我正在使用 inputText 从用户那里获取输入

<h:inputText id="firstName" value="#{beanManager.currentUser.firstName }" required="true"        style="font-size: 15px"></h:inputText>

然后我添加了一个消息标签

<h:message for="firstName"></h:message>

但是当输入文本为空白并且我按下提交时,我会收到以下错误消息:

> j_id_jsp_1382885624_1:ID: Validation Error: Value is required.

我怎样才能只显示错误信息?

> Validation Error: Value is required.

代码如下:

    <f:view>
    <h:form>
        <h:panelGrid border="1" columns="3" style="width: 643px; ">
            <h:outputText value="First Name" style="font-size: 25px; font-weight: bold"></h:outputText>
            <h:outputText value="Last Name" style="font-size: 25px; font-weight: bold"></h:outputText>
            <h:outputText value="ID" style="font-size: 25px; font-weight: bold"></h:outputText>
            <h:inputText id="firstName" value="#{beanManager.currentUser.firstName }" required="true" requiredMessage="Enter a Valid First Name" style="font-size: 15px"></h:inputText>
            <h:inputText id="LastName" value="#{beanManager.currentUser.lastName }" required="true" requiredMessage="Enter a Valid Last Name" style="font-size: 15px"></h:inputText>
            <h:inputText id="ID" value="#{beanManager.currentUser.ID}" required="true" requiredMessage="Enter a Valid ID"  style="font-size: 15px">
                </h:inputText>
            <h:form><h:commandButton action="#{beanManager.save }" value="Save Changes" style="height: 30px; width: 100px"></h:commandButton>       
            </h:form>
        </h:panelGrid>
        <h:commandLink action="backtopersonalview" value="Back To Users" style="height: 30px; width: 100px">
                </h:commandLink>
    </h:form>
</f:view>

【问题讨论】:

    标签: jsf validation jsf-1.2


    【解决方案1】:

    是的,这是可能的。

    对于当前消息,我可以说它由form iditem iddefault message 组成。

    <form_id>:<item_id>: <default_message:javax.faces.component.UIInput.REQUIRED>
    

    如果您想更改默认消息,您可以使用消息包来执行此操作,您必须在 faces-config.xml 中定义它。

    <application>
        <locale-config>
            <default-locale>en</default-locale>
            <supported-locale>en</supported-locale>
        </locale-config>
        <resource-bundle>
            <base-name>com.stackoverflow.messages.language</base-name>
        </resource-bundle>
    </application>
    

    并更改javax.faces.component.UIInput.REQUIRED 的值。

    例如,您的消息包 language.properties 可能包含:

    javax.faces.component.UIInput.REQUIRED=Your new required message!
    

    大多数JSF 实现都带有以下默认消息,我从Mojarra 得到copied

    javax.faces.component.UIInput.REQUIRED={0}: Validation Error: Value is required.
    

    还可以使用requiredMessage 属性为单个项目定义必需的消息。那么你的&lt;h:inputText /&gt; 应该是这样的:

    <h:inputText id="firstName" value="#{beanManager.currentUser.firstName }" required="true" requiredMessage="Your new required message!" style="font-size: 15px">
    

    【讨论】:

    • 有嵌套表格。当我这样做时,我经常遇到奇怪的问题。尝试对字段使用一种表单,对导航按钮使用另一种非嵌套表单或简单链接。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-30
    • 2013-08-14
    • 2011-06-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多