如何在jsf中的bean端验证空白字段? [英] How to validate blank field on bean side in jsf?

查看:52
本文介绍了如何在jsf中的bean端验证空白字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些输入字段和一些selectonemenu,我需要对其进行验证,并且基于它们的验证,我必须显示整个页面的错误消息.我还必须显示带有该输入字段和selectonemenu的消息.

I have some input fields and some selectonemenu, I need to validate them and based on their validation I have to show a error message for whole page. I have to also show message with that inputfields and selectonemenu.

我的验证仅适用于空值和空白值.由于我想显示一个基于null和空白字段的消息,因此尝试了bean端验证.但是我的验证方法从未被要求使用空白字段.

My validation is only for null and blank values. Since I want to show a message based upon null and blank fields I tried bean side validation. But my validation method is never called for blank fields.

推荐答案

只需在输入组件上使用 required ="true" .

Just use required="true" on the input components.

<h:inputText id="input" ... required="true" />
<h:message for="input" />
<h:selectOneMenu id="menu" ... required="true" />
<h:message for="menu" />

您对"bean side"不是很清楚,该问题中使用的术语非常差且过于通用,但是如果您实际上是要验证JSR303 bean并期望触发 @NotNull ,那么您需要将以下上下文参数添加到 web.xml :

You are not very clear about "bean side", the terminology used in the question is very poor and overly generic, but if you actually meant JSR303 bean validation and expecting the @NotNull to be triggered, then you need to add the following context parameter to web.xml:

<context-param>
    <param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
    <param-value>true</param-value>
</context-param>

通过这种方式,JSF会将空字符串提交的值转换为 null ,然后再传递给模型. @NotNull 不会插入空字符串.

This way JSF will convert empty string submitted values to null before passing to model. The @NotNull will namely not kick in on empty strings.

这篇关于如何在jsf中的bean端验证空白字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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