如何在 Spring MVC 中调用控制器方法
How to invoke controller methods in Spring MVC
我想在导航到我的 Web 应用程序的根目录时点击我的控制器的 readTasks
方法。我在方法中设置了一个断点,但在服务器上调试时我没有击中它。我正在使用 Eclipse。
我导航到:http://localhost:8080/ToDoList/ 并且我看到了我的索引页面,但未调用控制器方法。
我的控制器:
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TaskController {
@RequestMapping(value = "/", method = RequestMethod.GET)
public List<TaskEntity> readTasks()
{
TaskEntityDao tasks = new TaskEntityDaoImpl();
return tasks.getAllTasks();
}
}
我的web.xml:
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
我想我缺少一些初始化控制器的配置,但我不确定如何去做。我是否需要包含应用程序中每个控制器初始化的配置文件?
您需要编写一个 Spring 配置文件,其中包括对您的控制器所在包的组件扫描。这告诉 Spring 在 [=13] 时初始化此控制器=] 上下文加载。然后您需要将您的 servlet 指向此配置:
<servlet>
<servlet-name>Dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/dispatcher-config.xml</param-value>
</init-param>
</servlet>
您需要在 web.xml
:
中进行 Spring MVC 配置
<servlet>
<servlet-name>Dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/mvc-dispatcher-servlet.xml</param-value>
</init-param>
</servlet>
然后在WEB-INF文件夹下制作mvc-dispatcher-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd ">
<!-- enable to scan spring annotations, specify on web package -->
<context:component-scan base-package="id.swhp.spring.web"/>
<!-- Enable Spring MVC Anotations -->
<mvc:annotation-driven/>
</beans>
我想在导航到我的 Web 应用程序的根目录时点击我的控制器的 readTasks
方法。我在方法中设置了一个断点,但在服务器上调试时我没有击中它。我正在使用 Eclipse。
我导航到:http://localhost:8080/ToDoList/ 并且我看到了我的索引页面,但未调用控制器方法。
我的控制器:
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TaskController {
@RequestMapping(value = "/", method = RequestMethod.GET)
public List<TaskEntity> readTasks()
{
TaskEntityDao tasks = new TaskEntityDaoImpl();
return tasks.getAllTasks();
}
}
我的web.xml:
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
我想我缺少一些初始化控制器的配置,但我不确定如何去做。我是否需要包含应用程序中每个控制器初始化的配置文件?
您需要编写一个 Spring 配置文件,其中包括对您的控制器所在包的组件扫描。这告诉 Spring 在 [=13] 时初始化此控制器=] 上下文加载。然后您需要将您的 servlet 指向此配置:
<servlet>
<servlet-name>Dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/dispatcher-config.xml</param-value>
</init-param>
</servlet>
您需要在 web.xml
:
<servlet>
<servlet-name>Dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/mvc-dispatcher-servlet.xml</param-value>
</init-param>
</servlet>
然后在WEB-INF文件夹下制作mvc-dispatcher-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd ">
<!-- enable to scan spring annotations, specify on web package -->
<context:component-scan base-package="id.swhp.spring.web"/>
<!-- Enable Spring MVC Anotations -->
<mvc:annotation-driven/>
</beans>