来自的 JSF 奇怪消息

发布于 2024-12-01 22:17:41 字数 2015 浏览 1 评论 0原文

我使用 inputText 来获取用户的输入

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

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

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

,但是当 inputtext 为空并且我按提交时,我收到此错误消息:

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

I'm using inputText to get input from a user

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

and then I've added a message tag

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

but when the inputtext is blank and I press submit I get this error message:

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

how can I display the error message only as this?

> Validation Error: Value is required.

here is the code:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

那些过往 2024-12-08 22:17:42

是的,这是可能的。

对于当前消息,我可以说它由 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

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

还可以使用 requiredMessage 属性为单个项目定义必需的消息。那么您的 应该如下所示:

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

Yes, it's possible.

To the current message, I can say that it consists out of form id, item id and default message.

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

If you want to change the default message, you could do this with a message bundle, which you have to define in your 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>

And change the value for javax.faces.component.UIInput.REQUIRED.

For example your message bundle language.properties could contain:

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

Mostly all JSF implementations comes with the following default message, which I've copied from Mojarra:

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

It's also possible to define a required message to an single item with the requiredMessage attribute. Then your <h:inputText /> should look like this:

<h:inputText id="firstName" value="#{beanManager.currentUser.firstName }" required="true" requiredMessage="Your new required message!" style="font-size: 15px">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文