Java 如何在没有 JSTL 的情况下遍历 JSP 上的数组列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21900810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to iterate through an array list on a JSP without JSTL
提问by lehrer
//I have retrieved a Result from MySQL and created and Array-list User .i have sent this //US er Array-list in and sent it through request Response Object. Now i need to display iton //s JSP page. //1.Without JSTL //2.With JSTL
//我已经从 MySQL 中检索了一个结果并创建了 Array-list User 。我已经发送了这个 //US er Array-list 并通过请求响应对象发送了它。现在我需要在 //s JSP 页面上显示它。//1.不带JSTL //2.带JSTL
//The Name of table is user_reg it has four fields id,username,password,email. //please do explain with example. i need to dispay all fields in a jsp page. but i dont want //to do the jdbc work on the JSP
//表的名称是user_reg它有四个字段id,用户名,密码,电子邮件。//请举例说明。我需要显示一个jsp页面中的所有字段。但我不想 // 在 JSP 上做 jdbc 工作
package kinder.dto;
public class User {
private String id;
private String userName;
private String saltedkey;
private String emailId;
private String legalName;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getLegalName() {
return legalName;
}
public void setLegalName(String legalName) {
this.legalName = legalName;
}
public String getEmailId() {
return email;
}enter code here`
public void setEmailId(String emailId) {
this.emailId = emailId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return saltedkey;
}
public void setPassword(String password) {
this.saltedkey = password;
}
}
//dto
package kinder.dto;
public class User {
private String id;
private String userName;
private String saltedkey;
private String emailId;
private String legalName;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getLegalName() {
return legalName;
}
public void setLegalName(String legalName) {
this.legalName = legalName;
}
public String getEmailId() {
return emailId;
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return saltedkey;
}
public void setPassword(String password) {
this.saltedkey = password;
}
}
//servlet
//小服务程序
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
List<User> users = UserDAO.list();
request.setAttribute("users", users); // Will be available as ${products} in JSP
request.getRequestDispatcher("loginSuccess.jsp").forward(request, response);
} catch (SQLException | ClassNotFoundException e) {
throw new ServletException("Cannot obtain products from DB", e);
}
}
//how to get this in a JSP page
//如何在JSP页面中获取它
回答by Aniket Kulkarni
With JSTL
使用 JSTL
You need to create POJO class
您需要创建 POJO 类
class UserReg {
private Integer id;
private String userName;
private String password;
private String email;
//getters and setters
}
Servlet
小服务程序
Need to change list population logic
需要更改列表填充逻辑
//code.....
ResultSet rs = ...;
ArrayList<UserReg> usersList = new ArrayList<UserReg>();
while(rs.next()) {
//here create new object of UserReg for each row
UserReg user = new UserReg();
user.setId(rs.getInt(0));
// do it for userName, password, email
.....
.....
....
// last add to list
usersList.add(user);
}
//set list in request scope and forward request to JSP
request.setAttribute("usersList",usersList);
JSP
JSP
<c:forEach var="user" items="usersList">
<c:out value="${user.id}" />
<c:out value="${user.userName}" />
<c:out value="${user.password}" />
<c:out value="${user.email}" />
</c:forEach>
回答by dnyaneshwar
get some like this :
on jsp psge :
<% ArrayList<user> userList=(ArrayList<user>) request.getAttribute("user");
Iterator<user> iter = userList.iterator();
while(iter.hasNext()){
user user = iter.next();
using pojo class access it :
使用 pojo 类访问它:
user.getUsername(); user.getPassword();
user.getUsername(); user.getPassword();
%>