Anhermon
Anhermon

Reputation: 309

Spring controller not recognized

I'm trying to set up a simple spring mvc application using class configuration and without web.xml. For some reason it appears that my controller is not being properly scanned during the component scan.

My projet is as following:

src/main/java
 -org.webapp.mvc
   -config
     WebAppInitializer
     WebConfig
   -controllers
     SimpleController

src/main/webapp
   -hello.jsp
   -index.jsp

WebAppInitializer:

public class WebAppInitializer implements WebApplicationInitializer {

@Override
public void onStartup(ServletContext servletContext) throws ServletException     {
    WebApplicationContext context = getContext(); //spring configuration  context
    servletContext.addListener(new ContextLoaderListener(context));
    ServletRegistration.Dynamic dispatcher =  servletContext.addServlet("DispatcherServlet", new DispatcherServlet(context));
    dispatcher.setLoadOnStartup(1);
    dispatcher.addMapping("/*"); //URL mapping
}

private AnnotationConfigWebApplicationContext getContext() {
    AnnotationConfigWebApplicationContext context = new  AnnotationConfigWebApplicationContext();
    context.setConfigLocation("org.webapp.mvc.config.WebConfig");
    return context;
}

}

WebConfig:

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "org.webapp.mvc.controllers")
public class WebConfig {

}

SimpleController:

@Controller
public class SimpleController {

@RequestMapping(value = "/greeting")
public String test(Model model){
    model.addAttribute("greeting", "Hello World");
    return "hello.jsp";
}

}

Supposedly 'localhost:8080/project/greeting' should serve 'hello.jsp', but it shows that there's no metching mapping for the URI:

03:22:15.385 [http-nio-8080-exec-3] DEBUG o.s.web.servlet.DispatcherServlet -     DispatcherServlet with name 'DispatcherServlet' processing GET request for     [/project/greeting]
03:22:15.385 [http-nio-8080-exec-3] WARN  o.s.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/project/greeting] in DispatcherServlet with name 'DispatcherServlet'
03:22:15.385 [http-nio-8080-exec-3] DEBUG o.s.web.servlet.DispatcherServlet - Successfully completed request

Any idea what am I doing wrong?

