jsp和获取属性数据

时间:2015-11-30 11:10:14

标签: java jsp servlets

我正在尝试显示我保存到request.setAttribute("entries", entries)的注释属性,但我无法在jsp中访问此数据,我无法弄明白。我已经尝试了{note},但这不起作用。

这是我的控制器类:

                while( rs.next() )
                {
                        int id          = rs.getInt("id");
                        String name     = rs.getString("name");
                        String note     = rs.getString("note");
                        String title    = rs.getString("title");

                        Notes entry = new Notes(id, name, note, title);
                        entries.add(entry);
                }

                request.setAttribute("entries", entries);

                request.getRequestDispatcher( "/WEB-INF/homework2/MyNotes.jsp" ).forward(
                     request, response );


            }
            catch( SQLException e )
            {
                throw new ServletException( e );
            }
            finally
            {
                try
                {
                    if( c != null ) c.close();
                }
                catch( SQLException e )
                {
                    throw new ServletException( e );
                }
            }


        }   

}

这是我的jsp观点:

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

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
    <title>MyNotes</title>
    </head>
    <body>

    <p align="right">Hello, ${sessionScope.CurrentUser}!&nbsp;&nbsp;&nbsp;<a href="Logout">Logout</a></p>
    <span>JOT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="MyNotes">My Notes</a>&nbsp;|&nbsp;<a href="NewNote.jsp">New</a></span>
    <br>
    <br>
    <br>


   <p>${note} </p>
    <p>${applicationScope.entries.note},</p>

    </body>
    </html>

1 个答案:

答案 0 :(得分:0)

您的请求中已经有List,所以:

然后你可以(例如)把信息放到表格中:

<c:forEach items="${requestScope.entries}" var="entry">
    <tr>
        <td>ID: <c:out value="${entry.id}"/></td>
        <td>Name: <c:out value="${entry.name}"/></td>  
        <td>Note: <c:out value="${entry.note}"/></td>
        <td>Title: <c:out value="${entry.title}"/></td>  
    </tr>
</c:forEach>