SpringBoot集成jsp(附源码)+遇到的坑
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=Hello GOD
5、Controller文件
package com.example;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Map;
/**
* Created by Gensis on 2016/9/9.
*/
@Controller
public class HelloController {
// 从 application.properties 中读取配置,如取不到默认值为Hello
@Value("${application.hello:Hello}")
private String hello;
@RequestMapping("/helloJsp")
public String helloJsp(Map<String, Object> map) {
System.out.println("HelloController.helloJsp().hello=" + hello);
map.put("hello", hello);
return "helloJsp";
}
}
6、jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head><meta http-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: Failed to parse configuration class [com.example.SampleWebJspApplication]; nested exception is java.lang.IllegalStateException: Failed to introspect annotated methods on class org.springframework.boot.web.support.SpringBootServletInitializer
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:) ~[spring-context-.RELEASE.jar:.RELEASE]
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:) ~[spring-context-.RELEASE.jar:.RELEASE]
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:) ~[spring-context-.RELEASE.jar:.RELEASE]
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:) ~[spring-context-.RELEASE.jar:.RELEASE]
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:) ~[spring-context-.RELEASE.jar:.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:) ~[spring-context-.RELEASE.jar:.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:) ~[spring-context-.RELEASE.jar:.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:) [spring-boot-.RELEASE.jar:.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:) [spring-boot-.RELEASE.jar:.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:) [spring-boot-.RELEASE.jar:.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:) [spring-boot-.RELEASE.jar:.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:) [spring-boot-.RELEASE.jar:.RELEASE]
at com.example.SampleWebJspApplication.main(SampleWebJspApplication.java:) [classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_77]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:) ~[na:1.8.0_77]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:) ~[na:1.8.0_77]
at java.lang.reflect.Method.invoke(Method.java:) ~[na:1.8.0_77]
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:) [idea_rt.jar:na]
Caused by: java.lang.IllegalStateException: Failed to introspect annotated methods on class org.springframework.boot.web.support.SpringBootServletInitializer
at org.springframework.core.type.StandardAnnotationMetadata.getAnnotatedMethods(StandardAnnotationMetadata.java:) ~[spring-core-.RELEASE.jar:.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:) ~[spring-context-.RELEASE.jar:.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:) ~[spring-context-.RELEASE.jar:.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:) ~[spring-context-.RELEASE.jar:.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:) ~[spring-context-.RELEASE.jar:.RELEASE]
... common frames omitted
解决方案:添加以下依赖
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
4、java.lang.NoClassDefFoundError: javax/servlet/ServletContext
Caused by: java.lang.NoClassDefFoundError: javax/servlet/ServletContext
at java.lang.Class.getDeclaredMethods0(Native Method) ~[na:1.8.0_77]
at java.lang.Class.privateGetDeclaredMethods(Class.java:) ~[na:1.8.0_77]
at java.lang.Class.getDeclaredMethods(Class.java:) ~[na:1.8.0_77]
at org.springframework.core.type.StandardAnnotationMetadata.getAnnotatedMethods(StandardAnnotationMetadata.java:) ~[spring-core-.RELEASE.jar:.RELEASE]
... common frames omitted
Caused by: java.lang.ClassNotFoundException: javax.servlet.ServletContext
at java.net.URLClassLoader.findClass(URLClassLoader.java:) ~[na:1.8.0_77]
at java.lang.ClassLoader.loadClass(ClassLoader.java:) ~[na:1.8.0_77]
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:) ~[na:1.8.0_77]
at java.lang.ClassLoader.loadClass(ClassLoader.java:) ~[na:1.8.0_77]
... common frames omitted
解决方案:添加
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
删除其他依赖中的<scope>provided</scope>
8、源码地址
https://github.com/Genesisxu/SpringSeries/tree/master/SpringBoot-jsp
SpringBoot集成jsp(附源码)+遇到的坑的更多相关文章
- Java开发简单的家居购物商城系统 JSP 附源码
开发环境: Windows操作系统开发工具: MyEclipse+Jdk+Tomcat+MYSQL数据库 注意:使用tomcat7运行 运行效果图 源码及原文链接:https://javadao ...
- SpringBoot集成Swagger(根据源码深入学习Swagger的用法)
从源码层面讲解Swagger的用法,快速了解掌握Swagger 简介 Swagger 是一个规范且完整的框架,用于生成.描述.调用和可视化 Restful 风格的 Web 服务. 自动生成html文档 ...
- 详解SpringBoot集成jsp(附源码)+遇到的坑
本文介绍了SpringBoot集成jsp(附源码)+遇到的坑 ,分享给大家 1.大体步骤 (1)创建Maven web project: (2)在pom.xml文件添加依赖: (3)配置applica ...
- Django集成Markdown编辑器【附源码】
专注内容写作的你一定不要错过markdown 简单介绍 markdown是一种标记语言,通过简单的标记语法可以使普通的文本内容具有一定的格式,使用非常简单,学习成本极低 目前各大Blog平台都已支持m ...
- SpringBoot集成jsp
一.springBoot集成jsp: 1.修改pom文件 <!--集成jsp所需jar包--> <!--jsp页面使用jstl标签--> <dependency> ...
- 微服务8:通信之RPC实践篇(附源码)
★微服务系列 微服务1:微服务及其演进史 微服务2:微服务全景架构 微服务3:微服务拆分策略 微服务4:服务注册与发现 微服务5:服务注册与发现(实践篇) 微服务6:通信之网关 微服务7:通信之RPC ...
- 在网站开发中很有用的8个 jQuery 效果【附源码】
jQuery 作为最优秀 JavaScript 库之一,改变了很多人编写 JavaScript 的方式.它简化了 HTML 文档遍历,事件处理,动画和 Ajax 交互,而且有成千上万的成熟 jQuer ...
- 8个前沿的 HTML5 & CSS3 效果【附源码下载】
作为一个前沿的 Web 开发者,对于 HTML5 和 CSS3 技术或多或少都有掌握.前几年这些新技术刚萌芽的时候,开发者们已经使用它们来小试牛刀了,如今这些先进技术已经遍地开发,特别是在移动端大显身 ...
- iOS二十种超酷时尚艺术滤镜汇总【附源码】
本文总结了20种ios滤镜都是基于GPUImage的,有3种滤镜是GPUImage库中包含的,还有17种是Instagram中的经典滤镜,集成在一个项目中.使用GPUImage可以非常容易创建我们自己 ...
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(2)-easyui构建前端页面框架[附源码]
原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(2)-easyui构建前端页面框架[附源码] 开始,我们有了一系列的解决方案,我们将动手搭建新系统吧. 用 ...
随机推荐
- 安装Arch Linux(桌面环境)
安装xorg-server # pacman -S xorg-server xorg-server-utils xorg-xinit 安装显卡驱动 如果不知道是什么显卡,就使用以下命令查看 # lsp ...
- Java简单类(部门、领导、雇员关系)
class Dept { private int deptno ; private String dname ; private String loc ; private Emp emps [] ; ...
- 用GSON解析Json格式数据
GSON是谷歌提供的开源库,用来解析Json格式的数据,非常好用.如果要使用GSON的话,则要先下载gson-2.2.4.jar这个文件,如果是在Android项目中使用,则在Android项目的li ...
- OceanBase架构(二)
http://www.cnblogs.com/LiJianBlog/p/4779990.html OceanBase架构浅析(二) 单点性能 OceanBase架构的优势在于既支持跨行跨表事务,又 ...
- [转]线性插值&双线性插值&三线性插值
转自:http://www.cnblogs.com/yingying0907/archive/2012/11/21/2780092.html 內插是数学领域数值分析中的通过已知的离散数据求未知数据的过 ...
- 如何安装mysql服务
我刚开始安装mysql的时候,在windows的服务里面可以看到,但是装了以后有一段时间没有用它了,我在准备从windows的服务里面启动mysql服务的时候,发现没有mysql的服务了,那我的解决办 ...
- RDIFramework.NET V2.9版本 WinFom部分新增与修正的功能
RDIFramework.NET V2.9版本 WinFom部分新增与修正的功能 转眼间RDIFramework.NET框架走了快6个年头了,随着一个版本一个版本的升级改造,现在已经越来越完美了.使 ...
- JDBC连接mysql数据库,添加数据
如下:其中添加/删除/修改只是sql字符串不同 //3.要执行的字符串 String sql="INSERT INTO t_student(NAME,age,email) VALUES('x ...
- 夺命雷公狗-----React---11--添加css样式的方法
<!DOCTYPE> <html> <head> <meta charset="utf-8"> <title></ ...
- iOS开发中NSLog输出格式大全
本文的内容是总结了一下iOS开发中NSLog输出格式大全,虽然比较基础,但有总结毕竟会各位正在学习iOS开发的朋友们一些小小的帮助. %@ 对象 %d, %i ...