SpringBoot.03.SpringBoot集成jsp

x33g5p2x  于2022-04-11 转载在 Spring  
字(5.6k)|赞(0)|评价(0)|浏览(439)

前言

在SpringBoot中默认推荐使用的模板引擎是Thymeleaf,但是作为传统Web开发的王者jsp在现在的一些企业中仍然被广泛使用。所以对于在SpringBoot中如何使用jsp还是需要有所了解。
现在广泛被推崇的是前后端分离,大有替代传统web开发的趋势。而前后端分离模式中VUE对于前端至关重要。所以对于VUE的学习也是必须的。

准备工作

由于本次演示SpringBoot.03.SpringBoot集成jsp模板,我们需要jsp页面。这里我们来新建一个jsp模板。

依次选择File->settings->Editor->File and Code Templates,然后选中Files;在nameExtension中键入jsp,将以下内容填到文本框中

  1. <%@ page pageEncoding="UTF-8" language="java" contentType="text/html; UTF-8" %>
  2. <!doctype html>
  3. <html lang="en">
  4. <head>
  5. <meta charset="UTF-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  7. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  8. <title>这是jsp页面的标题</title>
  9. </head>
  10. <body>
  11. <h1>Hello jsp!</h1>
  12. </body>
  13. </html>

最后点击Apply->OK;如下图所示:

jsp集成案例

集成步骤

1.新建Module

我们选择Spring Initializr的方式新建一个Module,按照下图填写信息后点击Next

Dependencies中选择Spring Web,点击Finish,如下图所示:

2.pom.xml
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.5.4</version>
  9. <relativePath/>
  10. </parent>
  11. <groupId>com.christy</groupId>
  12. <artifactId>springboot-03-jsp</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>springboot-03-jsp</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. </properties>
  19. <dependencies>
  20. <!-- spring-boot-starter-web -->
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-starter-web</artifactId>
  24. </dependency>
  25. <!-- spring-boot-starter-test -->
  26. <dependency>
  27. <groupId>org.springframework.boot</groupId>
  28. <artifactId>spring-boot-starter-test</artifactId>
  29. <!-- scope为test说明该依赖只在测试时有用,打包时不会打进来 -->
  30. <scope>test</scope>
  31. </dependency>
  32. <!-- c标签库 -->
  33. <dependency>
  34. <groupId>jstl</groupId>
  35. <artifactId>jstl</artifactId>
  36. <version>1.2</version>
  37. </dependency>
  38. <!-- 让内嵌tomcat具有解析jsp功能 -->
  39. <dependency>
  40. <groupId>org.apache.tomcat.embed</groupId>
  41. <artifactId>tomcat-embed-jasper</artifactId>
  42. </dependency>
  43. </dependencies>
  44. <build>
  45. <plugins>
  46. <!-- spring-boot-maven-plugin 该插件是SpringBoot官方集成的maven插件,
  47. 除了能保证正确执行java -jar来启动项目
  48. 另外还能是项目正常加载jsp页面
  49. -->
  50. <plugin>
  51. <groupId>org.springframework.boot</groupId>
  52. <artifactId>spring-boot-maven-plugin</artifactId>
  53. </plugin>
  54. </plugins>
  55. </build>
  56. </project>
3.Springboot03JspApplication.java
  1. import org.springframework.boot.SpringApplication;
  2. import org.springframework.boot.autoconfigure.SpringBootApplication;
  3. @SpringBootApplication
  4. public class Springboot03JspApplication {
  5. public static void main(String[] args) {
  6. SpringApplication.run(Springboot03JspApplication.class, args);
  7. }
  8. }
4.application.yml
  1. server:
  2. port: 8803
  3. # 配置视图解析器
  4. spring:
  5. mvc:
  6. view:
  7. prefix: /
  8. suffix: .jsp
5.index.jsp

我们在webapp目录右键选择新建index.jsp页面,如下图所示:

修改jsp页面内容如下:

  1. <%@ page pageEncoding="UTF-8" language="java" contentType="text/html; UTF-8" %>
  2. <!doctype html>
  3. <html lang="en">
  4. <head>
  5. <meta charset="UTF-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  7. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  8. <title>这是jsp页面的标题</title>
  9. </head>
  10. <body>
  11. <h1>Hello SpringBoot!</h1>
  12. </body>
  13. </html>
6.JspController.java
  1. import org.slf4j.Logger;
  2. import org.slf4j.LoggerFactory;
  3. import org.springframework.stereotype.Controller;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. /**
  6. * @Author Christy
  7. * @Date 2021/9/2 10:02
  8. * @Controller 这里不能使用@RestController注解,否则无法跳转页面只能输出字符串
  9. **/
  10. @Controller
  11. @RequestMapping("jsp")
  12. public class JspController {
  13. private static final Logger log = LoggerFactory.getLogger(JspController.class);
  14. @RequestMapping("hello")
  15. public String sayHello(){
  16. log.info("Hello Spring Boot!");
  17. // 由于我们配置了视图解析器,返回的index会被重定向为url地址:http://localhost:8803/index.jsp
  18. return "index";
  19. }
  20. }
7.测试

我们启动项目,浏览器访问http://localhost:8803/jsp/hello,结果如下:

一脸懵逼有没有?按理说配置啥的都没有错啊!为啥会404呢?

问题分析

之所以出现404原因就是项目并没有找到webapp这个目录,至于更深层次的原因我也不知道。如果哪位大牛能解释这个问题麻烦告知一下。

解决方案

1.springboot:run

在pom.xml文件中我们集成另外SpringBoot官方的maven插件,那么在右侧边栏maven中找到Plugins下的springboot:run命令运行项目。如下图所示:

项目重启后浏览器访问http://localhost:8803/jsp/hello,结果如下:

2.设置Working directory

我们选择Edit Configuration,在Working directory中选择MODULE_DIR。如下图所示:

项目再次重启后浏览器访问http://localhost:8803/jsp/hello,结果如下:

如果出现访问页面是变成了页面下载,说明jsp页面没有被正确编译和解析。请刷新一下maven重新加载一下依赖;

修改jsp无须重启应用

在实际开发过程中页面调试是非常繁琐的,在更改了相应程序之后需要同步刷新页面数据,这就需要频繁的重启应用。对于jsp的集成,SpringBoot提供了一种方法在更改程序或者数据后不需要重启应用,只需要重新刷新页面就可以了。

我们打开配置文件,加上以下代码,内容如下:

  1. server:
  2. port: 8803
  3. servlet:
  4. jsp:
  5. init-parameters:
  6. development: true
  7. # 配置视图解析器
  8. spring:
  9. mvc:
  10. view:
  11. prefix: / #这个代表jsp的根路径 需要在java包下新建webapp目录
  12. suffix: .jsp

我们重启应用,访问http://localhost:8803/jsp/hello,页面还是显示Hello SpringBoot!,然后我们在jsp页面中新增<h1>Hello Christy!</h1>,然后刷新页面。结果如下:

相关文章