简体   繁体   English

在JSF 2.0中将selectOneMenu与验证一起使用

[英]Using selectOneMenu with validation in JSF 2.0

I am trying to building a drop down menu using JSF 2.0, and add a validation when the use submit the form with "Please Select" selected in the menu. 我正在尝试使用JSF 2.0构建下拉菜单,并在用户使用菜单中选择“请选择”的形式提交表单时添加验证。

<h:selectOneMenu id="templateMenu" value="#{bean.list}" required="true" requiredMessage="Please select a value.">
    <f:selectItem itemLabel="Please Select" noSelectionOption="true" />
    <f:selectItem itemLabel="Item01" itemValue="1"/>
    <f:selectItem itemLabel="Item02" itemValue="2"/>
    <f:selectItem itemLabel="Item03" itemValue="3"/>
    <f:selectItem itemLabel="Item04" itemValue="4"/>
    <f:validateRequired/>
</h:selectOneMenu>

However when I submit the page with "Please Select" selected, the validation doesn't fire, it just stay in the same page. 但是,当我提交选择了“请选择”的页面时,不会触发验证,它只会停留在同一页面上。

If anyone know what's happening please could you help me? 如果有人知道发生了什么事,请您能帮我吗?

Thanks a lot!! 非常感谢!!

You need h:message for your selectOneMenu. 您的selectOneMenu需要h:message Which will display the message of requiredMessage of selectOneMenu . 将显示selectOneMenurequiredMessage消息。

<h:message for="templateMenu"/>
<h:selectOneMenu id="templateMenu" value="#{csvUploadBean.selectedTemplate}"  required="true" requiredMessage="This value is required.">
    .....
</h:selectOneMenu>      

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM