• 【Spring boot 集成 JSP】


    Spring boot 集成 JSP
    这个部分比较复杂,所以单独创建一个工程来进行讲解;
    大体步骤:
    1 )创建 Maven web project
    2 )在 pom.xml 文件添加依赖;
    3 )配置 application.properties 支持 jsp
    4 )编写测试 Controller
    5 )编写 JSP 页面
    6 )编写启动类 App.java
    1,FreeMarker
    2,Groovy
    3,Thymeleaf (Spring 官网使用这个)
    4,Velocity
    5,JSP (貌似 Spring Boot 官方不推荐,STS 创建的项目会在 src/main/resources 下有个 templates 目录,这
    里就是让我们放模版文件的,然后并没有生成诸如 SpringMVC 中的 webapp 目录)
    不过本文还是选择大家都熟悉的 JSP 来举例,因为使用 JSP 与默认支持的模版需要特殊处理,所以拿来举例更
    好。
    (1)创建 Maven web project
    使用 Eclipse 新建一个 Maven Web Project ,项目取名为:
    springboot02
    (2)在 pom.xml 文件添加依赖
    < parent >
    < groupId > org.springframework.boot groupId >
    < artifactId > spring-boot-starter-parent artifactId >
    < version > 1.3.3.RELEASE version >
    parent >
    依赖包:
    < dependency >
    < groupId > org.springframework.boot groupId >
    < artifactId > spring-boot-starter-web artifactId >
    dependency >
    < dependency >
    < groupId > javax.servlet groupId >
    < artifactId > javax.servlet-api artifactId >
    < scope > provided scope >
    dependency >
    < dependency >
    < groupId > javax.servlet groupId >
    < artifactId > jstl artifactId >
    dependency >
    < dependency >
    < groupId > org.springframework.boot groupId >
    < artifactId > spring-boot-starter-tomcat artifactId >
    < scope > provided scope >
    dependency >
    < dependency >
    < groupId > org.apache.tomcat.embed groupId >
    < artifactId > tomcat-embed-jasper artifactId >
    < scope > provided scope >
    dependency >
    Jdk 编译版本:
    < build >
    < finalName > spring-boot-jsp finalName >
    < plugins >
    < plugin >
    < artifactId > maven-compiler-plugin artifactId >
    < configuration >
    < source > 1.8 source >
    < target > 1.8 target >
    configuration >
    plugin >
    plugins >
    build >
    (3)application.properties 配置
    上面说了 spring-boot 不推荐 JSP,想使用 JSP 需要配置 application.properties。
    添加 src/main/resources/application.properties 内容:
    # 页面默认前缀目录
    spring.mvc.view.prefix = /WEB-INF/views/
    # 响应页面默认后缀
    spring.mvc.view.suffix = .jsp
    # 自定义属性,可以在 Controller 中读取
    application.hello = Hello Zjs From application
    (4)编写测试 Controller
    编写类:com.hpit.sb.controller. HelloJSPController:
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.ModelMap;
    import org.springframework.web.bind.annotation.RequestMapping;
    /**
    * TODO 开发控制器,该控制器将返回到JSP视图
    *
    *
    */
    @Controller
    public class HelloJSPController {
    @RequestMapping ( "/index" )
    public String hello(ModelMap map ) {
    map .put( "message" , "this data is from the backing server" );
    return "index" ;
    }
    }
    (5)编写 JSP 页面
    在 src/main 下面创建 webapp/WEB-INF/views 目录用来存放我们的 jsp 页面:index.jsp
    <%@ page language = "java" contentType = "text/html; charset=UTF-8"
    pageEncoding = "UTF-8" %>
    DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
    < html >
    < head >
    < meta http-equiv = "Content-Type" content = "text/html; charset=UTF-8" >
    < title > hello jsp title >
    head >
    < body >
    ${message }
    body >
    (6)编写启动类
    编写 App.java 启动类:
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    @SpringBootApplication
    public class App {
    public static void main(String[] args ) throws Exception {
    SpringApplication. run (App. class , args );
    }
    }
    运行程序,访问页面:

     

    附注:关于集成 JSP 几个问题:
    1 Spring Boot 使用 jsp 时,仍旧可以打成 jar 包的形式吗?
    2 Spring Boot 使用 jsp 时,比如说 css image js 等三种静态资源文件,应该放在什么目录下?这些静态资源映
    射,在 spring boot 中具体应该怎么做?
    例如,下面是 spring 中做的静态资源映射,但是在 spring boot 中不知道怎么处理:
    3 下面这个 tomcat 的包必须导入 吗, spring-boot-starter-web 中不是有一个内嵌的 tomcat 吗?
    < dependency >
    < groupId > org.springframework.boot groupId >
    < artifactId > spring-boot-starter-tomcat artifactId >
    < scope > provided scope >
    dependency >
    <1>、针对第一个问题,答案是不可以的。
    我们先看一段英文描述,如下:
    When running a Spring Boot application that uses an embedded servlet container (and is packaged as an
    executable archive), there are some limitations in the JSP support.
    With Tomcat it should work if you use war packaging, i.e. an executable war will work, and will also be
    deployable to a standard container (not limited to, but including Tomcat). An executable jar will not work
    because of a hard coded file pattern in Tomcat.
    Jetty does not currently work as an embedded container with JSPs.
    Undertow does not support JSPs.
    原文的大体意思就是:Tomcat 支持 war 的打包方式,spring boot 支持 war 打包方式。Jetty 现在不支持
    JSP 嵌入容器。Undertow 根本就不支持 JSP。
    所以答案就是打包成 war,jsp 会自然按照 servlet 的标准部署。但也就意味着你不可以用嵌入式的方式运
    行,而是 Tomcat Server + war 的部署方式。
    看到这里有些网友肯定会有疑问那什么是嵌入式的 web 服务器?我们这边就拿 jetty 来说明下。
    Jetty 可以非常容易的嵌入到应用程序当中而不需要程序为了使用 Jetty 做修改。
    从某种程度上,你也可以把 Jetty 理解为一个嵌入式的 Web 服务器。所以我们经常会说嵌入式 jetty。
    Jetty 有一个口号:不要把你的应用部署到 Jetty 中,把 Jetty 部署到你的应用中。Jetty 可以在 Java 应用
    程序中向其他 POJO 一样被实例化,换句话说,以嵌入式的模式运行 Jetty 是指将 Http 模块放入你的应用程序
    中,而非部署你的程序到一个 HTTP 服务器。这就是所谓的嵌入式 jetty。
    另外在说明一点就是 JSP 解析是需要 JSP 引擎处理的,tomcat 就提供了 JSP 处理引擎。所以很显然 JSP
    是依赖容器而存在的,不然就没法访问了。那么既然是依赖于 tomcat 的话。
    有一网友找到一支持打成 jar 包运行的插件:
    Using Spring Boot with JSPs in Executable Jars
    https://github.com/ghillert/spring-boot-jsp-demo
    经过 java -jar xxx.jar 运行后,可以正常访问网页。
    这也可以说明原本是不支持的,但是如果非要支持的话,那么需要进行使用插件进行支持。
    <2>针对第二个问题
    对于第二个问题,如果看过之前的章节就很好解决了,只需要在 src/main/resouces 下新建一个 static 目
    录,然后在 static 下新建子目录:css,images,js 目录,在 images 放入一张 test.jpg 图片,那么访问路径是:
    http://127.0.0.1:8080/images/test.jpg
    当前目录结构应该是这样子的:
    (1)--src/java/resources
    (2)-- static
    (3)-- css
    (3)-- images
    (3)-- js
    那么有人会有疑问这个,打包的时候能打上嘛,答案是可以的,请看实际打包解压图:

     

  • 相关阅读:
    RabbitMQ概述
    hadoop生态圈面试精华之zookeeper(二)
    SAP-SD26-设定销售收入科目
    问题:EventSource 收不到流数据及 EventSource 的 onmessage 方法为null
    【软考 系统架构设计师】计算机组成与体系结构⑥ 流水线
    STM32CubeMX教程13 ADC - 单通道转换
    文献阅读1
    Redis中的Lua脚本(二)
    .NET6接入Skywalking链路追踪完整流程
    字符集和File类
  • 原文地址:https://blog.csdn.net/m0_72254454/article/details/127769058