2014-11-16 40 views
17

Ho un modulo che chiede all'utente di inserire l'ID. Questo modulo viene inviato a un servlet che controlla il database per vedere se l'utente esiste. Se l'utente esiste allora mi rimanda gli articoli ordinati. Gli articoli ordinati vengono restituiti come un elenco di array. Quindi questo elenco di array viene reindirizzato al file jsp per visualizzarlo sulla pagina Web. L'utente può avere più di un ordine, pertanto la dimensione dell'elenco di array può variare. Come ottengo le dimensioni dell'elenco di array in modo da poter visualizzare ogni elemento nell'elenco di array? Non voglio usare JSTL.Come ottenere la dimensione dell'elenco di array in java jsp?

index.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<!DOCTYPE html> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>JSP Page</title> 
<script> 
    /*function signin(id, id2) { 
    document.getElementById(id).style.display = 'block'; 
    document.getElementById(id2).style.display = 'none'; 
    //document.getElementById(id3).style.display = 'none'; 
    }*/ 
</script> 

<form id="Signin" method="post" action="FindUser"> 
    <h2>Login </h2> 
    <input type="text" name="txtCustID" 
      placeholder="UserID"><br> 
    <br><input type="submit" value="Find"> 
</form> 
<%--!<form id="Signup" method="post" action="FindUser" 
     style="display:none;"> 
    <h2>Sign Up </h2> 
    <input type="text" name="UserId" 
      placeholder="User ID"><br> 
    <br><input type="text" name="FirstaName" 
       placeholder="First Name"><br> 
    <br><input type="text" name="LastName" 
       placeholder="Last Name"><br> 
    <br><input type="text" name="Street" 
       placeholder="Street"><br> 
    <br><input type="text" name="City" 
       placeholder="City"><br> 
    <br><input type="submit" value="Sign Up"> 
</form> --%> 
</body> 

FindUser.java:

public class FindUser extends HttpServlet { 

@Override 
protected void doPost(HttpServletRequest request, HttpServletResponse 
    response) throws ServletException, IOException { 


String sID = request.getParameter("txtCustID"); 

String url = ("admin/UserFound.jsp"); 
try { 
    Users one = UserDAO.findUser(sID); 
    request.setAttribute("theCustomer", one); 
    if (one.getFirstName().equals("none")) { 
     url = "admin/UserNotFound.jsp"; 
    } 
    ArrayList user_order = UserDAO.findOrder(sID); 
    request.setAttribute("theOrder", user_order); 
    response.sendRedirect(url); 
    //RequestDispatcher rd = request.getRequestDispatcher(url); 
    //rd.forward(request, response); 

} catch (ClassNotFoundException e) { 
    System.err.print("Failed to load Driver"); 
    System.err.print(e); 
} catch (SQLException e) { 
    System.err.print("SQL Error" + e); 
    System.err.print("SQL State: " + e.getSQLState()); 
    System.err.print("Error Code: " + e.getErrorCode()); 
} catch (Exception e) { 
    System.err.println(e); 
} 
} 

UserFound.jsp:

<%-- 
Document : CustomerFound 
Created on : Nov 15, 2014, 9:40:47 PM 
Author  : mississauga 
--%> 

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<!DOCTYPE html> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>JSP Page</title> 
</head> 
<body> 


<p>First word is: ${theOrder[x].getBoxes()}</P> 

<%--<form id="Signin" method="post" action="FindUser" 
     style="display:none;"> 
    <h2>Login </h2> 
    <input type="text" name="txtCustID" 
      placeholder="UserID"><br> 
    <br><input type="submit" value="Find"> 
</form> --%> 
</body> 
</html> 

} 

risposta

51

Utilizzare la funzione fn:length.

Declare fn namespace sul l'inizio del file JSP

<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> 

Più avanti nel codice

${fn:length(collection)} 
-1

si dovrebbe impostare ambito della UserFound.jsp a request.

ArrayList orderList = (ArrayList) request.getAttribute("theOrder"); 

<ul> 
<% If(orderList != null) {%> 

    <% for(String orderName : orderList) { %> 
     <li> <%= orderName %> </li> 
    <% } %> 

<% }else{ %> 
    No Order Found 
<% } %> 


</ul> 

O

ArrayList orderList = (ArrayList) request.getAttribute("theOrder"); 
<ul> 
<% If(orderList != null) {%> 

<% for(int orderNum =0;i< orderList.size();++orderNum ) { %> 
    <li> <%= orderList.get(orderNum)%> </li> 
<% } %> 
<% }else{ %> 
    No Order Found 
<% } %> 
</ul> 
-1
ArrayList orderList = (ArrayList) request.getAttribute("theOrder"); 

//Unchecked cast from Object to ArrayList 
+1

si può aggiungere una spiegazione di come funziona? –

Problemi correlati