【发布时间】:2021-08-02 18:23:42
【问题描述】:
为什么 requestScope 为 null 而不是同时为 null? 这是 index.jsp 文件代码的一部分
<c:if test="${!pageContext.request.servletPath.equals('/Login')}">
<c:if test = "${requestScope.Cars!=null}">
<%
List<Car> cars = (ArrayList<Car>)request.getAttribute("Cars");
System.out.println(cars.get(0));
%>
</c:if>
<c:if test="${requestScope.Cars==null}">
<%
System.out.println(request.getAttribute("Cars")+" test");
%>
<jsp:forward page="/AllCarCategories"/>
</c:if>
</c:if>
第一个 if 语句只是为了检查它如何同时为空而不是空
这就是 AllCarCategories 的样子
@WebServlet(name = "AllCarCategories", urlPatterns = {"/AllCarCategories","/Login/AllCarCategories"})
public class AllCarCategories extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
MySQLDAOFactory dao =(MySQLDAOFactory) DAOFactory.getDAOFactory(1);
MySQLCarDao carDao = (MySQLCarDao) dao.getCarDao();
MySQLCarCategoryDao categoryDao = (MySQLCarCategoryDao)dao.getCarCategoryDao();
List<CarCategory> carCategories = categoryDao.findAllCarC();
List<Car> cars = carDao.findAllCars();
System.out.println(carDao.findAllCars().size()+" size ");
request.setAttribute("Cars",cars);
request.setAttribute("Categories",carCategories);
request.setAttribute("ImageMan", ImageManager.getInstance());
System.out.println("request url:" +request.getRequestURL()+" requst servlet Path"+request.getServletPath());
request.getRequestDispatcher("index.jsp").forward(request,response);
}
}
所以第一次当它的 null 将它转发到 AllCarCategories 并创建 Car 对象列表时,然后我将它转发到 jsp 文件并且从逻辑上它不能为 null,因为我创建了一个 Car 对象列表但它是 另一个谜团是它同时进入了两个 if 语句,因此它的 null 和 not null 同时
怎么可以同时为空而不为空?谢谢
【问题讨论】:
-
不可能同时为null和不为null。
-
但正如我在上面展示的那样
-
不,你只是认为你在上面展示了它,你只是认为它是。
-
是的,我在上面展示了它,我很好奇它同时为空而不是空
-
那些打印语句甚至不在同一个范围内,更不用说同时了。它们根本不是“同一时间”。
标签: java jsp tomcat servlets requestdispatcher