无法使用JSTL

时间:2016-07-08 07:15:39

标签: servlets foreach jstl

MainServ.java

package servs.com;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import serv.com.Users;

@WebServlet(name="mainServ",urlPatterns={"/mainServ"}) 
public class haha extends HttpServlet {
    private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        // JDBC driver name and database URL
           final String DB_URL="jdbc:mysql://localhost/test";

          //  Database credentials
          final String USER = "root";
           final String PASS = "root";

           Connection conn = null;
           Statement stmt = null;
           ArrayList<Users> usersArr = new ArrayList<Users>();
          // Set response content type
          response.setContentType("text/html");
          PrintWriter out = response.getWriter();
          String title = "Database Result";
          String docType =
            "<!doctype html public \"-//w3c//dtd html 4.0 " +
             "transitional//en\">\n";
             out.println(docType +
             "<html>\n" +
             "<head><title>" + title + "</title></head>\n" +
             "<body bgcolor=\"#f0f0f0\">\n" +
             "<h1 align=\"center\">" + title + "</h1>\n");
          try{
             // Register JDBC driver
             Class.forName("com.mysql.jdbc.Driver");

             // Open a connection
             conn = DriverManager.getConnection(DB_URL, USER, PASS);

             // Execute SQL query
             stmt = conn.createStatement();
             String sql;
             sql = "SELECT id, first, last, age FROM Employees";
             ResultSet rs = stmt.executeQuery(sql);





             while(rs.next()){
                //Retrieve by column name
                int id  = rs.getInt("id");
                int age = rs.getInt("age");
                String first = rs.getString("first");
                String last = rs.getString("last");

                Users u = new Users();
                u.setAge(age);
                u.setId(id);
                u.setFirst(first);
                u.setLast(last);

                usersArr.add(u);

             }

        rs.close();
        stmt.close();
        conn.close();
     }catch(SQLException se){
        //Handle errors for JDBC
        se.printStackTrace();

     }catch(Exception e){
        //Handle errors for Class.forName
        e.printStackTrace();

     }finally{
        //finally block used to close resources
        try{
           if(stmt!=null)
              stmt.close();
        }catch(SQLException se2){
        }// nothing we can do
        try{
           if(conn!=null)
           conn.close();
        }catch(SQLException se){
           se.printStackTrace();

        }//end finally try
     } //end try

    request.setAttribute("users", usersArr);
    request.getRequestDispatcher("NewFile.jsp").forward(request,response);
    System.out.println(usersArr.size()); 
    }
}

NewFile.JSP

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>    

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<c:forEach items="${users}" var="rowss">
  <c:forEach items="${rowss}" var="item">
     <c:out value="${item}"/> 
  </c:forEach>
</c:forEach>
<!-- 
<c:forEach var="user" items="${users}">
    <c:out value="${users.first}"/>
    <c:out value="${users.last}"/>
    <c:out value="${users.ageIp}"/>
    <c:out value="${users.id}"/>
</c:forEach>

 -->
</body>
</html>

Users.java

package servs.com;

final public class Users {
    private int id;
    private int age;
    private String first;
    private String last;

    public void setId(int i){
        this.id=i;
    }
    public void setAge(int i){
        this.age=i;
    }
    public void setFirst(String i){
        this.first=i;
    }
    public void setLast(String i){
        this.last=i;
    }
}

当我尝试在NewFile.jsp中打印时,我遇到了以下错误。

  

javax.servlet.jsp.JspTagException:不知道如何迭代   提供&#34;项目&#34;在&lt; forEach&gt;

在将对象放入Arraylist(userArr)时,我不知道错误在打印或我做错了什么。我正在将数据成功地从数据库中取出,因为我先打印它。

请您告诉我可以修改的内容以便打印数据。我会开放如果有相同的方法。

1 个答案:

答案 0 :(得分:0)

在类用户中添加Getter方法后问题得以解决。但我仍然怀疑我从未在任何地方使用过getter方法。为什么getter方法起到了任何作用。??

public String getFirst(){
    return this.first;
}

public String getLast(){
    return this.last;
}
public int getId(){
    return this.id;
}
public int getAge(){
    return this.age;
}

这是我在Users.java末尾附加的内容