本文介绍了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文件

  1. <dependencies>

  2. <dependency>

  3. <groupId>org.springframework.boot</groupId>

  4. <artifactId>spring-boot-starter-web</artifactId>

  5. </dependency>

  6. <dependency>

  7. <groupId>org.springframework.boot</groupId>

  8. <artifactId>spring-boot-starter-tomcat</artifactId>

  9. <scope>provided</scope>

  10. </dependency>

  11. <dependency>

  12. <groupId>org.apache.tomcat.embed</groupId>

  13. <artifactId>tomcat-embed-jasper</artifactId>

  14. </dependency>

  15. <dependency>

  16. <groupId>org.springframework.boot</groupId>

  17. <artifactId>spring-boot-starter-test</artifactId>

  18. <scope>test</scope>

  19. </dependency>

  20. </dependencies>

4、application.properties文件

  1. # 页面默认前缀目录
  2. spring.mvc.view.prefix=/WEB-INF/jsp/
  3. # 响应页面默认后缀
  4. spring.mvc.view.suffix=.jsp
  5. # 自定义属性,可以在Controller中读取
  6. application.hello=Hello GOD

5、Controller文件

  1. package com.example;

  2.  
  3. import org.springframework.beans.factory.annotation.Value;

  4. import org.springframework.stereotype.Controller;

  5. import org.springframework.web.bind.annotation.RequestMapping;

  6.  
  7. import java.util.Map;
  8.  
  9. /**

  10. * Created by Gensis on 2016/9/9.

  11. */

  12. @Controller

  13.  
  14. public class HelloController {

  15.  
  16. // 从 application.properties 中读取配置,如取不到默认值为Hello

  17. @Value("${application.hello:Hello}")

  18. private String hello;

  19.  
  20. @RequestMapping("/helloJsp")

  21. public String helloJsp(Map<String, Object> map) {

  22. System.out.println("HelloController.helloJsp().hello=" + hello);

  23. map.put("hello", hello);

  24. return "helloJsp";



  25. }



  26. }

6、jsp页面

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8" %>

  2. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">

  3. <html>

  4. <head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>GOD</title></head>

  5. <body>

  6. helloJsp

  7. <hr>

  8. ${hello}

  9. </body>

  10. </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

  1. 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

  2. at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:187) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]

  3. at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:321) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]

  4. at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:243) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]

  5. at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:273) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]

  6. at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:98) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]

  7. at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:681) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]

  8. at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:523) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]

  9. at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]

  10. at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:369) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]

  11. at org.springframework.boot.SpringApplication.run(SpringApplication.java:313) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]

  12. at org.springframework.boot.SpringApplication.run(SpringApplication.java:1185) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]

  13. at org.springframework.boot.SpringApplication.run(SpringApplication.java:1174) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]

  14. at com.example.SampleWebJspApplication.main(SampleWebJspApplication.java:20) [classes/:na]

  15. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_77]

  16. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_77]

  17. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_77]

  18. at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_77]

  19. at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) [idea_rt.jar:na]

  20. Caused by: java.lang.IllegalStateException: Failed to introspect annotated methods on class org.springframework.boot.web.support.SpringBootServletInitializer

  21. at org.springframework.core.type.StandardAnnotationMetadata.getAnnotatedMethods(StandardAnnotationMetadata.java:163) ~[spring-core-4.3.2.RELEASE.jar:4.3.2.RELEASE]

  22. at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:301) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]

  23. at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:237) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]

  24. at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:204) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]

  25. at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:173) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]

  26. ... 17 common frames omitted

解决方案:添加以下依赖

<dependency>


<groupId>org.apache.tomcat.embed</groupId>


<artifactId>tomcat-embed-jasper</artifactId>


</dependency>

4、java.lang.NoClassDefFoundError: javax/servlet/ServletContext

  1. Caused by: java.lang.NoClassDefFoundError: javax/servlet/ServletContext

  2. at java.lang.Class.getDeclaredMethods0(Native Method) ~[na:1.8.0_77]

  3. at java.lang.Class.privateGetDeclaredMethods(Class.java:2701) ~[na:1.8.0_77]

  4. at java.lang.Class.getDeclaredMethods(Class.java:1975) ~[na:1.8.0_77]

  5. at org.springframework.core.type.StandardAnnotationMetadata.getAnnotatedMethods(StandardAnnotationMetadata.java:152) ~[spring-core-4.3.2.RELEASE.jar:4.3.2.RELEASE]

  6. ... 21 common frames omitted

  7. Caused by: java.lang.ClassNotFoundException: javax.servlet.ServletContext

  8. at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_77]

  9. at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_77]

  10. at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) ~[na:1.8.0_77]

  11. at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_77]

  12. ... 25 common frames omitted

解决方案:添加

  1. <dependency>

  2. <groupId>javax.servlet</groupId>

  3. <artifactId>javax.servlet-api</artifactId>

  4. </dependency>

  5. <dependency>

  6. <groupId>javax.servlet</groupId>

  7. <artifactId>jstl</artifactId>

  8. </dependency>

删除其他依赖中的<scope>provided</scope>

8、源码地址

https://github.com/Genesisxu/SpringSeries/tree/master/SpringBoot-jsp

