2
votes

I'm trying to get a very simple Spring @mvc app to work and I'm running into what appears to be a mapping error.

From web.xml:

<servlet>
    <servlet-name>works</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>works</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

The controller:

@Controller
@RequestMapping("/test")
public class TESTController  {
    private static Logger appLogger = Logger.getLogger("AppLogFile");

    public String serviceRequest(   Model model)
    {
        appLogger.info("======================= TESTController GET ===============================");
        model.addAttribute("returnString","TESTController handled the request") ;
        return "SingleStringView"; 
    }

works-servlet.xml:

<context:component-scan base-package="com.ami.dbconnect.controller" />

    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>

    <bean id="TESTController" class="com.ami.dbconnect.controller.TESTController"></bean>

    <!-- view resolver not shown -->

The app is deployed to Tomcat 7 at /webapps/works. The Tomcat file structure is:

webapps
    /works
        /WEB-INF
            /classes
            /lib

I'm trying to invoke the controller with the url: localhost:8080/works/test

In tomcat7-stdout I see:

1106 [pool-2-thread-1] DEBUG org.springframework.web.servlet.DispatcherServlet - Published WebApplicationContext of servlet 'works' as ServletContext attribute with name [org.springframework.web.servlet.FrameworkServlet.CONTEXT.works]

1106 [pool-2-thread-1] INFO org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'works': initialization completed in 728 ms

1106 [pool-2-thread-1] DEBUG org.springframework.web.servlet.DispatcherServlet - Servlet 'works' configured successfully

14068 [http-apr-8080-exec-2] DEBUG org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name 'works' processing GET request for [/works/test]

14071 [http-apr-8080-exec-2] WARN org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/works/test] in DispatcherServlet with name 'works'

So my question (finally!): Is Spring not recognizing the annotations in the controller? If not, what could be wrong in this simple set up?

Thanks for any help or advice, beeky

2

2 Answers

2
votes

You need to move @RequestMapping annotation to your serviceRequest method.

@RequestMapping at class level may be used to specify common path prefix for all @RequestMapping-annotated of that class, but it doesn't take any effect without annotations at method level.

1
votes

Maybe problem is in the servlet mapping section? Try to change:

<servlet-mapping>
    <servlet-name>works</servlet-name>
    <url-pattern>/works/*</url-pattern>
</servlet-mapping>

And try again.