How can I request two dates in JSF with PrimeFaces and Java

0

I want to receive two dates, so far I have this:

In the xhtml:

<h:form id="form">
    <h:panelGrid columns="5" cellpadding="5" >
        <p:outputLabel for="fechaInicio" value="Fecha Inicial:" />
        <p:calendar id="fechaInicio" value="#{fechaV.fechainicio}" pattern="dd/MM/yyyy HH:mm:ss"/>
        <p:outputLabel for="fechaFin" value="Fecha Final:" />
        <p:calendar id="fechaFin" value="#{fechaV.fechaFin}" pattern="dd/MM/yyyy HH:mm:ss"/>
        <p:commandButton value="Consultar" id="conFechas" actionListener="#{fechaV.consulFechas}" update="demForm" icon="fa-th-list" />
    </h:panelGrid>
</h:form>
  

In java I have this:

public void onDateSelect(SelectEvent event) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
        setVfech(obtfecha.getVfech(fechaInicio));
        setVfech(obtfecha.getVfech(fechaFin));

    }

What I want is, that at the moment of giving it to consult, to be able to pull the two dates. Well the range of dates and I do not understand how the event is: (

    
asked by IvttAN 06.05.2017 в 01:06
source

1 answer

0

As I see you want to validate a range of dates, I'll give you an example to validate the initial date and a final date where they do not exceed 30 days and are not longer than today's date.

First of all in the Managed Bean define the properties to use:

    private boolean fechaInicialFinalMayoraHoy;
    private boolean fechaInicialyFinMayor30Dias;
    public static final int NEGATIVO_TREINTA = -30;

    private Date fechaInicio;
    private Date fechaFinal;

Add their respective accessor methods. If you look, we have two properties of type Date , in these will be stored the start and end date that we want to compare.

Now we add the method that will be responsible for validating the initial date and the end date:

 public void validarFechas() throws Exception {
        fechaInicialFinalMayoraHoy = false;
        fechaInicialyFinMayor30Dias = false;
        if (this.fechaInicio != null && this.fechaFinal != null) {
            if (isMayorFechaHoy(this.fechaInicio)
                    || isMayorFechaHoy(this.fechaFinal)) {
                fechaInicialFinalMayoraHoy = true;
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error!", "Fecha Inicio o Fecha Fin es mayor a la fecha de hoy"));
            }
            if (isMayor30Dias(this.fechaInicio, this.fechaFinal)) {
                fechaInicialyFinMayor30Dias = true;
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error!", "Fecha es mayor a 30 dias."));
            }
        }
    }

First we validate that the fields of type date are! = of null, later it is validated that the date is not greater than today's date (sysdate), the next validation will be that the date ranges are not greater than 30 days .

We add the code of the Boolean method that is responsible for verifying that the date entered is not greater than today's date.

 /**
     * Descripci&oacute;n de la regla: Si la fecha inicial o la fecha final es
     * mayor a la fecha acutal. la validaci&oacute termina como incorrecta
     *
     * @param fecha La fecha inicial de consulta o la fecha final de consulta .
     * @throws Exception Si las fechas violan la regla de negocio.
     */
    public Boolean isMayorFechaHoy(Date fecha) throws Exception {
        Date hoy = new Date();
        Boolean resultado = false;

        if (fecha != null && fecha.after(hoy)) {
            resultado = true;
        }

        return resultado;
    }

And finally, a method that validates the range of the two dates entered:

/**
     * Descripci&oacute;n de la regla: Si la fecha final es mayor a 30
     * d&iacute;as de la fecha inicial la validaci&oacute termina como
     * incorrecta
     *
     * @param fechaInicial La fecha inicial de consulta.
     * @param fechaFinal final de consulta.
     * @throws Exception Si las fechas violan la regla de negocio.
     */
    public Boolean isMayor30Dias(Date fechaInicial, Date fechaFinal) throws Exception {
        Date fhFinal = fechaFinal;
        Boolean resultado = false;
        if (fechaInicial != null && fhFinal != null) {
            Calendar c = Calendar.getInstance();
            c.setTime(fhFinal);
            c.add(Calendar.DATE, NEGATIVO_TREINTA);
            fhFinal = c.getTime();
            if (fechaInicial.getTime() < fhFinal.getTime()) {
                resultado = true;
            }
        }
        return resultado;
    }

With this we end up on the back end, now let's see the view, we will have our XHTML file very simple and it looks like this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui">

    <f:view contentType="text/html">
        <h:head>
            <f:facet name="first">
                <meta content='text/html; charset=UTF-8' http-equiv="Content-Type"/>
                <title>Validar rango de fechas</title>
            </f:facet>
        </h:head>
        <h:body>

            <h:form>
                <h:panelGrid id="grid" columns="1" cellpadding="1">
                    <p:messages id="messages" showDetail="true" autoUpdate="true" closable="true" />

                    <p:outputLabel for="fechaInicio" value="Fecha Inicio" />
                     <p:calendar id="fechaInicio" value="#{validarFechas.fechaInicio}" showOn="button" />    

                     <p:outputLabel for="fechaFin" value="Fecha Inicio" />
                     <p:calendar id="fechaFin" value="#{validarFechas.fechaFinal}" showOn="button" />    

                    <p:commandButton value="Validar" actionListener="#{validarFechas.validarFechas}" />
                </h:panelGrid>
            </h:form>
        </h:body>

    </f:view>
</html>

Now to do deploy. Once there is deploy OK, we will try entering a range of dates that is greater than 30 days:

By pressing the validate button, we will get the error:

Now let's enter a date greater than today:

When validating we see the error:

Versión Prime faces usada:
 Prime Faces 5.0
 Glass Fish server 4.1.1 
 JSF:2.2
    
answered by 09.05.2017 / 06:27
source