【问题标题】:How to iterate through an array list on a JSP without JSTL如何在没有 JSTL 的情况下遍历 JSP 上的数组列表
【发布时间】:2014-03-20 23:35:39
【问题描述】:

//我已经从 MySQL 检索了一个结果并创建了数组列表用户。我已经发送了这个 //US er Array-list 并通过请求响应对象发送了它。现在我需要在 //s JSP 页面上显示它。 //1.没有JSTL //2.使用JSTL

//表的名称是user_reg,它有四个字段id,username,password,email。 //请举例说明。我需要显示 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);
        }
    }

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

【问题讨论】:

  • 为什么不使用 JSTL?用它。并自己尝试一些东西。谷歌是你的朋友。如果你尝试了一些东西但你被卡住了,那么回到这里并发布你尝试过的代码。

标签: java jsp servlets arraylist


【解决方案1】:
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 类访问它:

user.getUsername(); user.getPassword();

    %>

【讨论】:

    【解决方案2】:

    使用 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>
    

    【讨论】:

    • Hello Aniket//// 我得到了 JSTL 语法.. 我想通过在服务器中接收数组列表来将 JSTL 作为表达式在 JSP 页面中完成
    • 如何在 JSP 中编写相同的代码作为表达式而不是标签
    • 如何在 JSP 中编写相同的代码作为表达式而不是 JSTL 标记
    • @lehrer :你为什么想要一个表达式?不建议使用。
    • 只是想学习。请帮忙/我知道 JSTL 更好,但我想先添加 java 然后再启动 JSTL
    猜你喜欢
    • 2015-06-22
    • 1970-01-01
    • 1970-01-01
    • 2021-11-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多