验证消息显示在页面JSF的底部-Javax Faces Developmentstage消息 [英] Validation messages are displaying at the bottom of the page JSF - javax faces developmentstage messages

查看:109
本文介绍了验证消息显示在页面JSF的底部-Javax Faces Developmentstage消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被困在JSF中显示错误消息.我的要求如下:

I am stuck with displaying error message in JSF. My requirement is given below:

我有一个用户表单,用户必须在其中输入详细信息,例如名字,姓氏,用户名和电子邮件地址.所有这些字段都是必填字段,其中,用户名和电子邮件地址是唯一的.因此,当用户输入所有字段时,如果他单击保存",那么在后端,我必须检查数据库中是否已经存在用户名和电子邮件地址,如果存在,我必须在页面顶部显示错误消息,说输入的数据已经存在,请重新输入."

I have a user form, where the user has to enter details like firstname, lastname, username and emailaddress. All these fields are mandatory, among these fields, username and emailaddress are unique. So when the user enters all the fields, and if he clicks save then at the backend I have to check whether username and email address already exists in the db, if it exists i have to show error message on the top of the page saying "the data entered is already exists, please reenter again".

名字和姓氏的验证消息应显示在每个字段的侧面.我不能使用<h:messages>,因为它会一次显示所有验证消息.

And validation messages for firstname and lastname should display on the side of each field. I can't use <h:messages> because it displays all the validation messages at once.

因此,我使用以下代码在表单中的隐藏字段中填充了消息:

so I am populating a hidden field in the form with the message using the following code:

context.addMessage("addUserErrorMessage", new FacesMessage("the data entered is already exists, please reenter again"));

addUserErrorMessage是隐藏表单字段的ID.

addUserErrorMessage is id of the hidden form field.

但是当我在页面顶部以

<h:message for="addUserFormMessage">

该消息显示在页面底部 当我通过萤火虫检查时,发现以下标记正在填充

the message is being displayed at the bottom of the page when I inspect through firebug, I found the following tags are populating

<ul id="javax_faces_developmentstage_messages" title="Project Stage[Development: Unhandled Messages">
    <li style="color:red;">the data entered is already exists, plese reenter again </li>
</ul>

当我在页面顶部写<h:message>标签时,我无法找出消息为什么显示在底部.

I am not able to find out, why the message is showing at the bottom, as I written <h:message> tag of the top of the page.

有人可以帮我吗?

预先感谢

推荐答案

所以我使用以下代码在表单的隐藏字段中填充了消息:

context.addMessage("addUserErrorMessage", new FacesMessage("the data entered is already exists, please reenter again"));

addUserErrorMessage是隐藏表单字段的ID.

addMessage()希望将客户端ID作为第一个参数,而不是组件ID.客户端ID应该类似于formId:addUserErrorMessage.客户端ID基本上是HTML DOM元素ID.在浏览器中打开页面,右键单击它,查看源代码,找到隐藏的输入并确定其id属性.

The addMessage() expects a client ID as first argument, not a component ID. The client ID should look like formId:addUserErrorMessage. The client ID is basically the HTML DOM element ID. Open the page in browser, rightclick it, View Source, locate the hidden input and figure its id attribute.

作为一种完全不同(且更常用)的选择,只需不要滥用隐藏的输入,而只需将null设置为客户端ID:

As a completely different (and more commonly used) alternative, just don't abuse a hidden input, but just set null as client ID:

context.addMessage(null, new FacesMessage(message));

这样,消息将被设置为 Global 消息,并且您可以使用<h:messages>globalOnly属性仅显示 Global 消息.

This way the message will be set as Global message and you can use the globalOnly attribute of the <h:messages> to display Global messages only.

<h:messages globalOnly="true" />

这篇关于验证消息显示在页面JSF的底部-Javax Faces Developmentstage消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