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

hgb9j2n6  于 5个月前  发布在  其他
关注(0)|答案(2)|浏览(92)

我正在尝试从数据库中获取数据并将其显示在网页上。
我希望表与实体的数据,但有这个:


的数据
我还有几节课:

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"


wnrlj8wa

wnrlj8wa1#

看起来你使用的JSP版本很老了。至于你在servlet上使用注解,你应该至少使用Servlet 3.0库,这些库应该在Web服务器上可用。
如果您让web.xml检查头标记中的版本,以获得正确的Servlet版本,则至少应为2.4。如果您对为什么要使用它有疑问,因为此版本及更高版本默认使用isELIgnored="false"启用EL。如果您需要在所有页面上忽略EL,但在该页面上使用EL,则可以修改页面。

<%@ page isELIgnored ="false" %>

字符串
如果你的web应用程序提供了任何实现了servlet的库,但版本较低,你应该删除它们。
如果您使用的是pom.xml,请指定库的作用域,该作用域在服务器上以provided的形式提供。
使用可以与Web应用程序的Servlet版本一起使用的JSTL版本。您可以使用Can not find the tag library descriptor for “http://java.sun.com/jsp/jstl/core” answer下载JSTL。

ycggw6v2

ycggw6v22#

首先尝试从<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"%>

字符串

相关问题