[update] Adding debug:

    03:22:08.824 [localhost-startStop-1] INFO  o.s.web.servlet.DispatcherServlet - FrameworkServlet 'DispatcherServlet': initialization started
    03:22:08.828 [localhost-startStop-1] DEBUG o.s.web.servlet.DispatcherServlet - Unable to locate MultipartResolver with name 'multipartResolver': no multipart request handling provided
    03:22:08.829 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver'
    03:22:08.872 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver'
    03:22:08.872 [localhost-startStop-1] DEBUG o.s.web.servlet.DispatcherServlet - Unable to locate LocaleResolver with name 'localeResolver': using default [org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver@587b4b84]
    03:22:08.874 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'org.springframework.web.servlet.theme.FixedThemeResolver'
    03:22:08.880 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.web.servlet.theme.FixedThemeResolver'
    03:22:08.880 [localhost-startStop-1] DEBUG o.s.web.servlet.DispatcherServlet - Unable to locate ThemeResolver with name 'themeResolver': using default [org.springframework.web.servlet.theme.FixedThemeResolver@7e02662a]
    03:22:08.886 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping'
    03:22:08.906 [localhost-startStop-1] DEBUG o.s.w.s.h.BeanNameUrlHandlerMapping - Looking for URL mappings in application context: Root WebApplicationContext: startup date [Sat Apr 18 03:22:08 IDT 2015]; root of context hierarchy
    03:22:08.907 [localhost-startStop-1] DEBUG o.s.w.s.h.BeanNameUrlHandlerMapping - Rejected bean name 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor': no URL paths identified
    03:22:08.907 [localhost-startStop-1] DEBUG o.s.w.s.h.BeanNameUrlHandlerMapping - Rejected bean name 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor': no URL paths identified
    03:22:08.907 [localhost-startStop-1] DEBUG o.s.w.s.h.BeanNameUrlHandlerMapping - Rejected bean name 'org.springframework.context.annotation.internalRequiredAnnotationProcessor': no URL paths identified
    03:22:08.907 [localhost-startStop-1] DEBUG o.s.w.s.h.BeanNameUrlHandlerMapping - Rejected bean name 'org.springframework.context.annotation.internalCommonAnnotationProcessor': no URL paths identified
    03:22:08.907 [localhost-startStop-1] DEBUG o.s.w.s.h.BeanNameUrlHandlerMapping - Rejected bean name 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor': no URL paths identified
    03:22:08.907 [localhost-startStop-1] DEBUG o.s.w.s.h.BeanNameUrlHandlerMapping - Rejected bean name 'org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor': no URL paths identified
    03:22:08.907 [localhost-startStop-1] DEBUG o.s.w.s.h.BeanNameUrlHandlerMapping - Rejected bean name 'environment': no URL paths identified
    03:22:08.907 [localhost-startStop-1] DEBUG o.s.w.s.h.BeanNameUrlHandlerMapping - Rejected bean name 'systemProperties': no URL paths identified
    03:22:08.907 [localhost-startStop-1] DEBUG o.s.w.s.h.BeanNameUrlHandlerMapping - Rejected bean name 'systemEnvironment': no URL paths identified
    03:22:08.907 [localhost-startStop-1] DEBUG o.s.w.s.h.BeanNameUrlHandlerMapping - Rejected bean name 'servletContext': no URL paths identified
    03:22:08.907 [localhost-startStop-1] DEBUG o.s.w.s.h.BeanNameUrlHandlerMapping - Rejected bean name 'contextParameters': no URL paths identified
    03:22:08.907 [localhost-startStop-1] DEBUG o.s.w.s.h.BeanNameUrlHandlerMapping - Rejected bean name 'contextAttributes': no URL paths identified
    03:22:08.907 [localhost-startStop-1] DEBUG o.s.w.s.h.BeanNameUrlHandlerMapping - Rejected bean name 'messageSource': no URL paths identified
    03:22:08.907 [localhost-startStop-1] DEBUG o.s.w.s.h.BeanNameUrlHandlerMapping - Rejected bean name 'applicationEventMulticaster': no URL paths identified
    03:22:08.907 [localhost-startStop-1] DEBUG o.s.w.s.h.BeanNameUrlHandlerMapping - Rejected bean name 'lifecycleProcessor': no URL paths identified
    03:22:08.907 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping'
    03:22:08.908 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping'
    03:22:08.916 [localhost-startStop-1] DEBUG o.s.w.s.m.a.DefaultAnnotationHandlerMapping - Looking for URL mappings in application context: Root WebApplicationContext: startup date [Sat Apr 18 03:22:08 IDT 2015]; root of context hierarchy
    03:22:08.918 [localhost-startStop-1] DEBUG o.s.w.s.m.a.DefaultAnnotationHandlerMapping - Rejected bean name 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor': no URL paths identified
    03:22:08.918 [localhost-startStop-1] DEBUG o.s.w.s.m.a.DefaultAnnotationHandlerMapping - Rejected bean name 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor': no URL paths identified
    03:22:08.918 [localhost-startStop-1] DEBUG o.s.w.s.m.a.DefaultAnnotationHandlerMapping - Rejected bean name 'org.springframework.context.annotation.internalRequiredAnnotationProcessor': no URL paths identified
    03:22:08.918 [localhost-startStop-1] DEBUG o.s.w.s.m.a.DefaultAnnotationHandlerMapping - Rejected bean name 'org.springframework.context.annotation.internalCommonAnnotationProcessor': no URL paths identified
    03:22:08.918 [localhost-startStop-1] DEBUG o.s.w.s.m.a.DefaultAnnotationHandlerMapping - Rejected bean name 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor': no URL paths identified
    03:22:08.918 [localhost-startStop-1] DEBUG o.s.w.s.m.a.DefaultAnnotationHandlerMapping - Rejected bean name 'org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor': no URL paths identified
    03:22:08.918 [localhost-startStop-1] DEBUG o.s.w.s.m.a.DefaultAnnotationHandlerMapping - Rejected bean name 'environment': no URL paths identified
    03:22:08.918 [localhost-startStop-1] DEBUG o.s.w.s.m.a.DefaultAnnotationHandlerMapping - Rejected bean name 'systemProperties': no URL paths identified
    03:22:08.918 [localhost-startStop-1] DEBUG o.s.w.s.m.a.DefaultAnnotationHandlerMapping - Rejected bean name 'systemEnvironment': no URL paths identified
    03:22:08.918 [localhost-startStop-1] DEBUG o.s.w.s.m.a.DefaultAnnotationHandlerMapping - Rejected bean name 'servletContext': no URL paths identified
    03:22:08.918 [localhost-startStop-1] DEBUG o.s.w.s.m.a.DefaultAnnotationHandlerMapping - Rejected bean name 'contextParameters': no URL paths identified
    03:22:08.918 [localhost-startStop-1] DEBUG o.s.w.s.m.a.DefaultAnnotationHandlerMapping - Rejected bean name 'contextAttributes': no URL paths identified
    03:22:08.918 [localhost-startStop-1] DEBUG o.s.w.s.m.a.DefaultAnnotationHandlerMapping - Rejected bean name 'messageSource': no URL paths identified
    03:22:08.918 [localhost-startStop-1] DEBUG o.s.w.s.m.a.DefaultAnnotationHandlerMapping - Rejected bean name 'applicationEventMulticaster': no URL paths identified
    03:22:08.918 [localhost-startStop-1] DEBUG o.s.w.s.m.a.DefaultAnnotationHandlerMapping - Rejected bean name 'lifecycleProcessor': no URL paths identified
    03:22:08.918 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping'
    03:22:08.918 [localhost-startStop-1] DEBUG o.s.web.servlet.DispatcherServlet - No HandlerMappings found in servlet 'DispatcherServlet': using default
    03:22:08.921 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter'
    03:22:08.924 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter'
    03:22:08.926 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter'
    03:22:08.928 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter'
    03:22:08.930 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter'
    03:22:09.049 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter'
    03:22:09.049 [localhost-startStop-1] DEBUG o.s.web.servlet.DispatcherServlet - No HandlerAdapters found in servlet 'DispatcherServlet': using default
    03:22:09.053 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver'
    03:22:09.063 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver'
    03:22:09.064 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver'
    03:22:09.069 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver'
    03:22:09.070 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver'
    03:22:09.085 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver'
    03:22:09.085 [localhost-startStop-1] DEBUG o.s.web.servlet.DispatcherServlet - No HandlerExceptionResolvers found in servlet 'DispatcherServlet': using default
    03:22:09.088 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator'
    03:22:09.093 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator'
    03:22:09.093 [localhost-startStop-1] DEBUG o.s.web.servlet.DispatcherServlet - Unable to locate RequestToViewNameTranslator with name 'viewNameTranslator': using default [org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator@18abaed]
    03:22:09.096 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'org.springframework.web.servlet.view.InternalResourceViewResolver'
    03:22:09.114 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.web.servlet.view.InternalResourceViewResolver'
    03:22:09.114 [localhost-startStop-1] DEBUG o.s.web.servlet.DispatcherServlet - No ViewResolvers found in servlet 'DispatcherServlet': using default
    03:22:09.119 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'org.springframework.web.servlet.support.SessionFlashMapManager'
    03:22:09.128 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.web.servlet.support.SessionFlashMapManager'
    03:22:09.128 [localhost-startStop-1] DEBUG o.s.web.servlet.DispatcherServlet - Unable to locate FlashMapManager with name 'flashMapManager': using default [org.springframework.web.servlet.support.SessionFlashMapManager@726c87e9]
    03:22:09.128 [localhost-startStop-1] DEBUG o.s.web.servlet.DispatcherServlet - Published WebApplicationContext of servlet 'DispatcherServlet' as ServletContext attribute with name [org.springframework.web.servlet.FrameworkServlet.CONTEXT.DispatcherServlet]
    03:22:09.128 [localhost-startStop-1] INFO  o.s.web.servlet.DispatcherServlet - FrameworkServlet 'DispatcherServlet': initialization completed in 304 ms
    03:22:09.128 [localhost-startStop-1] DEBUG o.s.web.servlet.DispatcherServlet - Servlet 'DispatcherServlet' configured successfully

