java.lang.ClassCastException:无法将Model.Film强制转换为java.util.List

时间:2019-12-25 19:17:39

标签: java jsp

我无法在桌子上打印电影清单。我尝试了多种方法,但它们似乎没有用。似乎我无法设法将对象转换为列表,然后可以通过jsp文件对其进行访问。目前,它不能显示另一个错误,该错误表示javax.servlet.ServletException:javax.servlet.jsp.JspTagException:不知道如何遍历中提供的“项目”。任何帮助将不胜感激。 非常感谢。

JSP FILE

<%@ page import = "Model.Film"%>
<%@ page import = "java.util.ArrayList"%>

<html>
<head>
<link rel="stylesheet" 
href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" 
integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" 
crossorigin="anonymous">
<meta charset="UTF-8">
<title>Films</title>
<meta <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> >

</head>
<body>

    <table class="table">
        <thead class="thead-dark">
        <tr>
            <th scope="col">Film Id</th>
            <th scope="col">Film Name</th>
            <th scope="col">Film Year</th>
            <th scope="col">Film Duration</th>
            <th scope="col">Film Credits</th>
            <th scope="col">Film Reviews</th>
        </tr>

        <c:forEach items="${getFilm}" var="film">
            <tr>
                <td>${film.getFilmID()}</td>
                <td>${film.getFilmName()}</td>
                <td>${film.getFilmYear()}</td>
                <td>${film.getFilmDuration()}</td>
                <td>${film.getFilmCredits()}</td>
                <td>${film.getFilmReview()}</td>
            </tr>
        </c:forEach>
    </table>
</body>
</html>

2. Servlet
    package Servlets;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Model.Film;
import Model.FilmDAO;


@WebServlet("/getFilm")
public class getFilm extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws 
ServletException, IOException {

        //SEARCH FILM BY Name - Console
        FilmDAO dao = new FilmDAO();

        String search = "Dabangg 3";
        Film searchConsole = dao.getFilmByName(search);
        System.out.println("Film Name, Method: Console: " + searchConsole);

        //SEARCH FILM BY Name - URL
        String searchResult = request.getParameter("filmName");
//      Film searchURL = dao.getFilmByName(searchResult);

        List<Film> searchURL = (List<Film>) dao.getFilmByName(searchResult);


        System.out.println("Film Name, Method: URL: " + searchURL);



        request.setAttribute("getFilm", searchURL);
        String type = request.getParameter("type");
        String outputPage;


        if("xml".equals(type)) {
            response.setContentType("text/xml");
            outputPage = "/WEB-INF/results/filmsXml.jsp";
        }
        else if("json".equals(type)) {
            response.setContentType("text/json");
            outputPage = "/WEB-INF/results/filmsJson.jsp";
        }
        else {
            response.setContentType("text/html");
            outputPage = "/WEB-INF/results/text.jsp";
        }
        RequestDispatcher rd = request.getRequestDispatcher(outputPage);
        rd.include(request, response);

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws 
ServletException, IOException {
        doGet(request, response);
    }

}

3 个答案:

答案 0 :(得分:1)

如果您真的想要电影在列表中,则需要创建一个列表,然后将电影添加到列表中。

// Choose the kind of List implementation you want
List<Film> searchURL = new ArrayList<Film>();
// Add the film to the list
searchURL.add(dao.getFilmByName(searchResult));

答案 1 :(得分:0)

A。。确保将df.assign(grp = df['class_size'].where(df['class_size'].le(5),'>5')) 添加到jstl-1.2.jar

B。。下面给出的是一个工作示例,成功部署后,您可以使用http://localhost:8080/TestDynamicProject/getFilm进行访问

Film.java

lib

FilmDAO.java

package models;

import java.util.Arrays;

public class Film {
    private String name;
    private int releaseYear;
    private String actors[];    

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getReleaseYear() {
        return releaseYear;
    }
    public void setReleaseYear(int releaseYear) {
        this.releaseYear = releaseYear;
    }
    public String[] getActors() {
        return actors;
    }
    public void setActors(String[] actors) {
        this.actors = actors;
    }
    public Film(String name, int releaseYear, String[] actors) {
        super();
        this.name = name;
        this.releaseYear = releaseYear;
        this.actors = actors;
    }
    @Override
    public String toString() {
        return "Film [name=" + name + ", releaseYear=" + releaseYear + ", actors=" + Arrays.toString(actors) + "]";
    }   
}

FilmsListServlet.java

package dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import models.Film;

public class FilmDAO {
    //Creating a HashMap to represent a database
    static Map<String,Film> filmMap=new HashMap<String,Film>();
    static {
        filmMap.put("Dabangg 3",new Film("Dabangg 3", 2019, new String[] {"Salman Khan","Sonakshi Sinha","Saiee Manjrekar"}));
        filmMap.put("Dabangg 2",new Film("Dabangg 2", 2012, new String[] {"Salman Khan","Sonakshi Sinha","Vinod Khanna"}));
        filmMap.put("Dabangg 1",new Film("Dabangg", 2010, new String[] {"Salman Khan","Sonakshi Sinha","Sonu Sood"}));
        filmMap.put("Deewar",new Film("Deewar", 1975, new String[] {"Amitabh Bachchan","Shashi Kapoor"}));
        filmMap.put("Sharaabi",new Film("Sharaabi", 1984, new String[] {"Amitabh Bachchan","Jaya Prada"}));
    }

    /**
     * Returns all films whose name contain the parameter
     * @param name
     * @return List<Film>
     */
    public List<Film> getFilmByName(String name){
        List<Film> list=new ArrayList<Film>();
        for (Map.Entry<String, Film> entry : filmMap.entrySet()) {
            if((entry.getKey().contains(name)))
                list.add(entry.getValue());
        }
        return list;
    }
}

text.jsp

package servlets;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.FilmDAO;
import models.Film;

@WebServlet("/getFilm")
public class FilmsListServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        FilmDAO dao = new FilmDAO();

        String search = "Dabangg";
        List<Film> filmsList = (List<Film>) dao.getFilmByName(search);
        //System.out.println(filmsList);
        request.setAttribute("films", filmsList);
        String type = request.getParameter("type");
        String outputPage;

        if ("xml".equals(type)) {
            response.setContentType("text/xml");
            outputPage = "/WEB-INF/results/filmsXml.jsp";
        } else if ("json".equals(type)) {
            response.setContentType("text/json");
            outputPage = "/WEB-INF/results/filmsJson.jsp";
        } else {
            response.setContentType("text/html");
            outputPage = "/WEB-INF/results/text.jsp";
        }
        RequestDispatcher rd = request.getRequestDispatcher(outputPage);
        rd.include(request, response);

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }    
}

enter image description here

答案 2 :(得分:0)

// Choose the kind of List implementation you want
List<Film> searchURL = new ArrayList<Film>();
// Add the film to the list
searchURL.add(dao.getFilmByName(searchResult));

这对我来说很有效。谢谢orSt。他帮助了我。