我正在练习基于Java的Web开发,使用Tomcat作为应用服务器,遵循MVC模式,没有任何框架。我想从数据库中发送一些字段,并以JSP形式显示。为此,我在java类中创建了一个数据数组列表,然后将其发送到控制器,控制器再将其发送到JSP文件,在JSP文件中以表格形式显示。
然而,数组列表在控制器中是很好的(使用ArrayList上的.size()方法来确认数据库记录的相等数量),但是当到达JSP页面时,数据显然是空的。我尝试过许多链接,但大多数都不适用于我的情况(不适用于maven),也是does没有提供解决方案的唯一一个。感谢任何帮助!
模型.java菜单
package Created;
public class menu_model {
private String menuid;
private String menuscr;
private String menuprnt;
public void setmid(String menuid) {
this.menuid = menuid;
}
public void setmscr(String menuscr) {
this.menuscr = menuscr;
}
public void setmprnt(String menuprnt) {
this.menuprnt = menuprnt;
}
public String getmid() {
return menuid;
}
public String getmscr() {
return menuscr;
}
public String getmprnt() {
return menuprnt;
}
}
菜单_DAO.java
package Created;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
public class menu_DAO {
public static String cond = "jdbc:oracle:thin:@localhost:1535:DATABASE";
public static String usnm = "username";
public static String pwrd = "password";
public static ArrayList<menu_model> menu_rtrv(){
try {
PreparedStatement pst = null;
menu_model po;
Class.forName("oracle.jdbc.OracleDriver");
Connection conn = DriverManager.getConnection(cond,usnm,pwrd);
pst = conn.prepareStatement("select m_id,m_scrnm,m_prnt from menu_menus where m_id in "
+ "(select um_id from um_menus where user_id=? and umenu_enable=?)");
pst.setString(1,"TEST");
pst.setString(2, "YES");
ResultSet rs=pst.executeQuery();
ArrayList<menu_model> rsarr = new ArrayList<>();
while (rs.next())
{
po = new menu_model();
po.setmid(rs.getString("m_id"));
po.setmscr(rs.getString("m_scrnm"));
po.setmprnt(rs.getString("m_prnt"));
rsarr.add(po);
}
conn.close();
return rsarr;
}
catch (ClassNotFoundException | SQLException e)
{
e.getMessage();
}
return null;
}
}
菜单_控件.java(控制器)
package Created;
import jakarta.servlet.RequestDispatcher;
import java.io.IOException;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.*;
import java.util.ArrayList;
@WebServlet (name = "menu_cntrlr", urlPatterns = {"/mctrl"})
public class menu_cntrlr extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ArrayList alist = menu_items.menu_rtrv();
request.setAttribute("list", alist);
RequestDispatcher dispatcher = request.getRequestDispatcher("m_menu.jsp");
dispatcher.forward(request, response);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
}
m_菜单.jsp
<%@page import="java.util.*,Created.menu_model"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
<link rel="stylesheet" href="static/vendor/fontawesome/all.min.css" />
<link rel="stylesheet" href="bs_ovrd.css" />
<title>Main menu</title>
</head>
<body>
<table border=2>
<tr>
<th>Menu ID</th>
<th>Menu Scr Name</th>
<th>Menu Parent</th>
</tr>
<%
ArrayList<menu_model> dlist=(ArrayList)request.getAttribute("list");
Iterator it=dlist.iterator();
while(it.hasNext())
{
menu_model m=(menu_model)it.next();
%>
<tr>
<td><%=m.getmid() %></td>
<td><%=m.getmscr() %></td>
<td><%=m.getmprnt() %></td>
</tr>
<%
}
%>
</table>
</body>
</html>
HTTP状态500 -内部服务器错误(加载时)
Type Exception Report
Message An exception occurred processing [/m_menu.jsp] at line [30]
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
org.apache.jasper.JasperException: An exception occurred processing [/m_menu.jsp] at line [30]
28:
29:
30: Iterator it=dlist.iterator();
31: while(it.hasNext())
32: {
33: menu_model m=(menu_model)it.next();
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:610)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:499)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:380)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:328)
jakarta.servlet.http.HttpServlet.service(HttpServlet.java:792)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Root Cause
java.lang.NullPointerException: Cannot invoke "java.util.ArrayList.iterator()" because "dlist" is null
org.apache.jsp.m_005fmenu_jsp._jspService(m_005fmenu_jsp.java:148)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
jakarta.servlet.http.HttpServlet.service(HttpServlet.java:792)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:466)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:380)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:328)
jakarta.servlet.http.HttpServlet.service(HttpServlet.java:792)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Note The full stack trace of the root cause is available in the server logs.
尝试使用JSTL并加载JSP页,但if条件返回且没有列表:
<c:if test="${empty list}">
it is null
</c:if>
<c:forEach var="mitem" items="${list}">
<td><c:out value="${mitem.menuid}"/></td>
<td><c:out value="${mitem.menuscr}"/></td>
<td><c:out value="${mitem.menuprnt}"/></td>
</c:forEach>
编辑:我尝试将控制器中声明的整数传递给JSP,发现了以下错误,我认为该错误与前一个错误有关:
Exception Report
Message An exception occurred processing [m_menu.jsp] at line [21]
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
org.apache.jasper.JasperException: An exception occurred processing [m_menu.jsp] at line [21]
18: </head>
19: <body>
20: <%//ArrayList<menu_model> dlist=(ArrayList)request.getAttribute("list");
21: int sc = (int) request.getAttribute("list");
22: out.print(sc);%>
23: <table border=2>
24: <tr>
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:610)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:499)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:380)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:328)
jakarta.servlet.http.HttpServlet.service(HttpServlet.java:792)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Root Cause
java.lang.NullPointerException: Cannot invoke "java.lang.Integer.intValue()" because the return value of "jakarta.servlet.http.HttpServletRequest.getAttribute(String)" is null
org.apache.jsp.m_005fmenu_jsp._jspService(m_005fmenu_jsp.java:145)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
jakarta.servlet.http.HttpServlet.service(HttpServlet.java:792)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:466)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:380)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:328)
jakarta.servlet.http.HttpServlet.service(HttpServlet.java:792)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
下面是我添加的代码:
//in the Controller
int c=2;
request.setAttribute("list", c);
//in the JSP
<% int sc = (int) request.getAttribute("list");
out.print(sc);
%>
1条答案
按热度按时间gr8qqesn1#
在加载URL http://localhost:8080/WebApplication1/m_menu.jsp时,这将直接调用JSP,而不首先通过
menu_cntrlr
servlet。这意味着它永远没有机会向请求添加属性。然后,当JSP尝试访问这些属性时,它们将作为null
返回。为了调用servlet,您需要访问与其
@WebServlet
Map的urlPatterns
参数对应的URL:网址:这将导致首先调用servlet,设置请求属性,然后转发到JSP,JSP随后将能够成功读取属性。
当遵循MVC模式和Java Servlet API时,JSP不应该直接通过URL访问。它们被认为是控制器的实现细节,并且为了满足它们的前提条件,必须始终首先调用控制器。因此,对于webapp的put JSP files into the
WEB-INF
directory,因为这阻止了它们通过URL从web浏览器或其他HTTP客户端直接寻址。