SpringBoot集成jsp(附源码)+遇到的坑

简介: SpringBoot集成jsp(附源码)+遇到的坑

1、大体步骤

(1)       创建Maven web project;


(2)       在pom.xml文件添加依赖;


(3)       配置application.properties支持jsp


(4)       编写测试Controller


(5)       编写JSP页面


(6)       编写启动类App.java

2、新建SpringInitialzr

image.png

image.png

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.*/@ControllerpublicclassHelloController {
// 从 application.properties 中读取配置,如取不到默认值为Hello@Value("${application.hello:Hello}")
privateStringhello;
@RequestMapping("/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

目录
相关文章
|
20天前
|
Web App开发 编解码 Java
B/S基层卫生健康云HIS医院管理系统源码 SaaS模式 、Springboot框架
基层卫生健康云HIS系统采用云端SaaS服务的方式提供,使用用户通过浏览器即能访问,无需关注系统的部署、维护、升级等问题,系统充分考虑了模板化、配置化、智能化、扩展化等设计方法,覆盖了基层医疗机构的主要工作流程,能够与监管系统有序对接,并能满足未来系统扩展的需要。
46 4
|
19天前
|
运维 监控 安全
云HIS医疗管理系统源码——技术栈【SpringBoot+Angular+MySQL+MyBatis】
云HIS系统采用主流成熟技术,软件结构简洁、代码规范易阅读,SaaS应用,全浏览器访问前后端分离,多服务协同,服务可拆分,功能易扩展;支持多样化灵活配置,提取大量公共参数,无需修改代码即可满足不同客户需求;服务组织合理,功能高内聚,服务间通信简练。
32 4
|
2天前
|
前端开发 Java 关系型数据库
Java医院绩效考核系统源码B/S架构+springboot三级公立医院绩效考核系统源码 医院综合绩效核算系统源码
作为医院用综合绩效核算系统,系统需要和his系统进行对接,按照设定周期,从his系统获取医院科室和医生、护士、其他人员工作量,对没有录入信息化系统的工作量,绩效考核系统设有手工录入功能(可以批量导入),对获取的数据系统按照设定的公式进行汇算,且设置审核机制,可以退回修正,系统功能强大,完全模拟医院实际绩效核算过程,且每步核算都可以进行调整和参数设置,能适应医院多种绩效核算方式。
21 2
|
3天前
|
运维 监控 Java
springboot基层区域HIS系统源码
医疗(医院)机构正式使用云HIS系统之前,要先进行院内基础数据的配置,主要在数据管理模块中进行,由系统管理员来操作。
10 0
|
4天前
|
Android开发
Android 高通平台集成无源码apk示例
Android 高通平台集成无源码apk示例
14 0
|
4天前
|
传感器 人工智能 前端开发
JAVA语言VUE2+Spring boot+MySQL开发的智慧校园系统源码(电子班牌可人脸识别)Saas 模式
智慧校园电子班牌,坐落于班级的门口,适合于各类型学校的场景应用,班级学校日常内容更新可由班级自行管理,也可由学校统一管理。让我们一起看看,电子班牌有哪些功能呢?
46 4
JAVA语言VUE2+Spring boot+MySQL开发的智慧校园系统源码(电子班牌可人脸识别)Saas 模式
|
12天前
|
缓存 Java 开发者
10个点介绍SpringBoot3工作流程与核心组件源码解析
Spring Boot 是Java开发中100%会使用到的框架,开发者不仅要熟练使用,对其中的核心源码也要了解,正所谓知其然知其所以然,V 哥建议小伙伴们在学习的过程中,一定要去研读一下源码,这有助于你在开发中游刃有余。欢迎一起交流学习心得,一起成长。
|
15天前
|
JavaScript Java 大数据
springboot高精度UWB定位系统源码
UWB (ULTRA WIDE BAND,) 技术是一种无线载波通讯技术,它不采用正弦载波,而是利用纳秒级的非正弦波窄脉冲传输数据,因此其所占的频谱范围很宽。UWB定位系统依托在移动通信,雷达,微波电路,云计算与大数据处理等专业领域的多年积累,自主研发,开发并产业化的一套UWB精确定位系统,最高定位精度可达10cm,具有高精度,高动态,高容量,低功耗的优点。
18 0
|
23天前
|
人工智能 移动开发 前端开发
Springboot医院智慧导诊系统源码:精准推荐科室
医院智慧导诊系统是在医疗中使用的引导患者自助就诊挂号,在就诊的过程中有许多患者不知道需要挂什么号,要看什么病,通过智慧导诊系统,可输入自身疾病的症状表现,或选择身体部位,在经由智慧导诊系统多维度计算,精准推荐科室,引导患者挂号就诊,实现科学就诊,不用担心挂错号。
35 2
|
23天前
|
存储 消息中间件 Java
基于Springboot框架+云计算的区域HIS云平台源码
基于云计算技术的B/S架构的HIS系统,为基层医院机构提供标准化的、信息化的、可共享的医疗信息管理系统,实现医患事务管理和临床诊疗管理等标准管理信息系统的功能。系统利用健康云计算平台的技术优势,建立统一的健康档案存储平台,有效实现医院数据共享与交换,解决数据重复采集及信息孤岛等问题,为实现区域卫生信息化平台奠定了基础。
43 8