I have this small form and am trying to understand the use of ajax in JSF validation. Here is the form. (The backing bean is just a simple bean with two variables firstName and lastName.)
我有这个小形式,并试图理解在JSF验证中使用ajax。这是表格。 (支持bean只是一个带有两个变量firstName和lastName的简单bean。)
<h:form>
<h:outputLabel for="firstName" value="First Name: " />
<h:inputText id="firstName" value="#{userBean.firstName}">
<f:validateLength minimum="5" />
<f:ajax event="keyup" render="firstNameMsg" />
</h:inputText>
<h:message id="firstNameMsg" for="firstName" style="color: red" />
<br />
<h:outputLabel for="lastName" value="Last Name: " />
<h:inputText id="lastName" value="#{userBean.lastName}">
<f:validateLength minimum="5" />
<f:ajax event="keyup" render="lastNameMsg" />
</h:inputText>
<h:message id="lastNameMsg" for="lastName" style="color: red" />
<br />
<h:commandButton value="Submit">
<f:ajax execute="firstName lastName" render=":greeting" />
</h:commandButton>
<br />
</h:form>
<h:outputText id="greeting" value="#{userBean.greeting}" />
I have ajax attached to my button which renders the outputText tag with the first name and last name upon submit which is fine. My question is why do I need to use the f:ajax
tag with the f:validateLength
tag in order to validate the two fields? My understanding is that ajax performs all the lifecycle phases up to the render response phase i.e. it includes validation. Does the validation phase not re render the page with the messages anyway regardless of whether it comes from an ajax request or a regular submit/action request? If I remove the ajax tags attached to the input fields the validation messages don't show up but they do if I remove ajax altogether and just use a regular submit using the commandButtons action event.
我有一个附加到我的按钮的ajax,它在提交时使用名字和姓氏呈现outputText标记,这很好。我的问题是为什么我需要使用带f:validateLength标签的f:ajax标签来验证这两个字段?我的理解是ajax执行渲染响应阶段之前的所有生命周期阶段,即它包括验证。无论是否来自ajax请求或常规提交/操作请求,验证阶段是否都不会重新呈现包含消息的页面?如果我删除附加到输入字段的ajax标记,则验证消息不会显示,但如果我完全删除ajax并且只使用commandButtons动作事件使用常规提交,它们就会显示。
Thanks, Alan
1 个解决方案
#1
7
This is because in normal request processing the only way (whihout any client side script processing) to submit values to the server is by sending the whole form. This happens when you remove all your <f:ajax>
. There's no possibility to get response from server (in this case your validation messages) only by typing in input fields.
这是因为在正常的请求处理中,向服务器提交值的唯一方法是(通过任何客户端脚本处理)是通过发送整个表单。删除所有
When you remove your <f:ajax>
from components firstName
and lastName
and only leave this last one with <h:commandButton>
then as it says it will execute firstName
and lastName
, do server side validation for those fields but as result it will render only :greeting
component. To fix it you can add to your <h:commandButton>
this:
当你从组件firstName和lastName中删除
<f:ajax execute="firstName lastName" render=":greeting firstNameMsg lastNameMsg"/>
But now those messages will show only when you submit your form, not when you type keys in input fields.
但现在这些消息只会在您提交表单时显示,而不是在输入字段中键入键时显示。
To get some summary: using AJAX you have to specify exactly which components (and all it's children) has to be rendered after sending request.
要获得一些摘要:使用AJAX,您必须准确指定在发送请求后必须呈现哪些组件(及其所有子组件)。
By the way, it's not a good idea to send server request for each key typed by user in input field. Better use JavaScript or other client side technologies to do this and on server only validate whole, final value.
顺便说一下,在输入字段中为用户输入的每个键发送服务器请求不是一个好主意。更好地使用JavaScript或其他客户端技术来执行此操作,并且在服务器上仅验证整个最终值。
I hope my explanation will help you.
我希望我的解释会对你有所帮助。
#1
7
This is because in normal request processing the only way (whihout any client side script processing) to submit values to the server is by sending the whole form. This happens when you remove all your <f:ajax>
. There's no possibility to get response from server (in this case your validation messages) only by typing in input fields.
这是因为在正常的请求处理中,向服务器提交值的唯一方法是(通过任何客户端脚本处理)是通过发送整个表单。删除所有
When you remove your <f:ajax>
from components firstName
and lastName
and only leave this last one with <h:commandButton>
then as it says it will execute firstName
and lastName
, do server side validation for those fields but as result it will render only :greeting
component. To fix it you can add to your <h:commandButton>
this:
当你从组件firstName和lastName中删除
<f:ajax execute="firstName lastName" render=":greeting firstNameMsg lastNameMsg"/>
But now those messages will show only when you submit your form, not when you type keys in input fields.
但现在这些消息只会在您提交表单时显示,而不是在输入字段中键入键时显示。
To get some summary: using AJAX you have to specify exactly which components (and all it's children) has to be rendered after sending request.
要获得一些摘要:使用AJAX,您必须准确指定在发送请求后必须呈现哪些组件(及其所有子组件)。
By the way, it's not a good idea to send server request for each key typed by user in input field. Better use JavaScript or other client side technologies to do this and on server only validate whole, final value.
顺便说一下,在输入字段中为用户输入的每个键发送服务器请求不是一个好主意。更好地使用JavaScript或其他客户端技术来执行此操作,并且在服务器上仅验证整个最终值。
I hope my explanation will help you.
我希望我的解释会对你有所帮助。