【问题标题】:Why jsp can't find attribute from servlet为什么jsp无法从servlet中找到属性
【发布时间】:2018-10-18 13:27:36
【问题描述】:

我无法将属性 list 从 servlet 转移到 jsp。这是我的代码:

searchInfo.java:

public class searchInfo extends HttpServlet {
static final String DB_URL = "jdbc:mysql://localhost/students" + "?serverTimezone=GMT%2B8" + "&useSSL=false";

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    ServletContext context = this.getServletContext();
    Connection conn = null;
    PreparedStatement pstmt = null;
    String sql;
    ResultSet rs = null;

    List<Map> list = new ArrayList<>();

    try {
        Class.forName(context.getInitParameter("JDBC_DRIVER"));
        conn = DriverManager.getConnection(DB_URL, context.getInitParameter("USER"), context.getInitParameter("PASS"));

        sql = "SELECT * FROM INFORMATION WHERE id=?";
        pstmt = conn.prepareStatement(sql);
        pstmt.setString(1, req.getParameter("id")); //get id from form
        rs = pstmt.executeQuery();

        toList(rs, list);

        req.setAttribute("list", list);
        req.getRequestDispatcher("/search2jsp.jsp").forward(req, resp);

        rs.close();
        pstmt.close();
        conn.close();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    }finally {
        try {
            if(pstmt != null) {
                pstmt.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if(conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    doGet(req, resp);
}

static void toList(ResultSet rs, List<Map> list) throws SQLException {
    while(rs.next()) {
        String id = rs.getString("id");
        String name = rs.getString("name");
        String sex = rs.getString("sex");
        int age = rs.getInt("age");
        String college =rs.getString("college");
        String major = rs.getString("major");
        String phone = rs.getString("phone");

        Map map = new HashMap();
        map.put("id", id);
        map.put("name", name);
        map.put("sex", sex);
        map.put("age", age);
        map.put("college", college);
        map.put("major",major);
        map.put("phone", phone);

        list.add(map);

        for(Map map1 : list) {
            System.out.println(map1);
        }

    }
}

}

search2jsp.jsp

<html>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Search Students Information</title>
</head>
<body>
    <h1 align="center">Search Students Information</h1>
    <table align="center" width="100%" border="1">
        <tr>
            <th>id</th>
            <th>name</th>
            <th>sex</th>
            <th>age</th>
            <th>college</th>
            <th>major</th>
            <th>phone</th>
        </tr>

            <c:forEach items="${list}" var="usr">
                <tr>
                    <td>${usr.id}</td>
                    <td>${usr.name}</td>
                    <td>${usr.sex}</td>
                    <td>${usr.age}</td>
                    <td>${usr.college}</td>
                    <td>${usr.major}</td>
                    <td>${usr.phone}</td>
                </tr>
            </c:forEach>

    </table>
</body>
</html>

但它在 search2jsp.jsp 中显示 Cannot resolve variable 'list'

【问题讨论】:

  • 请编辑您的问题并写下问题描述。你在做什么?如需指导,请阅读how to ask questions
  • 你遇到了什么错误?
  • @JonathanLaliberte Cannot resolve variable 'list' 在代码中 &lt;c:forEach items="${list}" var="usr"&gt;
  • 好的,那么您设置的变量有问题。我认为这是因为它不是项目列表。请编辑您的帖子并包含更多 Java 代码。
  • @JonathanLaliberte 谢谢。我已经将我的代码添加到我的问题中。

标签: java jsp servlets


【解决方案1】:

我已经解决了我的问题。方法如下:

  1. jstl.jarstandard.jar添加到项目依赖项

  2. 添加<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%> 在 jsp 文件的开头。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-02
    • 1970-01-01
    • 1970-01-01
    • 2015-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-22
    相关资源
    最近更新 更多