我使用打印作者和列表打印直接在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