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

hgb9j2n6  于 2024-01-04  发布在  其他
关注(0)|答案(2)|浏览(304)

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


的数据
我还有几节课:

Department

  1. package entity;
  2. import javax.persistence.*;
  3. import java.io.Serializable;
  4. @Entity
  5. @Table(name = "DEPT")
  6. @NamedQuery(name = "Department.getAll",
  7. query = "select d from Department d")
  8. public class Department implements Serializable {
  9. @Id
  10. @Column(name = "DEPTNO")
  11. private int DEPTNO;
  12. @Column(name = "DNAME")
  13. private String dname;
  14. @Column(name = "LOC")
  15. private String loc;
  16. public Department() {
  17. }
  18. public int getDEPTNO() {
  19. return DEPTNO;
  20. }
  21. public void setDEPTNO(int DEPTNO) {
  22. this.DEPTNO = DEPTNO;
  23. }
  24. public String getDname() {
  25. return dname;
  26. }
  27. public void setDname(String dname) {
  28. this.dname = dname;
  29. }
  30. public String getLoc() {
  31. return loc;
  32. }
  33. public void setLoc(String loc) {
  34. this.loc = loc;
  35. }
  36. @Override
  37. public boolean equals(Object o) {
  38. if (this == o) return true;
  39. if (o == null || getClass() != o.getClass()) return false;
  40. Department that = (Department) o;
  41. if (DEPTNO != that.DEPTNO) return false;
  42. if (!dname.equals(that.dname)) return false;
  43. return loc.equals(that.loc);
  44. }
  45. @Override
  46. public int hashCode() {
  47. int result = DEPTNO;
  48. result = 31 * result + dname.hashCode();
  49. result = 31 * result + loc.hashCode();
  50. return result;
  51. }
  52. @Override
  53. public String toString() {
  54. return "Department{" +
  55. "DEPTNO=" + DEPTNO +
  56. ", dname='" + dname + '\'' +
  57. ", loc='" + loc + '\'' +
  58. '}';
  59. }
  60. }

字符串

DepartmentService

  1. public class DepartmentService {
  2. public EntityManager entityManager = Persistence.createEntityManagerFactory("persistenceUnit").createEntityManager();
  3. public List<Department> getAll(){
  4. TypedQuery<Department> typedQuery = entityManager.createNamedQuery("Department.getAll", Department.class);
  5. return typedQuery.getResultList();
  6. }
  7. }

ShowAllServlet

  1. @WebServlet(name = "ShowAllServlet", urlPatterns = "/showAll")
  2. public class ShowAllServlet extends HttpServlet {
  3. @Override
  4. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  5. }
  6. @Override
  7. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  8. DepartmentService departmentService = new DepartmentService();
  9. req.setAttribute("result", departmentService.getAll());
  10. }
  11. }

index.jsp

  1. <%@ page import="entity.Department" %>
  2. <%@ page import="java.util.ArrayList" %>
  3. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
  4. <html>
  5. <body>
  6. <div class="main">
  7. <jsp:include page="/showAll"></jsp:include>
  8. <table id="mainTable">
  9. <tr>
  10. <th>DEPTNO</th>
  11. <th>DNAME</th>
  12. <th>LOC</th>
  13. </tr>
  14. <%--@elvariable id="result" type="java.util.List"--%>
  15. <c:forEach items="${result}" var="obj">
  16. <tr>
  17. <td>
  18. <c:out value="${obj.DEPTNO}"></c:out>
  19. </td>
  20. <td>
  21. <c:out value="${obj.dname}"></c:out>
  22. </td>
  23. <td>
  24. <c:out value="${obj.loc}"></c:out>
  25. </td>
  26. </tr>
  27. </c:forEach>
  28. </table>
  29. </div>
  30. </body>
  31. </html>


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


wnrlj8wa

wnrlj8wa1#

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

  1. <%@ 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>标签中取出变量,你不需要它们,其次你需要做更多的导入:

  1. <%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
  2. <%@page import="java.lang.String"%>
  3. <%@page import="javax.portlet.PortletSession"%>
  4. <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

字符串

相关问题