项目背景

一些前端还是JSP的老项目,需要改造为springboot,所以需要springboot能支持JSP。

项目结构

afei-demo├──src/main                                           ├   ├──java ├   ├   ├──com.afei.test.demo├   ├   ├   ├──controller ├   ├   ├   ├──service  ├   ├   ├   ├──mapper├   ├   ├   ├──Application.java  springboot项目main方法所在的主类             ├   ├──resources            ├   ├   ├──css 存放css文件的地方├   ├   ├──images 存放.jpg,.png等图片资源的地方├   ├   ├──js 存放js文件的地方├   ├   ├──application.properties springboot配置文件├   ├──webapp    ├   ├   ├──WEB-INF/jsp 这个路径和视图配置中的spring.mvc.view.prefix要对应├   ├   ├   ├──index.jsp ├   ├   ├   ├──monitor 监控相关jsp页面├   ├   ├   ├──warning 告警相关jsp页面

依赖组件

springboot支持JSP的主要依赖组件如下:

  • spring-boot-starter-web版本为1.5.9.RELEASE;

  • jstl版本为1.2;

  • tomcat-embed-jasper版本为8.5.23;

  • spring-boot-maven-plugin版本为1.4.2.RELEASE;

核心POM

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--springboot tomcat jsp 支持开启 start--><dependency><groupId>org.apache.tomcat.embed</groupId><artifactId>tomcat-embed-jasper</artifactId><version>8.5.23</version></dependency><dependency><groupId>jstl</groupId><artifactId>jstl</artifactId><version>1.2</version></dependency><!--springboot tomcat jsp 支持开启 end-->
</dependencies>

插件配置

<build><plugins><plugin><groupId>org.springframework.boot</groupId><!--springboot-maven不能使用1.5.9.RELEASE版本, 否则会导致WEB-INF下的jsp资源无法访问--><artifactId>spring-boot-maven-plugin</artifactId><version>1.4.2.RELEASE</version><configuration><!--指定springboot的main方法类,否则会由于某些测试类中也有main方法而导致构建失败--><mainClass>com.yyfax.fdfsfront.Application</mainClass></configuration><executions><execution> <goals> <goal>repackage</goal> </goals> </execution></executions></plugin><plugin><artifactId>maven-compiler-plugin</artifactId><version>3.1</version><configuration><source>1.8</source><target>1.8</target></configuration></plugin></plugins><resources><!-- 打包时将jsp文件拷贝到META-INF目录下--><resource><!-- 指定resources插件处理哪个目录下的资源文件 --><directory>src/main/webapp</directory><!--注意此次必须要放在此目录下才能被访问到--><targetPath>META-INF/resources</targetPath><includes><include>**/**</include></includes></resource><resource><directory>${project.basedir}/lib</directory><targetPath>BOOT-INF/lib/</targetPath><includes><include>**/*.jar</include></includes></resource><resource><directory>src/main/resources</directory><includes><include>**/**</include></includes><filtering>false</filtering></resource></resources></build>

再次强调,spring-boot-maven-plugin的版本是1.4.2.RELEASE,而spring-boot-starter-web的版本是1.5.9.RELEASE。否则会导致即使能成功构建JAR包,也无法访问JSP资源。这里坑浪费了我不少时间!

静态资源

resources标签下的配置非常重要,其作用是将webapp目录下所有的资源(主要是JSP资源,因为css,js,图片等资源放在resoures目录下)拷贝到最终springboot的JAR包中的META-INF/resources目录下。

  • 为什么是/META-INF/resources目录?

这个与springboot的配置spring.resources.static-locations有关,其默认赋值在源码ResourceProperties.java中,由源码可知,classpath:/META-INF/resources/是其中一个默认的静态资源存放路径:

@ConfigurationProperties(prefix = "spring.resources", ignoreUnknownFields = false)
public class ResourceProperties implements ResourceLoaderAware {private static final String[] SERVLET_RESOURCE_LOCATIONS = { "/" };private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {"classpath:/META-INF/resources/", "classpath:/resources/","classpath:/static/", "classpath:/public/" };private static final String[] RESOURCE_LOCATIONS;static {RESOURCE_LOCATIONS = new String[CLASSPATH_RESOURCE_LOCATIONS.length+ SERVLET_RESOURCE_LOCATIONS.length];System.arraycopy(SERVLET_RESOURCE_LOCATIONS, 0, RESOURCE_LOCATIONS, 0,SERVLET_RESOURCE_LOCATIONS.length);System.arraycopy(CLASSPATH_RESOURCE_LOCATIONS, 0, RESOURCE_LOCATIONS,SERVLET_RESOURCE_LOCATIONS.length, CLASSPATH_RESOURCE_LOCATIONS.length);}/*** Locations of static resources. Defaults to classpath:[/META-INF/resources/,* /resources/, /static/, /public/] plus context:/ (the root of the servlet context).*/private String[] staticLocations = RESOURCE_LOCATIONS;... ...
}

