Spring controller not recognized

I am trying to set up a simple spring mvc application using class config and no web.xml. For some reason, it seems that my controller is not being checked properly during component scan.

My project looks like this:

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";
}

}

      

Presumably "localhost: 8080 / project / greeting" should be "hello.jsp", but it shows that there is no label 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 I am doing wrong?

[update] Add 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

      

+3


source to share


4 answers


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 JSP to be rendered as a JSP instead of waiting for a controller with a rendering like that

Change the following line in the 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 the SimpleController to return "hello"

instead "hello.jsp"

so that the ViewResolver generates the correct path.

Make sure the maven resource javax.servlet: jstl: 1.2 is packaged in a class or installed in Tomcat as it is not standard.

With these changes, it works in my environment

BTW: I was using spring-webmvc-4.1.6.RELEASE I also just started it with spring-webmvc-3.2.13.RELEASE

I added my Pom here with a minimal set of dependencies 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>

      

+2


source


Add @EnableAutoConfiguration to your controller class.



0


source


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

      

What I see in your code, you added the request mapping with "/ greeting", so the url should look like this: hostname: port / greeting

if you are running on a local machine with an embedded server or an IDE like IntelliJ IDEA try:

http: // localhost: 8080 / greeting

In case you are using a different server, say tomcat, rename your war as ROOT.war and put it in webapps folder and the above url will work.

If you don't rename it and save the project.war file, then you should be able to access

http: // localhost: 8080 / project / greeting

0


source


Make sure your controller class is in package org.webapp.mvc.controllers, from logs it doesn't look like crawling picks up the class and displays its url

0


source







All Articles