Date issues in JSF

Asked

Viewed 135 times

0

I’m having a problem with JSF. Now I don’t know if it’s browser or program error itself. It is a hotel booking management system, when I enter by IE, it accepts normal, by Firefox it gives error. Any hint ?

Reserva.xhtml

        <h:panelGrid columns="3">
            <h:outputLabel value="Data de entrada:" for="entrada"/>
            <h:inputText value="#{reservaBean.reserva.entrada}"
                         p:type="date" p:placeholder="dd/mm/aaaa" 
                         required="true" id="entrada" label="Data de entrada">
                <f:convertDateTime type="date" pattern="dd/MM/yyyy" timeZone="GMT-3"/>

In Firefox appears this:

Date Entered: could not recognize '2019-05-22' as a date. Example: 01/05/2019

Already in IE he accepts normally.

The same happens when I want to change, in IE it accepts, already in Firefox no.

Change.xhtml

            Entrada:
            <h:inputText value="#{reservaBean.reserva.entrada}" id="entrada" label="Entrada" required="true" p:type="date">
                <f:convertDateTime type="date" pattern="dd/MM/yyyy" timeZone="GMT-3"/>
            </h:inputText>

In Firefox appears that does not recognize as date:

Input: could not recognize '2019-05-30' as a date.

Java reserve.

@Dependent
public class Reserva implements Serializable {

private long id;
private String nome;
private String email;
@Future
private Date entrada;
private int noites;
private int hospedes;
private double valor;
private double valorCalculado;

public Reserva() {
}

public long getId() {
    return id;
}

public void setId(long id) {
    this.id = id;
}

public String getNome() {
    return nome;
}

public void setNome(String nome) {
    this.nome = nome;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public Date getEntrada() {
    return entrada;
}

public void setEntrada(Date entrada) {
    this.entrada = entrada;
}

public int getNoites() {
    return noites;
}

public void setNoites(int noites) {
    this.noites = noites;
}

public int getHospedes() {
    return hospedes;
}

public void setHospedes(int hospedes) {
    this.hospedes = hospedes;
}

public double getValor() {
    return valor;
}

public void setValor(double valor) {
    this.valor = valor;
}

public double getValorCalculado() {
    return valorCalculado;
}

public void setValorCalculado(double valorCalculado) {
    this.valorCalculado = valorCalculado;
}
}

Reserved.

public class ReservaDAO {

private static final String SQL_INSERIR_RESERVA = "insert into reservas (nome, email, entrada, noites, hospedes, valor) values(?,?,?,?,?,?)";
private static final String SQL_LISTAR_RESERVAS = "select * from reservas order by nome";
private static final String SQL_CONSULTAR_RESERVA = "select * from reservas where nome like ? order by nome";
private static final String SQL_EXCLUIR_RESERVA = "delete from reservas where id = ?";
private static final String SQL_ALTERAR_RESERVA = "update reservas set nome=?, email=?, entrada=?, noites=?, hospedes=?, valor=? where id=?";

private Connection connection;

public void adicionar(Reserva reserva) throws SQLException {
    try {
        connection = ConnectionFactory.getConnection();
        try {
            PreparedStatement stmt = connection.prepareStatement(SQL_INSERIR_RESERVA);
            stmt.setString(1, reserva.getNome());
            stmt.setString(2, reserva.getEmail());
            stmt.setDate(3, new java.sql.Date(reserva.getEntrada().getTime()));
            stmt.setInt(4,reserva.getNoites());
            stmt.setInt(5, reserva.getHospedes());
            stmt.setDouble(6, reserva.getValor());
            stmt.execute();
            stmt.close();
        } finally {
            connection.close();
        }
    } catch (SQLException ex) {
        throw ex;
    }
}

public List<Reserva> listar() throws SQLException {
    List<Reserva> reservas = new ArrayList<>();
    try {
        connection = ConnectionFactory.getConnection();
        try {
            PreparedStatement stmt = connection.
                    prepareStatement(SQL_LISTAR_RESERVAS);
            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                Reserva r = new Reserva();
                r.setId(rs.getLong("id"));
                r.setNome(rs.getString("nome"));
                r.setEmail(rs.getString("email"));
                r.setEntrada(rs.getDate("entrada"));
                r.setNoites(rs.getInt("noites"));
                r.setHospedes(rs.getInt("hospedes"));
                r.setValor(rs.getDouble("valor"));
                reservas.add(r);
            }
            stmt.close();
            rs.close();
        } finally {
            connection.close();
        }
    } catch (SQLException e) {
        throw e;
    }
    return reservas;
}

public List<Reserva> consultar(String nome) throws SQLException {
    List<Reserva> reservas = new ArrayList<>();
    try {
        connection = ConnectionFactory.getConnection();
        try {
            PreparedStatement stmt = connection.
                    prepareStatement(SQL_CONSULTAR_RESERVA);
            stmt.setString(1, '%' + nome + '%');
            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                Reserva r = new Reserva();
                r.setId(rs.getLong("id"));
                r.setNome(rs.getString("nome"));
                r.setEmail(rs.getString("email"));
                r.setEntrada(rs.getDate("entrada"));
                r.setNoites(rs.getInt("noites"));
                r.setHospedes(rs.getInt("hospedes"));
                r.setValor(rs.getDouble("valor"));
                reservas.add(r);
            }
            stmt.close();
            rs.close();
        } finally {
            connection.close();
        }
    } catch (SQLException e) {
        throw e;
    }
    return reservas;
}

