Spring MVC-未调用控制器方法 [英] Spring MVC - Controller method not getting called
问题描述
Spring3 MVC-调用了控制器类,但未调用控制器方法.
Spring3 MVC - Controller class is getting called but not the controller method.
控制器:
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>
我的dispatcher-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内部的方法没有被调用.我可能犯了什么错误?
Constructor of my controller runs. But my method inside the Controller is not getting called. What could be the error that I have made?
推荐答案
您正在将/映射到控制器的方法.相反,您应该映射诸如/home 或/* 之类的字符串.
You are mapping / to your controller's method. Instead of that you should map a string like /home or /*.
@RequestMapping("/home")
@ResponseBody
public String home(Model model) throws Exception {
}
还可以在Controller中将 @RequestMapping(value ="/*")
用于 home()
方法.
Also in the Controller you can use @RequestMapping(value = "/*")
for the home()
method.
注意:由于 SPR-7064 <,/a>
Note: you should also be using Spring >= v3.0.3 due to SPR-7064
参考: 查看全文