如何在不使用JSTL的情况下遍历JSP上的数组列表

uxhixvfz  于 2022-12-07  发布在  其他
关注(0)|答案(3)|浏览(176)

//我已经从MySQL中检索了一个结果,并创建了一个数组列表用户。//我已经发送了这个用户数组列表,并通过请求响应对象发送了它。现在我需要显示一个JSP页面。//1.Without //2.With JSTL
//表的名称是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;
    }

}

//服务小程序

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);
        }
    }

//如何在JSP页面中取得这个

khbbv19g

khbbv19g1#

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();

使用pojo类访问它:
用户名();用户名和密码

%>
u0sqgete

u0sqgete2#

使用JSTL

需要创建POJO类

class UserReg {
  private Integer id;
  private String userName;
  private String password;
  private String email;
  //getters and setters
}

小服务程序
需要更改列表填充逻辑

//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页面

<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>
0kjbasz6

0kjbasz63#

<%@page import="java.util.ArrayList"%>
<%@page import="kinder.dto.User"%>
...
...
<%
ArrayList<User> users = (ArrayList<User>) request.getAttribute("users");

for (User : users) {
%>
    <div><%= user.getId() %></div>
<%
}
%>

相关问题