我的java课程是:
public class Name{
private String name;
private ArrayList<Phone> phoneList;
// plus getters and setters
}
public class Phone{
private String phone;
// plus getters and setters
}
我有一个NameList的ArrayList,如:
ArrayList<Name> nameList = some method that returns an ArrayList<Name>
我的servlet代码是:
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ArrayList<Name> nameList = some method that returns an ArrayList<Name>
request.setAttribute("nameList", nameList);
RequestDispatcher rd = request.getRequestDispatcher("someJSP.jsp");
rd.forward(request, response);
}
我的jsp代码是:
<c:choose>
<c:when test="${not empty requestScope['nameList'] }">
<c:forEach var="name" items="${requestScope[' nameList '] }">
Name:${name.name }
<c:choose>
<c:when test="${not empty requestScope[' nameList.phoneList'] }">
<c:forEach var="phone" items="${requestScope ['nameList.phoneList'] }">
Phone:${phone.phone } </div>
</c:forEach>
</c:when>
<c:otherwise>
<span>The phone list is empty.</span>
</c:otherwise>
</c:choose>
</c:forEach>
</c:when>
</c:choose>
当我运行jsp文件时,nameList不为空,但电话列表为空。
不知何故电话列表丢失了,我不知道如何解决这个问题。
答案 0 :(得分:0)
当我运行jsp文件时,nameList不为空,但是pone列表为空。,
因为您没有设置ponelist
某种方式电话列表丢失
在servlet中没有任何方法从getter和setter中返回值,这并没有丢失,
ArrayList<Name> nameList = some method that returns an ArrayList<Name>
request.setAttribute("nameList", nameList);
RequestDispatcher rd = request.getRequestDispatcher("someJSP.jsp");
rd.forward(request, response);
所以需要有另一个列表并设置类Phone
的属性,如上所述,
ArrayList<Phone> poneList = some method that returns an ArrayList<Phone>
request.setAttribute("ponelist", ponelist);
希望这会有所帮助!!
答案 1 :(得分:0)
我找到了解决方案。
问题出在第二个问题c:when和c:foreach。 我这样做了:
<c:choose>
<c:when test="${not empty requestScope['nameList'] }">
<c:forEach var="name" items="${requestScope[' nameList '] }">
Name:${name.name }
<c:choose>
<c:when test="${not empty requestScope[' nameList.phoneList'] }">
<c:forEach var="phone" items="${requestScope ['nameList.phoneList'] }">
Phone:${phone.phone } </div>
</c:forEach>
</c:when>
<c:otherwise>
<span>The phone list is empty.</span>
</c:otherwise>
</c:choose>
</c:forEach>
</c:when>
</c:choose>
我替换它们:
<c:choose>
<c:when test="${not empty requestScope['nameList'] }">
<c:forEach var="name" items="${requestScope[' nameList '] }">
Name:${name.name }
<c:choose>
<c:when test="${not empty nameList.phoneList }">
<c:forEach var="phone" items="${nameList.phoneList }">
Phone:${phone.phone } </div>
</c:forEach>
</c:when>
<c:otherwise>
<span>The phone list is empty.</span>
</c:otherwise>
</c:choose>
</c:forEach>
</c:when>
</c:choose>
phoneList位于我在请求中建立的nameList的每个名称对象中。 当我循环nameList时,我可以使用表达式访问phoneList:
${nameList.phoneList}
我希望我的英语是正确的。