jstl-在jsp中显示结果集 [英] jstl - display resultset in jsp

查看:61
本文介绍了jstl-在jsp中显示结果集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经按照BalusC的回复来解决我的问题

I have followed reply from BalusC to resolve my problem

如何制作Java ResultSet在我的jsp中可用吗?

现在,当我运行page.jsp时,出现此错误

Now, when I run page.jsp, i am getting this error

org.apache.jasper.JasperException: javax.el.PropertyNotFoundException: Property 'name' not readable on type java.lang.String

请您提出解决方案.

我也遵循了下面的规定,但没有运气.

i have followed below as well with no luck.

javax.el.PropertyNotFoundException:在JSP中使用JSTL

page.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>  
<%@page contentType="text/html" pageEncoding="UTF-8" import="java.util.List"%>  
<!DOCTYPE html>  
<html>  
    <head>  
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
        <title>JSP Page</title>  
    </head>  
    <body>  
    <c:forEach items="${rows}" var="row">
 <c:out value="${row.name}" />
 </c:forEach>
<c:if test="${not empty error}">Error: ${error}</c:if>

    </body>  
</html>  

Controller.java

public class Controller extends HttpServlet {


protected void doGet(HttpServletRequest request, HttpServletResponse response) 

throws ServletException, IOException {

try {
    List<Row> rows = Row.list();
    //request.getSession().setAttribute("rows", rows);
    request.setAttribute("rows", rows);
 } catch (SQLException e) {
    request.setAttribute("error", "Retrieving rows failed.");
    e.printStackTrace();
}

 catch (Exception e) {
    e.printStackTrace();
}
finally {System.out.print("servlet");}
request.getRequestDispatcher("page.jsp").forward(request, response);
}
}

Row.java

import javax.naming.*;
import javax.sql.*;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class Row { 
    private String name;

private String getName() {
    return name;
}

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

public static List<Row> list() throws SQLException {
    Connection connection = null;
    Statement statement = null;
    ResultSet resultSet = null;
    List<Row> rows = new ArrayList<Row>();

    try {
        Context initCtx = new InitialContext();
        Context envCtx = (Context) initCtx.lookup("java:comp/env");
        DataSource ds = (DataSource)
          envCtx.lookup("jdbc/TestDB");
         connection = ds.getConnection();
        statement = connection.createStatement();
        resultSet = statement.executeQuery("select id, name from account");
        while (resultSet.next()) {
            Row row = new Row();
            row.setName(resultSet.getString("name"));
            rows.add(row);
        }
    } catch(Exception e) {
        e.printStackTrace();
    }

    finally {
        if (resultSet != null) try { resultSet.close(); } 
 catch (SQLException logOrIgnore) {}
        if (statement != null) try { statement.close(); } 
 catch (SQLException logOrIgnore) {}
        if (connection != null) try { connection.close(); } 
 catch (SQLException logOrIgnore) {}
    }

    return rows;
}
}

非常感谢 穆罕默德

推荐答案

您的getterprivate

private String getName() {
    return name;
}

它不可见

将其更改为public

public String getName() {
    return name;
}

这篇关于jstl-在jsp中显示结果集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