0

我正在使用 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>
4

1 回答 1

2

是的,这是可能的。

对于当前消息,我可以说它由form iditem id和组成default 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

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

也可以使用该requiredMessage属性为单个项目定义必需的消息。然后你<h:inputText />应该看起来像这样:

<h:inputText id="firstName" value="#{beanManager.currentUser.firstName }" required="true" requiredMessage="Your new required message!" style="font-size: 15px">
于 2011-08-28T11:54:56.450 回答