当我删除行" ModelAttribute(" OrderDetail")List OrderDetail"不再显示任何错误。但是如果我把这行" List OrderDetail = new ArrayList();"当我再次加载它将丢失列表中的数据。有人可以帮帮我吗?
Cartcontroller.java
@RequestMapping(value = {"addcart.html"})
public String addCart(@RequestParam(value = "albumId") int albumId,
@ModelAttribute("orderDetail") ModelMap model, @ModelAttribute("orderDetail") List<OrderDetail> orderDetail ) {
if (orderDetail.size() == 0) {
Album album = albumService.findAlbum(albumId);
OrderDetail ordetail = new OrderDetail(album, 1);
orderDetail.add(ordetail);
} else {
boolean isDup = false;
for (int i = 0; i < orderDetail.size(); i++) {
if (orderDetail.get(i).getAlbum().getAlbumId() == albumId) {
int quantity = orderDetail.get(i).getQuantity();
orderDetail.get(i).setQuantity(quantity + 1);
isDup = true;
break;
}
}
if (isDup == false) {
Album album = albumService.findAlbum(albumId);
OrderDetail ordetail = new OrderDetail(album, 1);
orderDetail.add(ordetail);
}
}
List<Album> albumList = albumService.getAllAlbums();
model.addAttribute("albumList", albumList);
model.addAttribute("sumTypeOrder", orderDetail.size());
return "home";
}
针对home.jsp
<sec:authorize access="hasRole('User')">
<div id="logtab">
<spring:message code="user.logged" />
:
<sec:authentication property="name" />
</div>
</sec:authorize>
<div id="logtab">
<c:if test="${sumTypeOrder}>0">
<a href="cart">Your Cart("${sumTypeOrder}")</a>
</c:if>
</div>
</div>
<div class="left"></div>
<div class="content">
<c:forEach items="${albumList}" var="album">
<div class="album">
<td align="center">${album.name}</td>
<td align="center">${album.singer}</td>
<td align="center">${album.releaseDate}</td>
<td><a href="addcart.html?albumId=${album.albumId}">Add
to cart</a></td>
</div>
</c:forEach>
</div>
<div class="right"></div>
<div class="footer"></div>
</div>
</div>
loginController.java
@RequestMapping(value = { "/home", "/" }, method = RequestMethod.GET)
public String welcomePage(Model model, Map<String, Object> map) {
model.addAttribute("user", new User());
model.addAttribute("album", new Album());
Album album = new Album();
map.put("album", album);
map.put("albumList", albumService.getAllAlbums());
return "home";
}
异常:
SEVERE:servlet [spring]的Servlet.service()与路径的上下文 [/ MusicStore]抛出异常[请求处理失败;嵌套 异常是java.lang.IllegalArgumentException:参数类型 不匹配]与根本原因java.lang.IllegalArgumentException:参数 sun.reflect.NativeMethodAccessorImpl.invoke0(Native。)中的类型不匹配 方法)at sun.reflect.NativeMethodAccessorImpl.invoke(未知 来源)at sun.reflect.DelegatingMethodAccessorImpl.invoke(未知 来自)java.lang.reflect.Method.invoke(未知来源)at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176) 在 org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:436) 在 org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424) 在 org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:900) 在 org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:827) 在 org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882) 在 org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778) 在javax.servlet.http.HttpServlet.service(HttpServlet.java:620)at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) 在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) 在 org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 在 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) 在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) 在 org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:330) 在 org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118) 在 org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84) 在 org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342) 在 org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113) 在 org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342) 在 org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103) 在 org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342) 在 org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113) 在 org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342) 在 org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54) 在 org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342) 在 org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45) 在 org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342) 在 org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:183) 在 org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342) 在 org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105) 在 org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342) 在 org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87) 在 org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342) 在 org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192) 在 org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160) 在 org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346) 在 org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259) 在 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) 在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) 在 org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220) 在 org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) 在 org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504) 在 org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170) 在 org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) 在 org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950) 在 org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) 在 org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421) 在 org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1074) 在 org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:611) 在 org.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:316) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor $ Worker.run(Unknown Source) 在 org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61) 在java.lang.Thread.run(未知来源)