我正在使用Primefaces和JSF创建一个selectonemenu。 我想显示一条消息“选择其中一个选项”。我怎么能这样做?
这是代码:
<!--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>
谢谢
答案 0 :(得分:1)
在提交按钮下面添加也可以正常工作。 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>
答案 1 :(得分:0)
您可以使用:
if (users.getGender()==null) {
FacesContext.getCurrentInstance().addMessage(
null,
new FacesMessage(FacesMessage.SEVERITY_ERROR,
"Choose one of the options",
"Choose one of the options"));
}
答案 2 :(得分:0)
已解决。我忘记将代码行<p:message for="gender" />
添加到代码中。感谢。