我使用打印作者和列表打印直接在servlet中打印列表。突然從HttpServletRequest丟失的對象
當我嘗試放入jsp時,不管打印列表是否使用JSTL或scriptlet。
我試圖在JSTL和scriptlet中進行測試,如果對象爲空,結果是它!
爲什麼會發生這種情況,我該如何解決這個問題?
servlet代碼,工程
for (Artist artist:artists){
resp.getWriter().println(artist.getName());
}
是把對象請求
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
ApplicationContext ctx =
new ClassPathXmlApplicationContext("com/helloworld/beans/helloworld-context.xml");
ArtistDao artistDao = (ArtistDao) ctx.getBean("artistDao");
List<Artist> artists = null;
try {
artists = artistDao.getAll();
} catch (SQLException e) {
e.printStackTrace();
}
req.setAttribute("artists", artists);
try {
req.getRequestDispatcher("index.jsp").forward(req, resp);
} catch (ServletException e) {
e.printStackTrace();
}
小腳本代碼,突然發現該物體空
<%
List<Artist> artists = (List<Artist>) request.getAttribute("artists");
if (artists == null) {
out.println("artists null");
}
else {
for (Artist artist: artists){
out.println(artist.getName());
}
}
%>
即使JSTL代碼似乎
servlet代碼同意
<c:if test="${artists eq null}">
Artists are null
</c:if>
<c:forEach var="artist" items="${artists}">
${artist.name}
</c:forEach>
對於我的應用程序,我使用的是weblogic,spring 2.5.6和ibatis。
你有機會嘗試不同的應用程序服務器嗎?我沒有看到代碼的任何問題 - setAttribute/requestDispatch是一種常見模式。 – mdma 2010-05-17 07:46:40
我需要在工作中使用weblogic,但我會嘗試在tomcat中測試。和你一樣,我一直認爲這是應用服務器的問題。 – Jeune 2010-05-17 07:49:45