我正在尝试使用类配置而不使用web.xml来设置一个简单的spring mvc应用程序。 出于某种原因,在组件扫描过程中似乎没有正确扫描我的控制器。
我的项目如下:
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";
}
}
据说'localhost:8080 / project / greeting'应该提供'hello.jsp',但是它显示URI没有metching映射:
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
知道我做错了什么吗?
[更新] 添加调试:
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
答案 0 :(得分:2)
将以下内容添加到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;
}
这将允许将JSP映射为JSP,而不是期望具有这种映射的控制器
更改WebAppInitializer中的以下行
dispatcher.addMapping("/*"); //URL mapping
到
dispatcher.addMapping("/"); //URL mapping
这将使这个servlet成为默认映射,而不是将其用于所有内容。
将SimpleController更改为返回"hello"
而不是"hello.jsp"
,以便ViewResolver创建正确的路径。
确保将maven资源javax.servlet:jstl:1.2打包到类中或安装到Tomcat中,因为它不是标准的。
通过这些更改,它可以在我的环境中使用
BTW:我使用了spring-webmvc-4.1.6.RELEASE 我也用spring-webmvc-3.2.13.RELEASE
运行它我在这里添加了我的Pom,其中包含运行代码所需的最少依赖项。
<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>
答案 1 :(得分:0)
请将@EnableAutoConfiguration添加到您的控制器类。
答案 2 :(得分:0)
@RequestMapping(value = "/greeting")
public String test(Model model){
model.addAttribute("greeting", "Hello World");
return "hello.jsp";
}
我在您的代码中可以看到,您已使用&#34; / greeting&#34;添加了请求映射,因此网址应为: 主机名:端口/问候语
如果您使用嵌入式服务器或IDE(如IntelliJ IDEA)在本地计算机上运行,请尝试:
http://localhost:8080/greeting
如果您正在运行不同的服务器,比如tomcat,将您的战争重命名为ROOT.war,并将其设置为webapps文件夹,并且以上网址将会正常运行。
如果您没有重命名,并保留project.war,那么您将可以通过
访问答案 3 :(得分:0)
确保你的控制器类在org.webapp.mvc.controllers包中,从日志开始,看起来扫描没有提升类和它的URL映射