gpt4 book ai didi

arrays - 如何在java jsp中获取数组列表大小?

转载 作者:行者123 更新时间:2023-12-03 12:01:41 28 4
gpt4 key购买 nike

我有一个要求用户输入 ID 的表格。这个表单被发送到一个 servlet,它检查数据库以查看用户是否存在。如果用户存在,那么它会将他们订购的商品发回给我。有序项作为数组列表返回。然后这个数组列表被重定向到jsp文件以显示在网页上。用户可以有多个订单,因此数组列表大小可能会有所不同。如何获取数组列表大小以便我可以显示数组列表中的每个项目?我不想使用 JSTL。

索引.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>

查找用户.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>

}

最佳答案

使用 fn:length 功能。

声明 fn JSP 文件开头的命名空间

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

后面的代码
${fn:length(collection)}

关于arrays - 如何在java jsp中获取数组列表大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26962866/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com