. lang。ClassCastException:. lang。字符串不能被转换为com.example.service.AlbumService

时间:2022-11-07 22:47:39

I using spring mvc and hibernate xml mapping. And I have error about in jsp:

我使用spring mvc和hibernate xml映射。我在jsp上有错误:

My jsp (storeManager.jsp):

我的jsp(storeManager.jsp):

<jsp:useBean id="albumService" type="com.example.service.AlbumService" scope="request" />

<table>
    <thead>
        <tr>
            <th>Genre</th>
            <th>Artist</th>
            <th>Title</th>
            <th>Price</th>
            <th></th>
            <th></th>
            <th></th>
        </tr>
    </thead>

    <tbody>
        <tr>
        <%for (Album myAlbum : albumService.getAllAlbums()) { %>
            <%int tempGenreId = myAlbum.getGenreId().getGenreId(); %>
            <%int tempArtistId = myAlbum.getArtistid().getArtistid(); %>
            <td><%=albumService.findGenreByIdFromAlbumRepository(tempGenreId).getName()%></td>
            <td><%=albumService.findArtistByIdFromAlbumRepository(tempArtistId).getName()%></td>
            <td><%=myAlbum.getTitle()%></td>
            <td><%=myAlbum.getPrice()%></td>
            <td><a href="EditAlbum?editAlbumId=<%=myAlbum.getAlbumId()%>">Edit</a></td>
            <td><a href="DetailsAlbum?detailsAlbumId=<%=myAlbum.getAlbumId()%>">Details</a></td>
            <td><a href="DeleteAlbum?deleteAlbumId=<%=myAlbum.getAlbumId()%>">Delete</a></td>
        </tr>

        <%}%>
    </tbody>
</table>

And my Controller(StoreManagerController.java)

我的控制器(StoreManagerController.java)

@Controller
@RequestMapping("/StoreManager")
public class StoreManagerController 
{
@Autowired
private AlbumService albumService;

@Autowired
private GenreService genreService;

@Autowired
private ArtistService artistService;

@RequestMapping(value="/", method = RequestMethod.GET)
public ModelAndView getStoreManagerPage(HttpServletRequest request, HttpSession session,ModelMap model) {

    logger.debug("JUST GOT STORE MANAGER PAGE");

    List<Album> albumList = albumService.getAllAlbums();
    model.put("albumList", albumList);

    return new ModelAndView("storeManager", "albumService" , "albumService");
}

When I deploy and run. it happent a error following:

当我部署和运行时。下面发生了一个错误:

org.apache.jasper.JasperException: An exception occurred processing JSP page /WEB-INF/jsp/storeManager.jsp at line 18

15: <%@page import="java.util.List" %>
16: <%@page import="java.util.Map" %>
17: 
18: <jsp:useBean id="albumService" type="com.example.service.AlbumService" scope="request" />
19: 
20: 
21: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">


Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1047)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:817)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
ch.qos.logback.classic.selector.servlet.LoggerContextFilter.doFilter(LoggerContextFilter.java:70)

root cause

java.lang.ClassCastException: java.lang.String cannot be cast to com.example.service.AlbumService
org.apache.jsp.WEB_002dINF.jsp.storeManager_jsp._jspService(storeManager_jsp.java:78)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1047)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:817)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
ch.qos.logback.classic.selector.servlet.LoggerContextFilter.doFilter(LoggerContextFilter.java:70)

How can I solve this error?

我怎样才能解决这个错误呢?

1 个解决方案

#1


2  

Looks like the third parameter where you're creating the ModelAndView object is incorrect. You're passing in a string, but the jsp:useBean tag is expecting it to be an AlbumService object.

看起来创建ModelAndView对象的第三个参数是不正确的。您正在传递一个字符串,但是jsp:useBean标记期望它是一个AlbumService对象。

Change

改变

return new ModelAndView("storeManager", "albumService" , "albumService");

to

return new ModelAndView("storeManager", "albumService" , albumService);

#1


2  

Looks like the third parameter where you're creating the ModelAndView object is incorrect. You're passing in a string, but the jsp:useBean tag is expecting it to be an AlbumService object.

看起来创建ModelAndView对象的第三个参数是不正确的。您正在传递一个字符串,但是jsp:useBean标记期望它是一个AlbumService对象。

Change

改变

return new ModelAndView("storeManager", "albumService" , "albumService");

to

return new ModelAndView("storeManager", "albumService" , albumService);