栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

详解SpringBoot 添加对JSP的支持(附常见坑点)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

详解SpringBoot 添加对JSP的支持(附常见坑点)

序言:

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

常见问题:

1.修改JSP需重启才能生效:

在生产环境中,SpringBoot重新编译JSP可能会导致较大的性能损失,并且很难追查到问题根源,所以在最新的版本中,官方已经默认关闭此功能,详见JspServlet类的初始化参数。那么,如何解决这个问题呢?推荐两个解决办法:1.使用devtools 2. 添加配置(server.servlet.jsp.init-parameters.development=true)

2.各种404:

1.必须导入嵌入式容器和JASPER解析器 2.必须创建webapp目录

正文:SpringBoot 添加对JSP的支持

1. 搭建脚手架

首先使用 Spring Initializr 构建工程,其中源码和静态资源目录默认生成,这里只需手工添加Web资源目录。如图:


2. 在pom.xml 添加相关依赖

 


  
  4.0.0
  com.hehe
  springboot-web-jsp
  0.0.1-SNAPSHOT

  
  war

  
  
    org.springframework.boot
    spring-boot-starter-parent
    2.0.0.M4
    
  

  
  
    
      org.springframework.boot
      spring-boot-starter-web
    
    
      org.springframework.boot
      spring-boot-starter-tomcat
    
    
      org.apache.tomcat.embed
      tomcat-embed-jasper
    
    
      javax.servlet
      jstl
    
    
      org.springframework.boot
      spring-boot-devtools
    
    
      org.springframework.boot
      spring-boot-starter-test
      test
    
  

  
  
    
      spring-snapshots
      http://repo.spring.io/snapshot
      true
    
    
      spring-milestones
      http://repo.spring.io/milestone
    
  
  
    
      spring-snapshots
      http://repo.spring.io/snapshot
    
    
      spring-milestones
      http://repo.spring.io/milestone
    
  

  
  
    
      
 org.springframework.boot
 spring-boot-maven-plugin
      
    
  




3. 启动类添加Servlet支持

@SpringBootApplication
public class SpringbootWarJspApplication extends SpringBootServletInitializer {

  @Override
  protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
    return builder.sources(SpringbootWarJspApplication.class);
  }

  public static void main(String[] args) {
    SpringApplication.run(SpringbootWarJspApplication.class, args);
  }
}

4. 添加MVC映射

application.yml 配置如下:

spring:
 mvc:
  view:
   prefix: /WEB-INF/views/ # Read From Web Resources Dir
   suffix: .jsp

5. 编写JSP页面

在 WEB-INF/views 目录下新建一个JSP文件

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



 

Hello JSP !!

![](${pageContext.servletContext.contextPath}/doge.gif)

6.启动项目

启动方式1:在IDE启动WebJspApplication,然后打开项目地址。

启动方式2:部署到外置Tomcat,启动完成后,打开项目地址。这里需要注意的是,使用外置Tomcat部署的时候,需要将嵌入式容器调整为provided级别。(防止冲突)

    
      org.springframework.boot
      spring-boot-starter-tomcat
      provided
    
    
      org.apache.tomcat.embed
      tomcat-embed-jasper
      provided
    

7.单元测试

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@DirtiesContext
public class WebJspApplicationTest {

  @Autowired
  private TestRestTemplate restTemplate;

  @Test
  public void testJspWithEl() throws Exception {
    ResponseEntity entity = restTemplate.getForEntity("/", String.class);
    assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
    assertThat(entity.getBody()).contains("Hello JSP");
  }

}

全文至此,有疑问的小伙伴可在评论下方进行交流。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/143920.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号