Ir para conteúdo

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Carcleo

Fazer Um If No Java Para Verificar Se Existe Retorno!

Recommended Posts

Olá pessoal,


Estou tentando fazer um if para testar o retorno de um método que retorna um Array<Professores>.


Acontece que esse retorno pode vir vazio. Ou seja, ainda não haver nenhum Professor cadastrado no banco.


Nesse caso, como fazer para imprimir aviso?


Minha código esta retornando correto mas não consegui implementar o caso de não haver registros. Quando isso ocorre, ele só retorna a tela vazia.


<div id="indexMeio">
<jsp:useBean id="listaDeProfessores"
class="dao.professor.ListaProfessores" />
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<table style="border-collapse: collapse;border: 1px solid; text-align:center">
<tr style="background-color:#000; color:#FFF">
<th style="padding:3px;width:200px;height:50px;text-align:center;">NOME</th>
<th style="padding:3px;width:100px;height:50px;text-align:center;">LOGIN</th>
<th style="padding:3px;width:100px;height:50px;text-align:center;">INCLUSÃO</th>
<th style="padding:3px;width:200px;height:50px;text-align:center;">SALA</th>
<th style="padding:3px;width:100px;height:50px;text-align:center;">EDITAR</th>
<th style="padding:3px;width:100px;height:50px;text-align:center;">TROCAR SENHA</th>
<th style="padding:3px;width:100px;height:50px;text-align:center;">BLOQ/DESBLOQ</th>
<th style="padding:3px;width:100px;height:50px;align:center;">EXCLUIR</th>
</tr>

<c:forEach items="${listaDeProfessores.getListaProfessores()}"
var="professores">
<tr>
<td style="padding:3px;width:200px;height:30px;text-align:left;">${professores.nome}</td>
<td style="padding:3px;width:100px;height:30px;text-align:left;">${professores.login}</td>
<td style="padding:3px;width:100px;height:30px;text-align:center;">${professores.dataCadastro}</td>
<td style="padding:3px;width:200px;height:30px;text-align:left;">${professores.sala}</td>
<td style="padding:3px;width:100px;height:30px;text-align:center;">EDITAR</td>
<td style="padding:3px;width:100px;height:30px;text-align:center;">TROCAR SENHA</td>
<td style="padding:3px;width:100px;height:30px;text-align:center;">BLOQ/DESBLOC</td>
<td style="padding:3px;width:100px;height:30px;text-align:center;">EXCLUIR</td>
</tr>
</c:forEach>
</table>
</div>


Tentei da forma abaixo mas passa direto.


É como se o if não funcionasse.



<div id="indexMeio">
<jsp:useBean id="listaDeProfessores"
class="dao.professor.ListaProfessores" />
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<table style="border-collapse: collapse;border: 1px solid; text-align:center">
<tr style="background-color:#000; color:#FFF">
<th style="padding:3px;width:200px;height:50px;text-align:center;">NOME</th>
<th style="padding:3px;width:100px;height:50px;text-align:center;">LOGIN</th>
<th style="padding:3px;width:100px;height:50px;text-align:center;">INCLUSÃO</th>
<th style="padding:3px;width:200px;height:50px;text-align:center;">SALA</th>
<th style="padding:3px;width:100px;height:50px;text-align:center;">EDITAR</th>
<th style="padding:3px;width:100px;height:50px;text-align:center;">TROCAR SENHA</th>
<th style="padding:3px;width:100px;height:50px;text-align:center;">BLOQ/DESBLOQ</th>
<th style="padding:3px;width:100px;height:50px;align:center;">EXCLUIR</th>
</tr>
<%
if ( listaDeProfessores.getListaProfessores() == null )
{
out.print("<tr><td colspan=8 style='padding:3px;width:200px;height:30px;text-align:center;'> Não há registros</td></tr>");
}
else
{
%>

<c:forEach items="${listaDeProfessores.getListaProfessores()}"
var="professores">
<tr>
<td style="padding:3px;width:200px;height:30px;text-align:left;">${professores.nome}</td>
<td style="padding:3px;width:100px;height:30px;text-align:left;">${professores.login}</td>
<td style="padding:3px;width:100px;height:30px;text-align:center;">${professores.dataCadastro}</td>
<td style="padding:3px;width:200px;height:30px;text-align:left;">${professores.sala}</td>
<td style="padding:3px;width:100px;height:30px;text-align:center;">EDITAR</td>
<td style="padding:3px;width:100px;height:30px;text-align:center;">TROCAR SENHA</td>
<td style="padding:3px;width:100px;height:30px;text-align:center;">BLOQ/DESBLOC</td>
<td style="padding:3px;width:100px;height:30px;text-align:center;">EXCLUIR</td>
</tr>
</c:forEach>
<% } %>
</table>
</div>

Compartilhar este post


Link para o post
Compartilhar em outros sites

consegui assim:

 

<div id="indexMeio">
 
<jsp:useBean id="listaDeProfessores" class="dao.professor.ListaProfessores" />
 
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
 
<table style="border-collapse: collapse;border: 1px solid; text-align:center">
<tr style="background-color:#000; color:#FFF">
<th style="padding:3px;width:200px;height:50px;text-align:center;">NOME</th>
<th style="padding:3px;width:100px;height:50px;text-align:center;">LOGIN</th>
<th style="padding:3px;width:100px;height:50px;text-align:center;">INCLUSÃO</th>
<th style="padding:3px;width:200px;height:50px;text-align:center;">SALA</th>
<th style="padding:3px;width:100px;height:50px;text-align:center;">EDITAR</th>
<th style="padding:3px;width:100px;height:50px;text-align:center;">TROCAR SENHA</th>
<th style="padding:3px;width:100px;height:50px;text-align:center;">BLOQ/DESBLOQ</th>
<th style="padding:3px;width:100px;height:50px;align:center;">EXCLUIR</th>
</tr>
 
        <c:set var="lista" value="${listaDeProfessores.getListaProfessores().isEmpty()}" >        
        <c:choose> 
           <c:when test="${lista == true}">
       <tr>
 <td colspan=8 style="padding:3px;width:200px;height:30px;text-align:center;">Não há registros!</td>
 </tr>
           </c:when>
           <c:otherwise>
     <c:forEach items="${listaDeProfessores.getListaProfessores()}" var="professores">
   <tr>
     <td style="padding:3px;width:200px;height:30px;text-align:left;">${professores.nome}</td>
 <td style="padding:3px;width:100px;height:30px;text-align:left;">${professores.login}</td>
 <td style="padding:3px;width:100px;height:30px;text-align:center;">${professores.dataCadastro}</td>
 <td style="padding:3px;width:200px;height:30px;text-align:left;">${professores.sala}</td>
 <td style="padding:3px;width:100px;height:30px;text-align:center;">EDITAR</td>
 <td style="padding:3px;width:100px;height:30px;text-align:center;">TROCAR SENHA</td>
 <td style="padding:3px;width:100px;height:30px;text-align:center;">BLOQ/DESBLOC</td>
 <td style="padding:3px;width:100px;height:30px;text-align:center;">EXCLUIR</td>
    </tr>
     </c:forEach>
           </c:otherwise>
        </c:choose>
       </c:set>
</table>
</div>
Mas, se existe essa opção para se fazer if:

 

......
<c:if test="${listaDeProfessores.getListaProfessores().isEmpty()}">Sem Registro<br /></c:if>
........
Como se faz o 'else' dessa opção?

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.