zoukankan      html  css  js  c++  java
  • SpringBoot(四) Web开发 --- Thymeleaf、JSP

    Spring Boot提供了spring-boot-starter-web为Web开发予以支持,spring-boot-starter-web为我们提供了嵌入的Tomcat以及Spring MVC的依赖。

    模板引擎

    Spring Boot支持多种模版引擎包括:

    • FreeMarker
    • Groovy
    • Thymeleaf(官方推荐)
    • Mustache

    JSP技术Spring Boot官方是不推荐的,原因有三:

    1. tomcat只支持war的打包方式,不支持可执行的jar。
    2. Jetty 嵌套的容器不支持jsp
    3. Undertow
    4. 创建自定义error.jsp页面不会覆盖错误处理的默认视图,而应该使用自定义错误页面

    Thymeleaf引擎模板

    Thymeleaf是一款用于渲染XML/XHTML/HTML5内容的模板引擎。类似JSP,Velocity,FreeMaker等,它也可以轻易的与Spring MVC等Web框架进行集成作为Web应用的模板引擎。与其它模板引擎相比,Thymeleaf最大的特点是能够直接在浏览器中打开并正确显示模板页面,而不需要启动整个Web应用。它的功能特性如下:

    • Spring MVC中@Controller中的方法可以直接返回模板名称,接下来Thymeleaf模板引擎会自动进行渲染
    • 模板中的表达式支持Spring表达式语言(Spring EL)
    • 表单支持,并兼容Spring MVC的数据绑定与验证机制
    • 国际化支持

    Spring官方也推荐使用Thymeleaf,所以本篇代码整合就使用Thymeleaf来整合。

    使用Thymeleaf

    1.加入依赖

     1 <dependency>
     2     <groupId>org.springframework.boot</groupId>
     3     <artifactId>spring-boot-starter-thymeleaf</artifactId>2.1.6</dependency>
     4 
     5 <properties>
     6         <thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
     7         <!-- 布局功能的支持程序  thymeleaf3主程序  layout2以上版本 -->
     8         <!-- thymeleaf2   layout1-->
     9         <thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version>
    10   </properties>

    只要我们把HTML页面放在classpath:/templates/,thymeleaf就能自动渲染;

    2.导入thymeleaf的名称空间

    <html lang="en" xmlns:th="http://www.thymeleaf.org">

    3.使用thymeleaf语法

    <!DOCTYPE html>
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
        <h1>成功!</h1>
        <!--th:text 将div里面的文本内容设置为 -->
        <div th:text="${hello}">这是显示欢迎信息</div>
    </body>
    </html>

     

    注:通过xmlns:th=”http://www.thymeleaf.org“ 命令空间,将静态页面转换为动态的视图,需要进行动态处理的元素将使用“th:”前缀。

    Thymeleaf的默认参数配置和表达式

    # THYMELEAF (ThymeleafAutoConfiguration)
    #开启模板缓存(默认值:true)
    spring.thymeleaf.cache=true 
    #Check that the template exists before rendering it.
    spring.thymeleaf.check-template=true 
    #检查模板位置是否正确(默认值:true)
    spring.thymeleaf.check-template-location=true
    #Content-Type的值(默认值:text/html)
    spring.thymeleaf.content-type=text/html
    #开启MVC Thymeleaf视图解析(默认值:true)
    spring.thymeleaf.enabled=true
    #模板编码
    spring.thymeleaf.encoding=UTF-8
    #要被排除在解析之外的视图名称列表,用逗号分隔
    spring.thymeleaf.excluded-view-names=
    #要运用于模板之上的模板模式。另见StandardTemplate-ModeHandlers(默认值:HTML5)
    spring.thymeleaf.mode=HTML5
    #在构建URL时添加到视图名称前的前缀(默认值:classpath:/templates/)
    spring.thymeleaf.prefix=classpath:/templates/
    #在构建URL时添加到视图名称后的后缀(默认值:.html)
    spring.thymeleaf.suffix=.html
    #Thymeleaf模板解析器在解析器链中的顺序。默认情况下,它排第一位。顺序从1开始,只有在定义了额外的TemplateResolver Bean时才需要设置这个属性。
    spring.thymeleaf.template-resolver-order=
    #可解析的视图名称列表,用逗号分隔
    spring.thymeleaf.view-names=
    Simple expressions:(表达式语法)
    a. Variable Expressions: ${...}:获取变量值;OGNL;
    b. Selection Variable Expressions: *{...}:选择表达式:和${}在功能上是一样;
            补充:配合 th:object="${session.user}:
            <div th:object="${session.user}">
            <p>Name: <span th:text="*{firstName}">Sebastian</span>.</p>
            <p>Surname: <span th:text="*{lastName}">Pepper</span>.</p>
            <p>Nationality: <span th:text="*{nationality}">Saturn</span>.</p>
            </div>
        
    c. Message Expressions: #{...}:获取国际化内容
    d. Link URL Expressions: @{...}:定义URL;
                @{/order/process(execId=${execId},execType='FAST')}
    f. Fragment Expressions: ~{...}:片段引用表达式
                <div th:insert="~{commons :: main}">...</div>

    SpringBoot  -- JSP引擎模板

    在项目下新建一个webapp目录,webapp这个用来存放jsp的目录,静态资源还是放在resources的static下面。 

    1.加入依赖

    <!--WEB支持-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
    <!--jsp页面使用jstl标签-->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>
    
    <!--用于编译jsp-->
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>

    2.application.properties配置

    spring.mvc.view.prefix: /WEB-INF/jsp/
    spring.mvc.view.suffix: .jsp
    
    #配置程序端口,默认为8080
    server.port= 8080
    #用户绘画session过期时间,以秒为单位
    server.session.timeout=
    # 配置默认访问路径,默认为/
    server.context-path=
    
    
    # 配置Tomcat编码,默认为UTF-8
    server.tomcat.uri-encoding=UTF-8
    # 配置最大线程数
    server.tomcat.max-threads=1000

    3.添加pom.xml配置

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
  • 相关阅读:
    状态压缩 + 暴力 HDOJ 4770 Lights Against Dudely
    简单几何(推公式) UVA 11646 Athletics Track
    简单几何(四边形形状) UVA 11800 Determine the Shape
    简单几何(求交点) UVA 11437 Triangle Fun
    计算几何模板
    简单几何(相对运动距离最值) UVA 11796 Dog Distance
    简单几何(求划分区域) LA 3263 That Nice Euler Circuit
    覆盖的面积 HDU
    Desert King 最小比率生成树 (好题)
    约会安排 (区间合并)毒瘤题
  • 原文地址:https://www.cnblogs.com/JiangLai/p/9968466.html
Copyright © 2011-2022 走看看