【问题标题】:Why requestScope attribute is null and not null at the same time?为什么 requestScope 属性为空而不是同时为空?
【发布时间】: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


【解决方案1】:

在第一个 if 测试 null 之前添加一个额外的打印(最终在第二个 if 之后)像

<c:if test="${!pageContext.request.servletPath.equals('/Login')}">
    <%
        System.out.println("new login");
    %>

    <c:if test = "${requestScope.Cars!=null}">

(只是为了显示更多信息,不是真正的答案,我不开发JSP)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-06
    • 1970-01-01
    • 1970-01-01
    • 2013-03-13
    • 1970-01-01
    • 2013-12-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多