博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringBoot集成jsp(附源码)+遇到的坑
阅读量:6614 次
发布时间:2019-06-24

本文共 7598 字,大约阅读时间需要 25 分钟。

1、大体步骤

(1) 创建Maven web project;

(2) 在pom.xml文件添加依赖;
(3) 配置application.properties支持jsp
(4) 编写测试Controller
(5) 编写JSP页面
(6) 编写启动类App.java

2、新建SpringInitialzr

3、pom文件

复制代码
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-tomcat
provided
org.apache.tomcat.embed
tomcat-embed-jasper
org.springframework.boot
spring-boot-starter-test
test
复制代码

 

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. */@Controllerpublic class HelloController {    // 从 application.properties 中读取配置,如取不到默认值为Hello    @Value("${application.hello:Hello}")    private String hello;    @RequestMapping("/helloJsp")    public String helloJsp(Map
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" %>
GODhelloJsp

${hello}
复制代码

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:187) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]    at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:321) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]    at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:243) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:273) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:98) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:681) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:523) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:369) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]    at org.springframework.boot.SpringApplication.run(SpringApplication.java:313) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1185) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1174) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]    at com.example.SampleWebJspApplication.main(SampleWebJspApplication.java:20) [classes/:na]    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_77]    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_77]    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_77]    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_77]    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) [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:163) ~[spring-core-4.3.2.RELEASE.jar:4.3.2.RELEASE]    at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:301) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]    at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:237) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:204) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:173) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]    ... 17 common frames omitted
复制代码

解决方案:添加以下依赖

org.apache.tomcat.embed
tomcat-embed-jasper

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:2701) ~[na:1.8.0_77]    at java.lang.Class.getDeclaredMethods(Class.java:1975) ~[na:1.8.0_77]    at org.springframework.core.type.StandardAnnotationMetadata.getAnnotatedMethods(StandardAnnotationMetadata.java:152) ~[spring-core-4.3.2.RELEASE.jar:4.3.2.RELEASE]    ... 21 common frames omittedCaused by: java.lang.ClassNotFoundException: javax.servlet.ServletContext    at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_77]    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_77]    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) ~[na:1.8.0_77]    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_77]    ... 25 common frames omitted
复制代码

解决方案:添加

复制代码
javax.servlet
javax.servlet-api
javax.servlet
jstl
复制代码

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

8、源码地址

作者:

出处:

您的支持是对博主最大的鼓励,感谢您的认真阅读。

本文版权归作者和博客园共有,欢迎转载。

但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

http://www.cnblogs.com/God-/p/5857428.html

 

你可能感兴趣的文章
Android自定义实现圆形播放进度条
查看>>
阿里面试题目总结
查看>>
ListView的定位
查看>>
控件焦点问题
查看>>
移动CDN改善用户体验
查看>>
[谷歌旗下Blogger CSRF漏洞 全文]
查看>>
数据库监控模板
查看>>
spring ioc之bean的初始化
查看>>
MongoDb查询和分组统计示例
查看>>
C++ Primer第四版中文版(电子版)
查看>>
分析下为什么spring 整合mybatis后为啥用不上session缓存
查看>>
BAMEasyTable
查看>>
android activity 六个主要函数以及一些调用顺序
查看>>
【干货】在docker中安装配置Greenplum集群的过程
查看>>
政府和社会资合作(ppp)概述
查看>>
p6spy介绍
查看>>
记一次mysql中浮点类型字段引起的问题
查看>>
笔记本电脑同时连接无线网络(互联网)和有线网络(内网)的方法
查看>>
eclipse离线插件
查看>>
BOM导致的Build failed in Jenkins with error MSB4025
查看>>