Servlets jstl-在jsp中显示结果集
我已经按照BalusC的回复解决了我的问题 现在,当我运行page.jsp时,我得到了这个错误Servlets jstl-在jsp中显示结果集,servlets,jstl,Servlets,Jstl,我已经按照BalusC的回复解决了我的问题 现在,当我运行page.jsp时,我得到了这个错误 org.apache.jasper.JasperException: javax.el.PropertyNotFoundException: Property 'name' not readable on type java.lang.String 请你提出一个解决办法 我也遵循了下面的规则,但没有运气 page.jsp <%@ taglib prefix="c" uri="http://
org.apache.jasper.JasperException: javax.el.PropertyNotFoundException: Property 'name' not readable on type java.lang.String
请你提出一个解决办法
我也遵循了下面的规则,但没有运气
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>
JSP页面
错误:${Error}
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);
}
}
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;
}
}
公共类控制器扩展HttpServlet{
受保护的void doGet(HttpServletRequest请求、HttpServletResponse响应)
抛出ServletException、IOException{
试一试{
列表行=行。列表();
//request.getSession().setAttribute(“行”,行);
setAttribute(“行”,行);
}捕获(SQLE异常){
setAttribute(“错误”,“检索行失败”);
e、 printStackTrace();
}
捕获(例外e){
e、 printStackTrace();
}
最后{System.out.print(“servlet”);}
getRequestDispatcher(“page.jsp”).forward(请求,响应);
}
}
Row.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);
}
}
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;
}
}
import javax.naming.*;
导入javax.sql.*;
导入java.sql.*;
导入java.util.ArrayList;
导入java.util.List;
公共类行{
私有字符串名称;
私有字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共静态列表()引发SQLException{
连接=空;
Statement=null;
ResultSet ResultSet=null;
列表行=新建ArrayList();
试一试{
Context initCtx=new InitialContext();
Context-envCtx=(Context)initCtx.lookup(“java:comp/env”);
数据源ds=(数据源)
envCtx.lookup(“jdbc/TestDB”);
connection=ds.getConnection();
statement=connection.createStatement();
resultSet=statement.executeQuery(“从帐户中选择id、名称”);
while(resultSet.next()){
行=新行();
row.setName(resultSet.getString(“name”);
行。添加(行);
}
}捕获(例外e){
e、 printStackTrace();
}
最后{
如果(resultSet!=null),请尝试{resultSet.close();}
catch(SQLException logOrIgnore){}
如果(statement!=null),请尝试{statement.close();}
catch(SQLException logOrIgnore){}
如果(connection!=null),请尝试{connection.close();}
catch(SQLException logOrIgnore){}
}
返回行;
}
}
非常感谢
穆罕默德你的
getter
是private
private String getName() {
return name;
}
它是看不见的
将其更改为public
public String getName() {
return name;
}
你的
getter
是private
private String getName() {
return name;
}
它是看不见的
将其更改为public
public String getName() {
return name;
}