简体   繁体   English

Primefaces selectonemenu必需的消息

[英]Primefaces selectonemenu requiredmessage

I am creating a selectonemenu using Primefaces and JSF. 我正在使用Primefaces和JSF创建一个selectonemenu。 I want to appear a message saying "Choose one of the options". 我想出现一条消息,说“选择其中一个选项”。 How can I do that? 我怎样才能做到这一点?

This is the code: 这是代码:

  <!--Gender-->
           <p:selectOneMenu id="gender" value="#{users.gender}" required="true"
                               requiredMessage="Choose one of the options">
            <f:selectItem itemLabel="Choose gender" itemValue="#{null}" />
             <f:selectItem itemLabel="Male" itemValue="Male" />
            <f:selectItem itemLabel="Female" itemValue="Female" />
          </p:selectOneMenu>

Thanks 谢谢

adding below on the submit button will work fine as well. 在“提交”按钮上添加以下内容也可以正常工作。 ajax="false" AJAX = “假”

*********************edited**********
As you requested, please find below block of codes that works fine for me  ::                                                                                                                                                                                                    

<h:form>
<p:panelGrid  style="margin-top:5px;width:730px;">
 <f:facet name="header">
   <p:row >
    <p:column colspan="20">Select Company</p:column>
   </p:row>
 </f:facet>

 <p:row>
   <p:column>
     <p:selectOneMenu id="companyMenu" value="#{clientList.clientCode}" 
           style="width:550px;margin:5px;text-color:black" required="true" 
                               requiredMessage="Please select a company !">
       <f:selectItem itemLabel="Select a Company" noSelectionOption="true" 
                                                 itemValue="#{null}" />
       <f:selectItems value="#{clientList.clients.entrySet()}" var="entry"  
                           itemValue="#{entry.key}" itemLabel="#{entry.value}" >
       </f:selectItems>
     </p:selectOneMenu>
   </p:column>

   <p:column>
     <p:commandButton value="Submit" style="margin:5px;" action="#
            {viewPayment.companyDetails(clientList.clientCode)}" ajax="false"/>
   </p:column>
 </p:row>

</p:panelGrid>

</h:form> 

You can use : 您可以使用 :

if (users.getGender()==null) {
            FacesContext.getCurrentInstance().addMessage(
                    null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR,
                            "Choose one of the options",
                            "Choose one of the options"));
}

SOLVED.I forgot to add the line <p:message for="gender" /> to the code. 已解决。我忘了在代码中添加<p:message for="gender" />行。 Thanks. 谢谢。

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

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