Spring MVC - 控制器方法没有被调用

时间:2012-12-26 18:13:07

标签: spring-mvc configuration controller

Spring3 MVC - 调用Controller类而不是控制器方法。

控制器:

package com.quizat.controller;

@Controller
public class QuizatController {
    public QuizatController(){
        System.out.println("in controller");
    }
    @RequestMapping("/")    
    @ResponseBody
    public String home(Model model) throws Exception {
        System.out.println("home is running");
        return "success";
    }
}

我的web.xml

<display-name>Spring3Example</display-name>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
  </init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>

我的调度程序-servlet.xml

<mvc:annotation-driven />
<context:annotation-config />
<context:component-scan base-package="com.quizat" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass">
  <value>org.springframework.web.servlet.view.JstlView</value>
  </property>
<property name="prefix">
<value>/view/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>

我的控制器的构造函数运行。但我在Controller中的方法没有被调用。我可能犯的错误是什么?

1 个答案:

答案 0 :(得分:2)

您正在将 / 映射到控制器的方法。而不是你应该映射一个字符串,如 / home / *

@RequestMapping("/home") 
@ResponseBody
public String home(Model model) throws Exception {
} 

同样在Controller中,您可以@RequestMapping(value = "/*")方法使用home()

注意:由于SPR-7064

,您还应该使用Spring&gt; = v3.0.3

参考:How do I map Spring MVC controller to a uri with and without trailing slash?