【发布时间】:2017-07-07 09:35:18
【问题描述】:
当导航到我的网络应用程序的根目录时,我想点击控制器的 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的配置文件吗?
-
我不知道 - 我意识到这是非常基本的东西,但我真的不明白如何编写它来初始化我的控制器......
-
在你的控制器中添加一个 postconstruct 方法并在其中放置一个断点来查看控制器是否正在初始化
-
尝试将
@RequestMapping("/ToDoList")添加到您的控制器
标签: java spring-mvc model-view-controller requestdispatcher