我试图使用Spring Boot构建一个非常简单的Spring MVC应用程序。到现在为止,我所有的尝试都失败了这是移民:
WebConfig:
@SpringBootApplication
@ComponentScan("newTestProject")
public class WebConfig extends WebMvcConfigurerAdapter {
public static void main(String[] args) throws Exception {
SpringApplication.run(WebConfig.class, args);
}
@Bean
public ViewResolver jspViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
resolver.setExposeContextBeansAsAttributes(true);
return resolver;
}
}
WebAppInitializer:
@Configuration
public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[]{};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[]{WebConfig.class};
}
}
控制器:
@Controller
public class IndexController {
@RequestMapping(value = "/")
public String index() {
return "index";
}
}
POM:
<?xml version="1.0" encoding="UTF-8"?>
<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>groupId</groupId>
<artifactId>messagesAppHiber</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<packaging>war</packaging>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
控制台中没有错误消息:
2017-03-15 21:25:49.631 INFO 1192 --- [ main] newTestProject.config.WebConfig : Starting WebConfig on Rafale-2-ПК with PID 1192 (D:\TEMP\untitled\target\classes started by Rafale-2 in D:\TEMP\untitled)
2017-03-15 21:25:49.633 INFO 1192 --- [ main] newTestProject.config.WebConfig : No active profile set, falling back to default profiles: default
2017-03-15 21:25:49.702 INFO 1192 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@1188e820: startup date [Wed Mar 15 21:25:49 EET 2017]; root of context hierarchy
2017-03-15 21:25:51.122 INFO 1192 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-03-15 21:25:51.136 INFO 1192 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2017-03-15 21:25:51.137 INFO 1192 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.11
2017-03-15 21:25:51.248 INFO 1192 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2017-03-15 21:25:51.249 INFO 1192 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1549 ms
2017-03-15 21:25:51.393 INFO 1192 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2017-03-15 21:25:51.399 INFO 1192 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-03-15 21:25:51.399 INFO 1192 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-03-15 21:25:51.399 INFO 1192 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-03-15 21:25:51.399 INFO 1192 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2017-03-15 21:25:51.661 INFO 1192 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@1188e820: startup date [Wed Mar 15 21:25:49 EET 2017]; root of context hierarchy
2017-03-15 21:25:51.714 INFO 1192 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto public java.lang.String newTestProject.controllers.IndexController.index()
2017-03-15 21:25:51.717 INFO 1192 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2017-03-15 21:25:51.717 INFO 1192 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2017-03-15 21:25:51.741 INFO 1192 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-03-15 21:25:51.741 INFO 1192 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-03-15 21:25:51.769 INFO 1192 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-03-15 21:25:51.941 INFO 1192 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-03-15 21:25:51.989 INFO 1192 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-03-15 21:25:51.992 INFO 1192 --- [ main] newTestProject.config.WebConfig : Started WebConfig in 2.634 seconds (JVM running for 2.935)
2017-03-15 21:25:57.507 INFO 1192 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2017-03-15 21:25:57.507 INFO 1192 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2017-03-15 21:25:57.519 INFO 1192 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 12 ms
结果应该是我的浏览器中显示的index.jsp,而是我得到Whitelabel错误页面。这个配置有什么问题?
答案 0 :(得分:1)
1)删除WebAppInitializer
类 - 因为spring boot自动配置它所以不需要它。
2)删除web.xml
。我不知道你有什么,但由于你正在使用注释配置,所以不需要它。
3)通过覆盖configureDefaultServletHandling
中的WebConfig
来启用默认servlet。
@Override
public void configureDefaultServletHandling(
DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
4)添加以下maven依赖项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
5)使用maven运行项目
$ mvn clean spring-boot:run
PS:我不知道你的jsp文件中有什么。但是如果你使用普通的html,这种方法肯定会起作用。如果您正在使用某些jstl
内容,则必须添加jstl依赖项
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>