无法在网页上显示请求的属性属性

问题描述 投票:0回答:2

我正在尝试从数据库获取数据并将其显示在网页上。

我期望包含实体数据的表格,但是有这个:

enter image description here

我接下来有一些课程:

Department
:

package entity;

import javax.persistence.*;
import java.io.Serializable;


@Entity
@Table(name = "DEPT")
@NamedQuery(name = "Department.getAll", 
query = "select d from Department    d")
public class Department implements Serializable {
@Id
@Column(name = "DEPTNO")
private int DEPTNO;

@Column(name = "DNAME")
private String dname;

@Column(name = "LOC")
private String loc;

public Department() {
}

public int getDEPTNO() {
    return DEPTNO;
}

public void setDEPTNO(int DEPTNO) {
    this.DEPTNO = DEPTNO;
}

public String getDname() {
    return dname;
}

public void setDname(String dname) {
    this.dname = dname;
}

public String getLoc() {
    return loc;
}

public void setLoc(String loc) {
    this.loc = loc;
}

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    Department that = (Department) o;

    if (DEPTNO != that.DEPTNO) return false;
    if (!dname.equals(that.dname)) return false;
    return loc.equals(that.loc);

}

@Override
public int hashCode() {
    int result = DEPTNO;
    result = 31 * result + dname.hashCode();
    result = 31 * result + loc.hashCode();
    return result;
}

@Override
public String toString() {
    return "Department{" +
            "DEPTNO=" + DEPTNO +
            ", dname='" + dname + '\'' +
            ", loc='" + loc + '\'' +
            '}';
}
}

DepartmentService
:

public class DepartmentService {

public EntityManager entityManager = Persistence.createEntityManagerFactory("persistenceUnit").createEntityManager();

public List<Department> getAll(){

    TypedQuery<Department> typedQuery = entityManager.createNamedQuery("Department.getAll", Department.class);
    return typedQuery.getResultList();
}
}

ShowAllServlet
:

@WebServlet(name = "ShowAllServlet", urlPatterns = "/showAll")
public class ShowAllServlet extends HttpServlet {

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

}

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    DepartmentService departmentService = new DepartmentService();
    req.setAttribute("result", departmentService.getAll());
}
}

index.jsp
:

<%@ page import="entity.Department" %>
<%@ page import="java.util.ArrayList" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<body>
<div class="main">
<jsp:include page="/showAll"></jsp:include>
<table id="mainTable">
    <tr>
        <th>DEPTNO</th>
        <th>DNAME</th>
        <th>LOC</th>
    </tr>
    <%--@elvariable id="result" type="java.util.List"--%>
    <c:forEach items="${result}" var="obj">
        <tr>
            <td>
                <c:out value="${obj.DEPTNO}"></c:out>
            </td>
            <td>
                <c:out value="${obj.dname}"></c:out>
            </td>
            <td>
                <c:out value="${obj.loc}"></c:out>
            </td>
        </tr>
    </c:forEach>

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

我已经查过了,请求有属性

"result"

enter image description here

java jsp jstl el
2个回答
0
投票

看起来您正在使用 JSP 非常旧的版本。只要您在 servlet 上使用注释,您应该至少使用 Web 服务器上可用的 Servlet 3.0 库。

如果您有

web.xml
检查标头标记中的版本以获得正确的 Servlet 版本,则该版本至少应为 2.4。如果您有疑问为什么应该使用它,因为此版本及更高版本默认使用
isELIgnored="false"
启用 EL。如果您需要忽略所有页面上的 EL,但在该页面上使用 EL 除外,您可以修改页面。

<%@ page isELIgnored ="false" %>     

如果您的 Web 应用程序提供了任何已实现 servlet 的库,但版本较低,则应将其删除。

如果您使用

pom.xml
指定库的范围,该库在服务器上可用
provided

使用可与 Web 应用程序的 Servlet 版本一起使用的 JSTL 版本。您可以使用 找不到“http://java.sun.com/jsp/jstl/core”的标签库描述符答案来下载 JSTL。


0
投票

首先尝试从

<c:out>
标签中获取变量,你不需要它们,其次你需要进行更多导入:

<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="java.lang.String"%>
<%@page import="javax.portlet.PortletSession"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
© www.soinside.com 2019 - 2024. All rights reserved.