我在音乐商店工作,允许您使用由Servlet,JSP和JavaBeans组成的MVC框架购买歌曲。我有一个ControllerServlet,它根据JSP中表单的操作值将请求分派给相关的JSP视图。在同一个包中有另一个名为CartBean的类,它作为2个数组列表的模型,包含有关歌曲和专辑的信息(每个调用另外两个不同的JavaBeans,称为SongBean和AlbumBean)。以下是servlet的开始,其中3种类型的bean被声明为全局变量,因此它们暴露给整个类,而不仅仅是在doGet和doPost中。
/**
* Servlet implementation class ControllerServlet
*/
@WebServlet(urlPatterns="/search", displayName="ControllerServlet")
public class ControllerServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
Logger logger = Logger.getLogger(this.getClass().getName());
// make the SongBean and AlbumBean global variables so the parsed data is available
// to whole class (not just doGet) as long as class exists
public ArrayList<SongBean> songs = new ArrayList<SongBean>();
public ArrayList<AlbumBean> albums = new ArrayList<AlbumBean>();
public CartBean cart = new CartBean();
在doPost方法中,我有以下内容,如果用户尝试向购物车添加内容,他应该能够在从doGet返回的搜索结果JSP视图中选择一些复选框时这样做
} else if (action.equals("add")) {
String selected[] = request.getParameterValues("checkbox-anything-songs");
// iterate through the songs selected in results.jsp for matching songs in order to add to cart
for (int i = 0; i < selected.length; i++) {
System.out.println("You selected song with songID: " + selected[i]);
// iterate through SongBeans in global variable, songs
for (SongBean song : songs) {
// selected song is added to cart using songID to match with SongBean's songID & break out of for loop
if (song.getSongID().matches(selected[i])) {
cart.addSong(song);
break;
}
}
}
ArrayList<SongBean> songsInCart = cart.getSongs();
System.out.println("You added " + selected.length + " songs to your cart and have a total of " + cart.getSongs().size() + " songs in your cart now!");
System.out.println("Songs in cart now: " + songsInCart);
request.setAttribute("songsCart", songsInCart);
RequestDispatcher requestdispatcher = request.getRequestDispatcher("/cart.jsp");
requestdispatcher.forward(request, response);
我试图将cart或songsInCart的对象带到cart.jsp视图,以便我可以在其中显示已保存的数据。我知道正确的数据保存在对象中,就像我在控制台打印中看到的那样,并知道它正在保存正确的数据。问题是,当我在将请求发送到那里并使用getAttribute获取songsCart对象后尝试在cart.jsp视图中显示它时,它一直给我以下错误:
HTTP Status 500 - An exception occurred processing JSP page /cart.jsp at line 182
type Exception report
message An exception occurred processing JSP page /cart.jsp at line 182
description The server encountered an internal error that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: An exception occurred processing JSP page /cart.jsp at line 182
179:
180: <!-- Message about whether the shopping cart is empty or not -->
181: <c:choose>
182: <c:when test="<%= songbean.size() == 0 && albumbean.size() == 0 %>">
183: <h2><small>Your shopping cart is empty.</small></h2>
184: </c:when>
185: <c:when test="<%= songbean.size() >= 1 || albumbean.size() >= 1 %>">
我怀疑它与cart.jsp顶部的代码有关,如下所示,我收到一条警告信息&#34;类型安全:未选中从Object转换为ArrayList&#34;对于该行:&#34; ArrayList songbean =(ArrayList)session.getAttribute(&#34; songsCart&#34;);&#34;:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="edu.unsw.comp9321.*, java.util.*" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
// initialize session
// get the beans from importing edu.unsw.comp9321.*
CartBean cartBean;
SongBean songBean;
AlbumBean albumBean;
// check the session status to see if session has been initialized and shopping cart activated
if (session.getAttribute("shopping") == null) {
cartBean = new CartBean();
songBean = new SongBean();
albumBean = new AlbumBean();
session.setAttribute("shopping", cartBean);
session.setAttribute("songBean", songBean);
session.setAttribute("albumBean", albumBean);
System.out.println("cart.jsp: Creating New Session.");
} else {
System.out.println("cart.jsp: Already In Session.");
}
cartBean = (CartBean) session.getAttribute("shopping");
// get SongBeans and AlbumBeans so can fetch CartBean attributes from them
ArrayList<SongBean> songbean = (ArrayList<SongBean>) session.getAttribute("songsCart");
ArrayList<AlbumBean> albumbean = cartBean.getAlbums();
%>
我不知道出了什么问题,我怀疑它与songsCart对象没有被正确转换为包含所有歌曲的ArrayList有关。无论是那个还是与c标签有关,或者没有正确地执行getAttributes。有谁知道出了什么问题?