在Springmvc中配置注释

时间:2012-12-02 21:37:12

标签: java spring-mvc annotations

运行springmvc应用程序时出现以下错误。在我的控制器文件中,我使用了注释将viewAll.view映射到displagy inventory / ListAll.jsp,但这似乎不起作用。 我可以在控制台中使用控制器文件中的sysout看到消息“在ViewDVDController.viewAll()中”。

如果我错过了一些映射,请你告诉我吗?

Error:

HTTP Status 404 - /SpringMVC/views/inventory/viewAll.jsp

--------------------------------------------------------------------------------

type Status report

message /SpringMVC/views/inventory/viewAll.jsp

description The requested resource (/SpringMVC/views/inventory/viewAll.jsp) is not available.


--------------------------------------------------------------------------------

Apache Tomcat/7.0.29

的index.html

  <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Highview DVD Management</title>
    </head>
    <body>
     <p> This DVD Management Model contains the following modules</p>

     <ul>
      <li><a href="inventory/viewAll.view">View All DVDs</a> </li>
      <li><a href="inventory/addDVD.form">Add a DVD</a> </li>
     </ul>
    </body>
  </html>

ViewDVDController.java

    @Controller
    public class ViewDVDController extends MultiActionController {
        public ViewDVDController(){
            System.out.println("In ViewDVDController()");
        }
        @Autowired
        private InventoryManager manager;

        public InventoryManager getManager(){
            return manager;
        }

        public void setManager(InventoryManager manager){
            this.manager=manager;
        }

        @RequestMapping(value="viewAll.view", method=RequestMethod.GET)
        public ModelAndView viewAll() throws Exception {
            System.out.println("In ViewDVDController.viewAll()");
            Collection<DVDInfo> all = manager.getAll();
            return new ModelAndView("ListAll","catalog",all);

        }


    }

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
  <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xmlns="http://java.sun.com/xml/ns/javaee" 
       xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
    <display-name>Spring MVCDemo</display-name>

    <servlet>
      <servlet-name>spring</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet>

    <servlet-mapping>
      <servlet-name>spring</servlet-name>
      <url-pattern>/inventory/*</url-pattern>
    </servlet-mapping>


    <welcome-file-list>
      <welcome-file>index.html</welcome-file>
    </welcome-file-list>

   </web-app>

spring-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
   xmlns:cotext="http://www.springframework.org/schema/context"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
             http://www.springframework.org/schema/context
             http://www.springframework.org/schema/context/spring-context-3.0.xsd
             http://www.springframework.org/schema/mvc
             http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

   <mvc:annotation-driven/>
   <cotext:component-scan base-package="springmvc"/>     

   <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/views/inventory/"/>
    <property name="suffix" value=".jsp" />
   </bean>

   </beans>

log4j日志:

    22:31:38,370 DEBUG ctory.support.DefaultListableBeanFactory: 430 - Creating instance of bean 'org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator'
    22:31:38,372 DEBUG ctory.support.DefaultListableBeanFactory: 458 - Finished creating instance of bean 'org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator'
    22:31:38,372 DEBUG gframework.web.servlet.DispatcherServlet: 647 - Unable to locate RequestToViewNameTranslator with name 'viewNameTranslator': using default [org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator@1a0b94d]
    22:31:38,372 DEBUG ctory.support.DefaultListableBeanFactory: 245 - Returning cached instance of singleton bean 'viewResolver'
    22:31:38,373 DEBUG ctory.support.DefaultListableBeanFactory: 430 - Creating instance of bean 'org.springframework.web.servlet.support.SessionFlashMapManager'
    22:31:38,376 DEBUG ctory.support.DefaultListableBeanFactory: 458 - Finished creating instance of bean 'org.springframework.web.servlet.support.SessionFlashMapManager'
    22:31:38,377 DEBUG gframework.web.servlet.DispatcherServlet: 709 - Unable to locate FlashMapManager with name 'flashMapManager': using default [org.springframework.web.servlet.support.SessionFlashMapManager@16c14c0]
    22:31:38,377 DEBUG gframework.web.servlet.DispatcherServlet: 523 - Published WebApplicationContext of servlet 'spring' as ServletContext attribute with name [org.springframework.web.servlet.FrameworkServlet.CONTEXT.spring]
    22:31:38,377  INFO gframework.web.servlet.DispatcherServlet: 463 - FrameworkServlet 'spring': initialization completed in 804 ms
    22:31:38,377 DEBUG gframework.web.servlet.DispatcherServlet: 136 - Servlet 'spring' configured successfully
    22:31:38,383 DEBUG gframework.web.servlet.DispatcherServlet: 819 - DispatcherServlet with name 'spring' processing GET request for [/SpringMVC/inventory/viewAll.view]
    22:31:38,385 DEBUG .annotation.RequestMappingHandlerMapping: 213 - Looking up handler method for path /viewAll.view
    22:31:38,387 DEBUG .annotation.RequestMappingHandlerMapping: 220 - Returning handler method [public org.springframework.web.portlet.ModelAndView springmvc.presentation.ViewDVDController.viewAll() throws java.lang.Exception]
    22:31:38,388 DEBUG ctory.support.DefaultListableBeanFactory: 245 - Returning cached instance of singleton bean 'viewDVDController'
    22:31:38,390 DEBUG gframework.web.servlet.DispatcherServlet: 902 - Last-Modified value for [/SpringMVC/inventory/viewAll.view] is: -1
    In ViewDVDController.viewAll()
    siz of the collection is  2
    22:31:38,413 DEBUG ctory.support.DefaultListableBeanFactory:1498 - Invoking afterPropertiesSet() on bean with name 'viewAll'
    22:31:38,413 DEBUG gframework.web.servlet.DispatcherServlet:1178 - Rendering view [org.springframework.web.servlet.view.JstlView: name 'viewAll'; URL [/views/inventory/viewAll.jsp]] in DispatcherServlet with name 'spring'
    22:31:38,413 DEBUG pringframework.web.servlet.view.JstlView: 371 - Added model object 'modelAndView' of type [org.springframework.web.portlet.ModelAndView] to request in view with name 'viewAll'
    22:31:38,413 DEBUG pringframework.web.servlet.view.JstlView: 371 - Added model object 'org.springframework.validation.BindingResult.modelAndView' of type [org.springframework.validation.BeanPropertyBindingResult] to request in view with name 'viewAll'
    22:31:38,417 DEBUG pringframework.web.servlet.view.JstlView: 236 - Forwarding to resource [/views/inventory/viewAll.jsp] in InternalResourceView 'viewAll'
    22:31:38,420 DEBUG gframework.web.servlet.DispatcherServlet: 913 - Successfully completed request

2 个答案:

答案 0 :(得分:1)

最后,我得到了答案。 我导入了错误的ModelAndView类  (org.springframework.web * .portlet。 * ModelAndView)而不是org.springframework.web。* servlet。 * ModelAndView在控制器文件中。

答案 1 :(得分:0)

View Controller在哪个包中?请注意 base-package (在上下文中:component-scan)的值必须与您的包名匹配。