至于其他的css,图片,js等静态资源文件,放到resources目录下即可,并结合如下核心代码:

@Configuration
@Slf4j
public class FdfsWebMvcConfigurerAdapter extends WebMvcConfigurerAdapter {@Beanpublic SecurityInterceptor getSecurityInterceptor() {return new SecurityInterceptor();}@Overridepublic void addInterceptors(InterceptorRegistry registry) {// 除了login登陆访问路径和error页面,其他都要经过SecurityInterceptor拦截器(SecurityInterceptor extends HandlerInterceptorAdapter)InterceptorRegistration addInterceptor = registry.addInterceptor(getSecurityInterceptor());// 排除配置addInterceptor.excludePathPatterns("/error");addInterceptor.excludePathPatterns("/login**");// 拦截配置addInterceptor.addPathPatterns("/**");}@Overridepublic void addViewControllers(ViewControllerRegistry registry) {// 配置welcome默认首页registry.addViewController("/").setViewName("forward:/main/index.shtml");registry.setOrder(Ordered.HIGHEST_PRECEDENCE);super.addViewControllers(registry);}@Beanpublic ServletRegistrationBean servletRegistrationBean(DispatcherServlet dispatcherServlet) {ServletRegistrationBean bean = new ServletRegistrationBean(dispatcherServlet);// RequestMapping路径都以.shtml结尾,例如:http://localhost/main/login.shtmlbean.addUrlMappings("*.shtml");// 其他文件该是什么后缀就是什么后缀bean.addUrlMappings("*.html");bean.addUrlMappings("*.css");bean.addUrlMappings("*.js");bean.addUrlMappings("*.png");bean.addUrlMappings("*.gif");bean.addUrlMappings("*.ico");bean.addUrlMappings("*.jpeg");bean.addUrlMappings("*.jpg");return bean;}@Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {// 这里的配置很重要,由于我们把css,图片,js等静态资源文件放在resources目录下,所以必须增加这些ResourceHandler,才能访问到这些资源registry.addResourceHandler("/favicon.ico").addResourceLocations("classpath:/favicon.ico");registry.addResourceHandler("/css/**").addResourceLocations("classpath:/css/");registry.addResourceHandler("/images/**").addResourceLocations("classpath:/images/");registry.addResourceHandler("/js/**").addResourceLocations("classpath:/js/");registry.addResourceHandler("/500.html").addResourceLocations("classpath:/500.html");registry.addResourceHandler("/index.jsp").addResourceLocations("classpath:/index.jsp");super.addResourceHandlers(registry);}}

视图配置

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

这里的配置要非常注意,以笔者这两个配置为例,如果我在controller中return new ModelAndView("main/index");,那么它访问的资源路径是/WEB-INF/jsp/main/index.jsp(prefix+ModelAndView+suffix)。这里强烈建议spring.mvc.view.prefix的配置以/结尾,即配置/WEB-INF/jsp/而不要配置/WEB-INF/jsp。否则如果return new ModelAndView("main/index");就会抛出如下错误:

File [/WEB-INF/jspmain/index.jsp] not found
  • 总结

  1. 配置为/WEB-INF/jsp/,那么return new ModelAndView("/main/index");return new ModelAndView("main/index");都可以访问。

  2. 配置为/WEB-INF/jsp,那么只有return new ModelAndView("/main/index");才可以访问。

SpringBoot支持JSP教程相关推荐

  1. SpringBoot集成Jsp教程

    Springboot的默认视图支持是Thymeleaf, Thymeleaf是一个流行的模板引擎,是用来开发Web和独立环境项目的服务器端的Java模版引擎,开发传统Java WEB工程时,我们还是使 ...