public void excluir(Long id) throws SQLException {
    try {
        connection = ConnectionFactory.getConnection();
        try {
            PreparedStatement stmt = connection.prepareStatement(SQL_EXCLUIR_RESERVA);
            stmt.setLong(1, id);
            stmt.execute();
            stmt.close();
        } finally {
            connection.close();
        }
    } catch (SQLException ex) {
        throw ex;
    }
}

public void alterar(Reserva reserva) throws SQLException {
    try {
        connection = ConnectionFactory.getConnection();
        try {
            System.out.println(reserva);
            PreparedStatement stmt = connection.prepareStatement(SQL_ALTERAR_RESERVA);
            stmt.setString(1, reserva.getNome());
            stmt.setString(2, reserva.getEmail());
            stmt.setDate(3, new java.sql.Date(reserva.getEntrada().getTime()));
            stmt.setInt(4,reserva.getNoites());
            stmt.setInt(5, reserva.getHospedes());
            stmt.setDouble(6, reserva.getValor());
            stmt.setLong(7, reserva.getId());
            stmt.execute();
            stmt.close();
        } finally {
            connection.close();
        }
    } catch (SQLException ex) {
        throw ex;
    }
}

}

Reservabean.java

@Named(value = "reservaBean")
@SessionScoped
public class ReservaBean implements Serializable{
private static final double DIARIA_SIMPLES = 190.;
private static final double DIARIA_DUPLO = 350.;
private static final double DIARIA_TRIPLO = 500.;
private static final double DIARIA_QUADRUPLO = 620.;
private static final double[] DIARIAS = {DIARIA_SIMPLES,
    DIARIA_DUPLO, DIARIA_TRIPLO, DIARIA_QUADRUPLO};
private static final Map<String, Object> VALORESDOSATRIBUTOS;
static {
    VALORESDOSATRIBUTOS = new HashMap<>();
    VALORESDOSATRIBUTOS.put("type", "number");
    VALORESDOSATRIBUTOS.put("min", "1");
    VALORESDOSATRIBUTOS.put("max", "4");
    VALORESDOSATRIBUTOS.put("required", "required");
    VALORESDOSATRIBUTOS.put("title",
            "O número de hóspedes deve ser um valor entre 1 e 4");
}
@Inject
private Reserva reserva;
private List<Reserva> reservas;

public ReservaBean() {
}

public List<Reserva> getReservas() {
    return reservas;
}

public void setReservas(List<Reserva> reservas) {
    this.reservas = reservas;
}

public Reserva getReserva() {
    return reserva;
}

public void setReserva(Reserva reserva) {
    this.reserva = reserva;
}

public Map<String, Object> getValoresDosAtributos() {
    return VALORESDOSATRIBUTOS;
}

public String pag_novo() {
    reserva = new Reserva();
    return "/reserva";
}

public String pag_consultar() throws SQLException {
    ReservaDAO rdao = new ReservaDAO();
    reservas = rdao.listar();
    return "/consultar";
}

public String pag_alterar(Reserva r) {
    reserva = r;
    return "/alterar";
}

public String novoContato() throws SQLException {
    ReservaDAO rdao = new ReservaDAO();
    rdao.adicionar(reserva);
    reserva = new Reserva();
    resetar();
    return "/reserva";
}

public String consultar() throws SQLException {
    ReservaDAO rdao = new ReservaDAO();
    reservas = rdao.consultar(reserva.getNome());
    return null;
}

public void exluir(Reserva r) throws SQLException {
    ReservaDAO rdao = new ReservaDAO();
    rdao.excluir(r.getId());        
    reservas.remove(r);
}

public String alterar() throws SQLException {
    ReservaDAO cdao = new ReservaDAO();
    cdao.alterar(reserva);        
    reserva = new Reserva();
    return "/consultar";
}

public String cancelar() {
    reserva = new Reserva();
    return "/consultar";
}

public String reservar() {
    reserva.setValor(DIARIAS[reserva.getHospedes() - 1] * reserva.getNoites());
    return "/reserva";
}

public String calculaValor() {
   reserva.setValorCalculado(DIARIAS[reserva.getHospedes() - 1] * reserva.getNoites());
   reserva.setValor(DIARIAS[reserva.getHospedes() - 1] * reserva.getNoites());
   return null;
}

public void resetar() {
    reserva.setNome(null);
    reserva.setEmail(null);
    reserva.setEntrada(null);
    reserva.setNoites(0);
    reserva.setHospedes(0);
    reserva.setValor(0);
}
}

Thank you for your attention

  • Dates are always in dd/MM/yyyy, I don’t know why jsf shows yyyy-MM-dd

  • Dude, try to convert the date in your bean first to the dd/MM/yyyy format before moving to view

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.