Upvotes: 3

Views: 3578

Answers (3)

redge
redge

Reputation: 1182

Add the following to WebConfig

    @Bean
    public UrlBasedViewResolver jspResolver(){
        System.out.println("in WebConfig jspResolver");
        UrlBasedViewResolver urlBasedViewResolver = new UrlBasedViewResolver();
        urlBasedViewResolver.setViewClass(JstlView.class);
        urlBasedViewResolver.setPrefix("/");
        urlBasedViewResolver.setSuffix(".jsp");
        return urlBasedViewResolver;
    }

this will allow the mapping of JSPs as JSPs instead of expecting a controller with such a mapping

Change the following line in WebAppInitializer

    dispatcher.addMapping("/*"); //URL mapping

to

    dispatcher.addMapping("/"); //URL mapping

This will make this servlet the default mapping instead of using it for everything.

Change SimpleController to return "hello" instead of "hello.jsp" so the ViewResolver will create the correct path.

Make sure that maven resource javax.servlet:jstl:1.2 is packaged into the class or installed into Tomcat as it doesn't come as standard.

With these changes it works in my environment

BTW : I used spring-webmvc-4.1.6.RELEASE I also just ran it with spring-webmvc-3.2.13.RELEASE

I have added my Pom here with the minimal set of dependencies I needed to run the code.

<project xmlns="http://maven.apache.org/POM/4.0.0" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>annotationConfig1</groupId>
    <artifactId>annotationConfig1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <warSourceDirectory>WebContent</warSourceDirectory>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>3.2.13.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>6.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

    </dependencies>
</project>

Upvotes: 2

Vinny
Vinny

Reputation: 797

Make sure that your controller class is in the org.webapp.mvc.controllers package, from the logs, it doesn't look like the scan is picking up the class and it's URL mapping

Upvotes: 0

A N M Bazlur Rahman
A N M Bazlur Rahman

Reputation: 2300

@RequestMapping(value = "/greeting")
public String test(Model model){
    model.addAttribute("greeting", "Hello World");
    return "hello.jsp";
}

What I can see in your code that, you have added request mapping with "/greeting", so the url should be like : hostname:port/greeting

if you are running in your local machine with embedded server or with the IDE e.g IntelliJ IDEA, try:

http://localhost:8080/greeting

in case, you're running different server, say tomcat, rename your war as a ROOT.war and put it webapps folder and above url will work.

If you don't rename it, and keep the project.war, then you will able to access by

http://localhost:8080/project/greeting

Upvotes: 0

Related Questions