  2. 第二章:SpringBoot与JSP间不可描述的秘密

    springboot内部对jsp的支持并不是特别理想,而springboot推荐的视图是Thymeleaf,对于java开发人员来说还是大多数人员喜欢使用jsp,接下来我们来讲解下springboot ...

  3. springboot的jsp应该放在哪_详解SpringBoot 添加对JSP的支持(附常见坑点)

    序言: SpringBoot默认不支持JSP,如果想在项目中使用,需要进行相关初始化工作.为了方便大家更好的开发,本案例可直接作为JSP开发的脚手架工程 SpringBoot+War+JSP . 常见 ...

  4. 搭建SpringBoot、Jsp支持学习笔记

    Spring Boot 添加JSP支持 大体步骤: (1)            创建Maven web project: (2)            在pom.xml文件添加依赖: (3)     ...

  5. (转)springboot:添加JSP支持

    转自: 14.springboot:添加JSP支持 - 简书(1)创建Maven web project 使用Eclipse新建一个Maven Web Project ,项目取名为:spring-bo ...

  6. SpringBoot的Web开发入门案例8—支持jsp

    新建springboot工程:springboot_jsp,打包方式为war 导入web模块: 生成的项目结构: 包含启动类SpringbootJspApplication: package com. ...

  7. springboot超详细教程_全网最细致的SpringBoot实战教程,超适合新手小白入坑学习...

    一.Spring Boot 入门 Spring Boot 来简化Spring应用开发的一个框架,约定大于配置 Spring Boot 的底层用的就是Spring 访问官网:spring.io 再点击p ...

  8. SpringBoot Thymeleaf使用教程(实用版)

    SpringBoot Thymeleaf使用教程(实用版) 使用Thymeleaf 三大理由: 简洁漂亮 容易理解 完美支持HTML5 使用浏览器直接打开页面 不新增标签 只需增强属性 学习目标 快速 ...

  9. 很详细的SpringBoot整合UEditor教程

    很详细的SpringBoot整合UEditor教程 2017年04月10日 20:27:21 小宝2333 阅读数:21529 版权声明:本文为博主原创文章,未经博主允许不得转载. https://b ...

最新文章

  1. Python数据科学-技术详解与商业实践视频教程
  2. hihoCoder #1457 : 后缀自动机四·重复旋律7
  3. cf修改游戏客户端是什么意思_微信codm什么意思 微信codm 小飞机 落!什么意思[多图]-游戏攻略...
  4. FZU_1683 矩阵快速幂 求和
  5. eclipse编辑器未包含main类型_Shopify模版编辑器问题排查及解决办法汇总
  6. java学习笔记之斐波那契数列
  7. matlab中用数据拟合圆心,拟合圆并求圆心(matlab)
  8. 开课吧9.9元学python靠谱吗-quot;我,90 后,月薪 5k,副业 2w ”年轻人搞副业到底有多野?...
  9. C#实现基于ffmpeg加虹软的人脸识别
  10. android 常用输入法,[转载]Android 系统输入法的调用
  11. java中的关键字有哪些_java关键字有哪些?java关键字大全
  12. Autojs.pro 7.0 - 免root 连点器
  13. 关于电子发票打印报销最优美的姿势——发票大师网页版
  14. 92_目标:2019年底博客访问量达到10W+
  15. 结对项目开发(石家庄地铁乘车系统)
  16. 护眼软件Linux,四个 Linux 下的“护眼”软件解析
  17. mac 网络共享 wifi共享
  18. linux使用windows无线网卡,linux下安装windows xp无线网卡驱动
  19. app软件小程序开发
  20. 2022企业人效管理白皮书

热门文章

  1. python刷阅读_简单的37行python爬虫刷CSDN博客阅读数
  2. 阿里云物联网平台 > 设备接入 > 使用开放协议自主接入 > CoAP协议接入 >
  3. Maven学习总结(58)—— 常用的 Maven 镜像地址和中央仓库地址汇总
  4. Docker学习总结(68)—— Docker 数据卷相关知识总结
  5. Java设计模式学习总结(7)——结构型模式之适配器模式
  6. MVC三层架构在各框架中的特征
  7. JavaScript学习总结(2)——JavaScript数据类型判断
  8. mamp安装php扩展,向MAMP添加GMP PHP扩展
  9. shell数组使用技巧
  10. python 数据结构 1