Reputation: 1014
I want to hit the readTasks
method of my controller when navigating to the root of my web app. I have set a breakpoint in the method but I am not hitting it when debugging on the server. I am using Eclipse.
I nagivate to: http://localhost:8080/ToDoList/ and I see my index page but the controller method is not invoked.
My Controller:
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();
}
}
My 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>
I believe I am missing some configuration to initialise the controller, but I am not sure how to go about it. Do I need a configuration file that contains initialisation of every single controller in my application?
Upvotes: 1
Views: 1844
Reputation: 550
You need to made a Spring MVC configuration, in your 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>
Then make mvc-dispatcher-servlet.xml
under WEB-INF folder.
<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>
Upvotes: 1
Reputation: 9622
You'll need to write a Spring configuration file which includes a component scan for the package that your controller is located in. This tells Spring to initialise this controller when the Spring context loads. You then need to point your servlet to this configuration:
<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>
Upvotes: 1