印刷ライターを使用してサーブレットでリストを直接印刷すると、リストが印刷されます。
ただし、jsp を挿入しようとすると、JSTL を使用するかスクリプトレットを使用するかにかかわらず、リストが出力されません。
オブジェクトがnullであるかどうかをJSTLとスクリプトレットでテストしようとしましたが、そうであることがわかりました!
これはなぜ起こり、どうすれば修正できますか?
動作するサーブレット コード
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();
}
オブジェクト null を突然検出するスクリプトレット コード
<%
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コードでさえ同意しているようです
<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 を使用しています。