Python中文网

一个关于 编程问题的解答网站.

有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java jsp和获取属性数据

我试图显示保存在request.setAttribute("entries", entries)中的note属性,但我无法在jsp中访问此数据,也无法找到它。我试过了,但没用

这是我的控制器类:

                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 );
                }
            }


        }   

}

这是我的观点:

<?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) 个答案

  1. # 1 楼答案

    您的请求中已包含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>