【问题标题】:Trying to pass a list from servlet to jsp but prints null尝试将列表从 servlet 传递到 jsp 但打印 null
【发布时间】:2024-04-19 22:20:01
【问题描述】:

我正在尝试将 ArrayList 的对象从 servlet 传递到 jsp 文件,但是当我尝试打印它时,它什么也没打印。我为jsp使用了类似帖子中的确切行...有人可以帮助我,因为我以前从未使用过jsp。 想法是使用 dom 解析器遍历 xml 文件,然后将其元素打印到特定形式的 html 表中。我的 java 代码成功收集了所有元素并将它们存储在一个列表中,我想在 jsp 中传递该列表以在询问的表中格式化...

SERVLET 代码(缺少部分会导致它很大):

import all the needed libraries

public class MyServlet extends HttpServlet {  

    private static xml_obj obj = null;
    public static ArrayList<xml_obj> objList = new ArrayList<xml_obj>();

    public static void main(String[] args){
        try {
            Start(); //starting the methods for the xml traversal and creates the list
            //System.out.println("AA"+objList.get(1).getName());
        }
        catch(Exception e) {
            e.getMessage();
        }
    }
    public void doPost (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {  
        ArrayList list = getList();
        //System.out.println(objList.get(1).getName());
        request.setAttribute ("Xml_objList", objList );  
        RequestDispatcher view = request.getRequestDispatcher("DomNav.jsp");  
        view.forward (request,response);          
    }       
    static void Start(){

                /*..........code missing.............*/
            myDOMTreeProc dtp = new myDOMTreeProc();
            dtp.processLvl(ListOfCh, 0); //processLvl -> method in myDOMTreeProc

    }

    public static ArrayList<xml_obj> getList() {
          return objList;
       }
}


class myDOMTreeProc {
 /*........DOM XML TRAVERSE.......*/
}

class attribute {
private String Name;
private String Value;

 /*.............setters/getters.......*/

}

class xml_obj {
   public int Lvl;
   private String Name;
   private String Value;
   private String Parent;
   private ArrayList<attribute> attributes=null;
   /*.............setters/getters.......*/

}

JSP 代码:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<%@page import="java.util.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
                                 "http://www.w3.org/TR/html4/loose.dtd">


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Expression Language Example</title>
</head>
<body>
<h1>TEST JSP</h1>
<% ArrayList list = (ArrayList) request.getAttribute("Xml_objList"); %>
 <c:forEach var="item" items="${Xml_objList}">
          ${item.Lvl}
    </c:forEach>
</body>
</html>

列表是正确的,我测试了它。我认为问题在于我在jsp中传递它时。

【问题讨论】:

    标签: java jsp servlets


    【解决方案1】:

    关注Java Naming convention,一切都会好起来的。只需将int Lvl 替换为int lvl;

    JSP:

    <c:forEach var="item" items="${Xml_objList}">
          ${item.lvl}
    </c:forEach>
    

    您可以尝试使用${item.getLvl()}${item['lvl']} 而不是${item['lvl']}

    【讨论】:

    • 请离开cmets...Plzzzzzzzzzzz
    • 不工作...问题我需要一个 xml 文件吗?
    • 什么不起作用? lvl 是一个 int 变量。你在哪里使用list 这里定义的&lt;% ArrayList list = (ArrayList) request.getAttribute("Xml_objList"); %&gt;
    • 您不需要任何 xml 文件。只需将所有数据存储在 Servlet 中的列表中,然后在 JSP 中迭代此列表。