1
Good morning, everyone! I was having trouble formatting dates in jsp, but with the help of friends here, I was able to solve.
The problem is that I think my code is far from good practices in JSP, I had to program inside the page, but friends told me that I could call the getInicioFormated() direct method that I would solve, but I couldn’t... I’m leaving the classes down here, I removed most of what I found unnecessary, but if anyone needs anything just say.
Obs.: for persistence I am using Hibernate.
Thanks for the personal help!
Supplier class.
/* imports e anotations desnecessários para o exemplo, foram omitidos.
public class Fornecedor extends Pessoa{
@Column
private Integer codigo;
@Column
private String pessoaContato;
@Column
private String cnpj;
@Column
private Date inicioAtividades;
/* construtores, get and seters desnecessários para o exemplo, foram omitidos.
public Date getInicioAtividades() {
return inicioAtividades;
}
public void setInicioAtividades( java.util.Date inicioAtividades) {
this.inicioAtividades = (Date) inicioAtividades;
}
public String getInicioFormatado() {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
String data = sdf.format(inicioAtividades);
return data;
}
Servlet Control Supplier
protected void buscar(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try{
List<Fornecedor> listaFornecedores = new FornecedorDao().listarFornecedores();
request.setAttribute("listaFornecedores", listaFornecedores);
request.getRequestDispatcher("cadFornecedores.jsp").forward(request, response);
}catch(Exception e){
e.printStackTrace();
}
Vendor Dao
public List<Fornecedor> listarFornecedores() {
session = HibernateUtil.getSessionFactory().openSession();
List<Fornecedor> listaFornecedores = new ArrayList<Fornecedor>();
query = session.createQuery("FROM Fornecedor");
listaFornecedores = query.list();
session.close();
return listaFornecedores;
}
Registration of JSP suppliers (where I did a lot of work)
<c:if test="${fn:length(listaFornecedores) > 0 }">
<br>
<table class="table table-hover">
<tr>
<th>Nome</th>
<th>Código</th>
<th>Telefone</th>
<th>Endereço</th>
<th>Nº</th>
<th>Cep</th>
<th>Bairro</th>
<th>Cidade</th>
<th>Estado</th>
<th>E-mail</th>
<th>Nome do Contato</th>
<th>CNPJ</th>
<th>Início das Atividades</th>
<th></th>
</tr>
<% List<Fornecedor> listaFornecedores = new FornecedorDao().listarFornecedores();
Integer i = 0;
Fornecedor f = new Fornecedor();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
%>
<c:forEach items="${listaFornecedores }" var="forn">
<tr>
<td id="nome${forn.id}">${forn.nome }</td>
<td id="codigo${forn.id}">${forn.codigo }</td>
<td id="telefone${forn.id}">${forn.telefone }</td>
<td id="endereco${forn.id}">${forn.endereco }</td>
<td id="numero${forn.id}">${forn.numeroDoEndereco }</td>
<td id="cep${forn.id}">${forn.cep }</td>
<td id="bairro${forn.id}">${forn.bairro }</td>
<td id="cidade${forn.id}">${forn.cidade }</td>
<td id="estado${forn.id}">${forn.estado }</td>
<td id="email${forn.id}">${forn.email }</td>
<td id="nomecontato${forn.id}">${forn.pessoaContato }</td>
<td id="cnpj${forn.id}">${forn.cnpj }</td>
<td id="inicioAtividades${forn.id}">
<%
f.setid(listaFornecedores.get(i).getid());
f.setInicioAtividades(listaFornecedores.get(i).getInicioAtividades());
String data = sdf.format(f.getInicioAtividades());
out.print(data);
//System.out.println(f.getInicioFormatado());
i=i+1;
%>
</td>
<td><a href="#alterarFornecedor"
class="btn btn-xs btn-info alterarFornecedor"
data-togle="modal" data-id = "${forn.id }">Alterar</a> <a
href="excluirForn.html?id=${forn.id }"
class="btn btn-xs btn-danger">Remover</a></td>
</tr>
</c:forEach>
</table>
</c:if>
Kra, thank you, really! Even more than I needed. It worked perfectly, and even gave me the idea in case you have no registered supplier, show the message with c:if. :)
– sounobre