java - JSTL - 从 java 获取对象

标签 java jsp tomcat

我正在尝试使用 JSP 打印数据库表中的所有元素以进行分配,但要求是从后端获取元素。 (JSP CRUD 操作工作得很好)。问题是我得到一张空 table 。这是我的代码:

JSP:

<html>
<head>
<title>SELECT Operation</title>
</head>

<body>

<c:set var="students" scope="session" value="${StudentDaoImpl.allStudents}"/>

<table border="1" width="100%">
<tr>
   <th>Student ID</th>
   <th>Name</th>
</tr>

<c:forEach var="student" items="${students}">
<tr>
   <td><c:out value="${student.ID}"/></td>
   <td><c:out value="${student.Name}"/></td>
</tr>
</c:forEach>
</table>

</body>
</html>

StudentDaoImpl 类:

    public class StudentDaoImpl implements StudentDao{

    private Connection connection;

    public StudentDaoImpl(){
        Properties connectionProps = new Properties();
        connectionProps.put("user", "root");
        connectionProps.put("password", "password");

        try {
            connection = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/test",connectionProps);
            System.out.println("Connection to MySql successful.");
        } catch (Exception e) {
            System.out.println("Could not connect to MySql.");
            e.printStackTrace();
        }
    }


    @Override
    public List<Student> getAllStudents(){
        StudentDaoImpl DAO = new StudentDaoImpl();
        return DAO.executeQuery("SELECT * FROM students");
    }


    private List<Student> executeQuery(String sqlQuery){
        List<Student> students = new LinkedList<>();

        try {
            Statement statement = this.connection.createStatement();
            ResultSet resultSet = statement.executeQuery(sqlQuery);
            while(resultSet.next()){
                Student currentStudent = new Student();
                currentStudent.setID(resultSet.getInt("ID"));
                currentStudent.setName(resultSet.getString("NAME"));
                students.add(currentStudent);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return students;
    }

}

和学生类:

public class Student {
    private int id;
    private String name;

    public Student(){

    }

    public void setID(int newID){
        this.id = newID;
    }

    public int getID(){
        return this.id;
    }

    public void setName(String newName){
        this.name = newName;
    }

    public String getName(){
        return this.name;
    }

}

如果我直接从 JSP 访问数据库,一切正常,但如果我这样尝试,我得到的只是表头。

此外,StudentDaoImpl 类仅使用 java 即可正常工作并将结果打印到标准输出,因此可以肯定这是一个通信问题。

我正在使用 Netbeans。我也得到了这个运行时:

Running war on http://localhost:8080/JSPwithJDBC
Using existing Tomcat server configuration at D:\NetBeans 8.0.2\Projects\JSPwithJDBC\target\tomcat
Feb 08, 2015 3:16:13 AM org.apache.catalina.startup.Embedded start
INFO: Starting tomcat server
Feb 08, 2015 3:16:13 AM org.apache.catalina.core.StandardEngine start
INFO: Starting Servlet Engine: Apache Tomcat/6.0.29
Feb 08, 2015 3:16:14 AM org.apache.coyote.http11.Http11Protocol init
INFO: Initializing Coyote HTTP/1.1 on http-8080
Feb 08, 2015 3:16:14 AM org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-8080

它似乎正在运行 tomcat 6,即使我安装了 tomcat 8。我读过 tomcat 6 对此有问题......有什么办法可以强制它在 tomcat 8 上运行?

非常感谢任何帮助!

最佳答案

我认为您的问题的答案在您的日志片段中。如果你仔细观察,你会发现它说它正在启动与 netbeans 关联的现有 tomcat 实例。

"Using existing Tomcat server configuration at D:\NetBeans 8.0.2\Projects\JSPwithJDBC\target\tomcat"

如果您真的想从 Netbeans 开始安装 Tomcat 8。首先使用 Netbens 安装 Tomcat 8 服务器实例。

按照以下链接中的说明进行操作。它包含使用 Netbeans 安装 tomcat 7 实例的说明。

https://technology.amis.nl/2012/01/02/installing-tomcat-7-and-configuring-as-server-in-netbeans/

要回答您的 JSTL 相关问题,请使用请求处理程序数据绑定(bind)代码更新您的问题。( Controller 或请求处理程序映射类代码)

关于java - JSTL - 从 java 获取对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28389511/

相关文章:

java - JPA 给自己惹麻烦

java.lang.IllegalStateException : You need to use a Theme. AppCompat 主题(或后代)与此 Activity

java - Struts 2显示图像

java - Spring hibernate : dataSource via tomcat jndi

java - 新鲜的eclipse和tomcat中的错误

javascript - 加载页面时快速重定向

Java、mysql错误:Column count doesn't match value count at row 1

java - 在构造函数具有非空参数列表的情况下使用构造函数引用

java - 我们如何从操作中获取 JSON 响应

我的动态 Web 项目(JPA、JSP)中的 MySQL 驱动程序问题