1、大体步骤
(1) 创建Maven web project;
(2) 在pom.xml文件添加依赖;
(3) 配置application.properties支持jsp
(4) 编写测试Controller
(5) 编写JSP页面
(6) 编写启动类App.java
2、新建SpringInitialzr
3、pom文件
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><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></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies>
4、application.properties文件
#页面默认前缀目录spring.mvc.view.prefix=/WEB-INF/jsp/#响应页面默认后缀spring.mvc.view.suffix=.jsp#自定义属性,可以在Controller中读取application.hello=HelloGOD
5、Controller文件
packagecom.example; importorg.springframework.beans.factory.annotation.Value; importorg.springframework.stereotype.Controller; importorg.springframework.web.bind.annotation.RequestMapping; importjava.util.Map; /*** Created by Gensis on 2016/9/9.*/publicclassHelloController { // 从 application.properties 中读取配置,如取不到默认值为Hello"${application.hello:Hello}") (privateStringhello; "/helloJsp") (publicStringhelloJsp(Map<String, Object>map) { System.out.println("HelloController.helloJsp().hello="+hello); map.put("hello", hello); return"helloJsp"; } }
6、jsp页面
<%pagelanguage="java"contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><!DOCTYPEhtmlPUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"><html><head><metahttp-equiv="Content-Type"content="text/html; charset=UTF-8"><title>GOD</title></head><body>helloJsp<hr>${hello} </body></html>
7、遇到的问题
1、Spring Boot: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean
把pom中所有的<scope>provided</scope>注释掉
2、报404
<1>注意controller和restcontroller区别
<2>检查application.properties
<3>检查pom是否缺支持jsp包
3、Failed to introspect annotated methods on class org.springframework.boot.web.support.SpringBootServletInitializer
org.springframework.beans.factory.BeanDefinitionStoreException: Failedtoparseconfigurationclass [com.example.SampleWebJspApplication]; nestedexceptionisjava.lang.IllegalStateException: Failedtointrospectannotatedmethodsonclassorg.springframework.boot.web.support.SpringBootServletInitializeratorg.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:187) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE] atorg.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:321) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE] atorg.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:243) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE] atorg.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:273) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE] atorg.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:98) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE] atorg.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:681) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE] atorg.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:523) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE] atorg.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE] atorg.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:369) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE] atorg.springframework.boot.SpringApplication.run(SpringApplication.java:313) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE] atorg.springframework.boot.SpringApplication.run(SpringApplication.java:1185) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE] atorg.springframework.boot.SpringApplication.run(SpringApplication.java:1174) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE] atcom.example.SampleWebJspApplication.main(SampleWebJspApplication.java:20) [classes/:na] atsun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethod) ~[na:1.8.0_77] atsun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_77] atsun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_77] atjava.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_77] atcom.intellij.rt.execution.application.AppMain.main(AppMain.java:147) [idea_rt.jar:na] Causedby: java.lang.IllegalStateException: Failedtointrospectannotatedmethodsonclassorg.springframework.boot.web.support.SpringBootServletInitializeratorg.springframework.core.type.StandardAnnotationMetadata.getAnnotatedMethods(StandardAnnotationMetadata.java:163) ~[spring-core-4.3.2.RELEASE.jar:4.3.2.RELEASE] atorg.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:301) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE] atorg.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:237) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE] atorg.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:204) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE] atorg.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:173) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE] ... 17commonframesomitted
解决方案:添加以下依赖
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
4、java.lang.NoClassDefFoundError: javax/servlet/ServletContext
Causedby: java.lang.NoClassDefFoundError: javax/servlet/ServletContextatjava.lang.Class.getDeclaredMethods0(NativeMethod) ~[na:1.8.0_77] atjava.lang.Class.privateGetDeclaredMethods(Class.java:2701) ~[na:1.8.0_77] atjava.lang.Class.getDeclaredMethods(Class.java:1975) ~[na:1.8.0_77] atorg.springframework.core.type.StandardAnnotationMetadata.getAnnotatedMethods(StandardAnnotationMetadata.java:152) ~[spring-core-4.3.2.RELEASE.jar:4.3.2.RELEASE] ... 21commonframesomittedCausedby: java.lang.ClassNotFoundException: javax.servlet.ServletContextatjava.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_77] atjava.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_77] atsun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) ~[na:1.8.0_77] atjava.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_77] ... 25commonframesomitted
解决方案
<dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId></dependency><dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId></dependency>
8、源码地址
https://github.com/Genesisxu/SpringSeries/tree/master/SpringBoot-jsp