详解SpringBoot集成jsp(附源码)+遇到的坑的更多相关文章

  1. 详解Go语言调度循环源码实现

    转载请声明出处哦~,本篇文章发布于luozhiyun的博客: https://www.luozhiyun.com/archives/448 本文使用的go的源码15.7 概述 提到"调度&q ...

  2. 玩转Android之Picasso使用详详详详详详解,从入门到源码剖析!!!!

    Picasso是Squareup公司出的一款图片加载框架,能够解决我们在Android开发中加载图片时遇到的诸多问题,比如OOM,图片错位等,问题主要集中在加载图片列表时,因为单张图片加载谁都会写.如 ...

  3. 多图详解Go的sync.Pool源码

    转载请声明出处哦~,本篇文章发布于luozhiyun的博客:https://www.luozhiyun.com 本文使用的go的源码时14.4 Pool介绍 总所周知Go 是一个自动垃圾回收的编程语言 ...

  4. Android事件分发详解(三)——ViewGroup的dispatchTouchEvent()源码学习

    package cc.aa; import android.os.Environment; import android.view.MotionEvent; import android.view.V ...

  5. 【Java入门提高篇】Day26 Java容器类详解(八)HashSet源码分析

    前面花了好几篇的篇幅把HashMap里里外外说了个遍,大家可能对于源码分析篇已经讳莫如深了.别慌别慌,这一篇来说说集合框架里最偷懒的一个家伙——HashSet,为什么说它是最偷懒的呢,先留个悬念,看完 ...

  6. 【Java入门提高篇】Day22 Java容器类详解(五)HashMap源码分析(上)

    准备了很长时间,终于理清了思路,鼓起勇气,开始介绍本篇的主角——HashMap.说实话,这家伙能说的内容太多了,要是像前面ArrayList那样翻译一下源码,稍微说说重点,肯定会让很多人摸不着头脑,不 ...

  7. 【Java入门提高篇】Day21 Java容器类详解(四)ArrayList源码分析

    今天要介绍的是List接口中最常用的实现类——ArrayList,本篇的源码分析基于JDK8,如果有不一致的地方,可先切换到JDK8后再进行操作. 本篇的内容主要包括这几块: 1.源码结构介绍 2.源 ...

  8. Geth命令用法-参数详解 and 以太坊源码文件目录

    本文是对以太坊客户端geth命令的解析 命令用法 geth [选项] 命令 [命令选项] [参数-] 版本 1.7.3-stable 命令 account 管理账户 attach 启动交互式JavaS ...

  9. 详解Tomcat系列(一)-从源码分析Tomcat的启动

    在整个Tomcat系列文章讲解之前, 我想说的是虽然整个Tomcat体系比较复杂, 但是Tomcat中的代码并不难读, 只要认真花点功夫, 一定能啃下来. 由于篇幅的原因, 很难把Tomcat所有的知 ...

随机推荐

  1. Android偏好设置(6)应用和监听各偏好参数

    Reading Preferences By default, all your app's preferences are saved to a file that's accessible fro ...

  2. JAVA Android王牌教程

    Java基础 在Java基础系列文章中,我将说明Java的基础内容,特别是面向对象的相关概念. Java基础01 从HelloWorld到面向对象 Java基础02 方法与数据成员 Java基础03 ...

  3. Oracle的一些名词和概念

    1.数据库 这里的数据库不是通常情况下我们所说的数据库,而是一个Oracle的专业名词.它是磁盘上存储数据的集合,在物理上表现为数据文件. 日志文件和控制文件等,在逻辑上以表空间形式存在.使用时,必须 ...

  4. Spring注解驱动开发之声明式事务

    前言:现今SpringBoot.SpringCloud技术非常火热,作为Spring之上的框架,他们大量使用到了Spring的一些底层注解.原理,比如@Conditional.@Import.@Ena ...

  5. vue项目中安装cnpm和node_modules

    1.安装cnpm的nodejs包管理工具,命令行: npm install -g cnpm --registry=https://registry.npm.taobao.org   2. 每个vue项 ...

  6. ReactJS-1-基本使用

    JSX使用 一.为什么使用JSX?React的核心机制之一就是虚拟DOM:可以在内存中创建的虚拟DOM元素.但是用js创建虚拟dom可读性差,于是创建了JSX,继续使用HTML代码创建dom,增加可读 ...

  7. 个人复习记录之-js

    1变量:内存中储存一个数据的储存空间. 使用:声明,赋值,取值.注 不能以数字开头,不能用保留字 *声明提前:在程序正式执行前,都会将所有var声明的变量提前到开始位置,集中创建***赋值留在原地** ...

  8. 《基于Node.js实现简易聊天室系列之项目前期工作》

    前期工作主要包括:项目的创建,web服务器的创建和数据库的连接. 项目创建 网上关于Node.js项目的创建的教程有很多,这里不必赘述.Demo所使用的Node.js的框架是express,版本为4. ...

  9. spark shuffle:分区原理及相关的疑问

    一.分区原理 1.为什么要分区?(这个借用别人的一段话来阐述.) 为了减少网络传输,需要增加cpu计算负载.数据分区,在分布式集群里,网络通信的代价很大,减少网络传输可以极大提升性能.mapreduc ...

  10. iOS Programming Editing UITableView

    iOS Programming Editing UITableView 1.1 Editing mode  UITableView has an editing property, and when ...