1

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?

petehallw
  • 1,014
  • 6
  • 21
  • 49

2 Answers2

1

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>
Plog
  • 9,164
  • 5
  • 41
  • 66
1

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>
Sukma Wardana
  • 540
  • 8
  • 21
  • Thanks for your answer - in web.xml it references 'dispatcher-config.xml' but then you say to make 'mvc-dispatcher-servlet.xml'. Should these not be named the same? I tried this with my package name in `base-package=[mypackage]` but it still does not initialise my controller. Does the controller need to be annotated with something? Thanks – petehallw Jul 07 '17 at 10:12
  • Whops I just copied from spring docs and not know it have different naming, but yeah it should have the same name (already edited my post) on your controller class give `@Controller` or @RestController` annotations. – Sukma Wardana Jul 07 '17 at 10:15
  • Thank you, good answer however I am still not hitting my controller. I specified the package in `base-package` that my controller is in, and my controller is annotated with `@RestController`. – petehallw Jul 07 '17 at 10:25
  • Did you already configure your `applicationContext.xml`? Maybe you could see my Spring project configuration, especially the `applicationContext.xml`, `mvc-dispatcher.xml` and `web.xml` on [my github](https://github.com/swhp/javaee-springframeworks/tree/master/src/main/webapp/WEB-INF) – Sukma Wardana Jul 07 '17 at 10:33
  • Thank you, I'm taking a look now :) – petehallw Jul 07 '17 at 10:45
  • I think I should do what I'd advise anyone else to do in my situation and read up on the fundamentals. I don't really understand what I'm doing with the configuration and just trying to hack it together! – petehallw Jul 07 '17 at 17:00