I have two selectOneMenu
nested, one of countries and another of cities, these load the data correctly and when selecting a country the cities are updated correctly, the problem is that both selectOneMenu
have the attribute required
and requiredMessage
, but when I do not select any of the two fields the application does nothing (which is correct), but it does not show the error messages either. In some post I read that tag
<f:selectItem
the attribute itemValue
should go with "#{null}"
but this already contains it.
the following is the code
citySelector.xhtml
<!DOCTYPE html>
<html lang="es"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:jsf="http://xmlns.jcp.org/jsf">
<body jsf:id="body">
<ui:composition>
<label jsf:for="codigoPais">Pais</label>
<h:selectOneMenu id="codigoPais" value="#{ciudadSelector.codigoPais}" required="true" requiredMessage="Debe seleccionar un pais">
<f:selectItem itemValue="#{null}" itemLabel="-- Seleccione un pais --" noSelectionOption="true"/>
<f:selectItems value="#{ciudadSelector.paises}" var="pais" itemValue="#{pais.codigo}" itemLabel="#{pais.nombre}" />
<f:ajax listener="#{ciudadSelector.onPaisChange}" render="codigoCiudad"></f:ajax>
</h:selectOneMenu>
<label jsf:for="codigoCiudad">Ciudad</label>
<h:selectOneMenu id="codigoCiudad" value="#{ciudadSelector.codigoCiudad}" required="true" requiredMessage="Debe seleccionar una ciudad">
<f:selectItem itemValue="#{null}" itemLabel="-- Seleccione una ciudad --" noSelectionOption="true"/>
<f:selectItems value="#{ciudadSelector.ciudades}" var="ciudad" itemValue="#{ciudad.codigo}" itemLabel="#{ciudad.nombre}" />
</h:selectOneMenu>
<div>
<h:messages />
</div>
</ui:composition>
</body>
</html>
CitySelectorController
@Named("ciudadSelector")
@ViewScoped
public class CiudadSelectorController implements Serializable{
@Inject
private CiudadService ciudadService;
private Map<String, List<Ciudad>> mapCiudades;
private List<Pais> paises;
private List<Ciudad> ciudades;
private String codigoPais;
private String codigoCiudad;
@PostConstruct
public void init() {
this.paises = this.ciudadService.obtenerListaPaises();
this.mapCiudades = this.ciudadService.construirMapaCiudadesPorPais();
}
//getter y setters
public void onPaisChange() {
if(this.codigoPais != null && !"".equals(this.codigoPais)) {
this.ciudades = this.mapCiudades.get(this.codigoPais);
}else {
this.ciudades = new ArrayList<>();
}
}
}
The component ciudadSelector.xhtml
is included in a form
located ininformes.xhtlm
reports.xhtml
<html lang="es" xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:jsf="http://xmlns.jcp.org/jsf">
<head jsf:id="head">
<meta charset="UTF-8" />
<title></title>
</head>
<body jsf:id="body">
<ui:composition template="../../WEB-INF/templates/layout.xhtml">
<ui:define name="main">
<form jsf:id="consulta">
<div>
<ui:include src="../componentes/comun/ciudadSelector.xhtml" />
</div>
<div>
<button type="submit" jsf:action="#{informe.obtenerInformeCiudad(ciudadSelector.codigoPais, ciudadSelector.codigoCiudad)}">
Consultar
<f:ajax execute="@form" render="informe" />
</button>
</div>
</form>
<div>
<h:messages />
</div>
</ui:define>
</ui:composition>
</body>
</html>
When making the summit of the form, the data of the country and the selected city is sent to the InformesController, this does it well when I select the two data, but when I do not select one or neither, it does not reach the method.
ReportsController
@Named("informes")
@RequestScoped
public class informesController {
@Inject
private FacesContext facesContext;
public String obtenerInformeCiudad(String codigoPais, String codigoCiudad) {
FacesMessage m = new FacesMessage(FacesMessage.SEVERITY_INFO, "ingresando al metodo", "registrando ingreso al método");
facesContext.addMessage(null, m);
return "";
}
}
The method obtenerInformeCiudad
when executed (if both fields are selected) also does not display the message sent to the FacesMessage
.
You're telling it to only update the report component (*), which doesn't seem to include the
<h:messages>
.Includes the messages id in the list of components to update.
(*) By the way, I don't see any element with id *report* in your code. Is the code you have entered correct? Normally if JSF encounters IDs that don't exist, it fails during the initial rendering of the page.