09_07_第七阶段:微服务开发||01-SpringBoot||06springboot_Web开发【模板引擎Thymeleaf:替代JSP的模板引擎】【观看狂神随笔】

本文详细介绍了SpringBoot中如何处理静态资源,包括webjars、resources、static和public目录的优先级,并展示了如何自定义静态资源路径。同时,讲解了如何定制首页,引入Thymeleaf模板引擎进行页面渲染。此外,还探讨了SpringMVC的扩展,如自定义视图解析器和视图跳转。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SpringBoot Web开发

回顾:

自动装配

​ 1. 创建应用,选择模块

SpringBoot到底帮我们配置了什么?我们能不能进行修改?能修改那些东西?能不能扩展?

  • xxxAutoConfiguration->向容器中自动配置组件
    • 在spring.factories文件中
  • xxxProperties->自动配置类,装配配置文件中自定义的一些内容!

现在要解决的问题:

  • 导入静态资源…怎么导入?
  • 首页
  • jsp页面…模板引擎Thymeleaf
  • 装配和扩展SpringMVC
    • 视图解析器
  • 增删改查
  • 拦截器
  • 国际化【了解】
    • 实现中英文切换

一、静态资源

以下源码路径:
  • WebMvcAutoConfiguration【重点】—>addResourceHandlers(方法)—>WebProperties—>Resources(方法)
addResourceHandler(registry, "/webjars/**", "classpath:/META-INF/resources/webjars/");

private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/",
				"classpath:/resources/", "classpath:/static/", "classpath:/public/" };
  • 以上路径都可以用来存放静态资源webjars,resources,static,public,/**(Resources根目录下的)【会自动查询以上目录】
根据上面建立静态资源目录

在这里插入图片描述

优先级:

​ resources>static>public

访问:
  1. 在Public中创建一个1.js
  2. 访问http://localhost:8080/1.js即可
各个目录的用途:
  • public:一般放一些公共的资源(比如大家都会去访问的js)
  • static:里面放一些静态资源和首页(比如图片)
  • resources:放一些upload的上传文件
总结**:
  1. 在springboot,我们可以使用以下方式处理静态资源

    • webjars
      • 访问路径:localhost/8080/webjars/下面的目录结构
      • 比如:localhost:8080/webjars/jquery/3.6.0/jquery.js
    • resources,static,public,/**(Resources根目录下的)
      • 访问路径:localhost:8080/
  2. 优先级:resources>static(默认)>public

  3. 自定义静态资源(一旦使用了就会让原有的失效)——一般不使用

    • spring:
        mvc:
          static-path-pattern: /myUrl
      

二、首页如何定制

  • 注:在Resources/templates目录下的所有页面,只能通过Controller来进行跳转。
    • 这个需要模板引擎的支持【thymeleaf】

三、模板引擎【thymeleaf】

  • 模板引擎的作用就是我们来写一个页面模板
  • 以前用jsp,现在用thymeleaf

注:是前后端分离过程中的中间产物
在这里插入图片描述

结论:只要使用thymeleaf,只需要导入对应的依赖就可以了,我们将html放在我们的template目录下即可
  • <!--thymeleaf-->
    <dependency>
        <groupId>org.thymeleaf</groupId>
        <artifactId>thymeleaf-spring5</artifactId>
    </dependency>
    <dependency>
        <groupId>org.thymeleaf.extras</groupId>
        <artifactId>thymeleaf-extras-java8time</artifactId>
    </dependency>
    
如何使用thymeleaf
  1. 导入约束,命名空间

    • xmlns:th="http://www.thymeleaf.org">
      
    • 所有的html元素都可以被thymeleaf替换接管

      • th: 元素名
标准表达式语法
  • 
    Simple expressions:(表达式语法)
    Variable Expressions: ${...}:获取变量值;OGNL;
        1)、获取对象的属性、调用方法
        2)、使用内置的基本对象:#18
             #ctx : the context object.
             #vars: the context variables.
             #locale : the context locale.
             #request : (only in Web Contexts) the HttpServletRequest object.
             #response : (only in Web Contexts) the HttpServletResponse object.
             #session : (only in Web Contexts) the HttpSession object.
             #servletContext : (only in Web Contexts) the ServletContext object.
    
        3)、内置的一些工具对象:
          #execInfo : information about the template being processed.
          #uris : methods for escaping parts of URLs/URIs
          #conversions : methods for executing the configured conversion service (if any).
          #dates : methods for java.util.Date objects: formatting, component extraction, etc.
          #calendars : analogous to #dates , but for java.util.Calendar objects.
          #numbers : methods for formatting numeric objects.
          #strings : methods for String objects: contains, startsWith, prepending/appending, etc.
          #objects : methods for objects in general.
          #bools : methods for boolean evaluation.
          #arrays : methods for arrays.
          #lists : methods for lists.
          #sets : methods for sets.
          #maps : methods for maps.
          #aggregates : methods for creating aggregates on arrays or collections.
    ==================================================================================
    
      Selection Variable Expressions: *{...}:选择表达式:和${}在功能上是一样;
      Message Expressions: #{...}:获取国际化内容
      Link URL Expressions: @{...}:定义URL;
      Fragment Expressions: ~{...}:片段引用表达式
    
    Literals(字面量)
          Text literals: 'one text' , 'Another one!' ,…
          Number literals: 0 , 34 , 3.0 , 12.3 ,…
          Boolean literals: true , false
          Null literal: null
          Literal tokens: one , sometext , main ,…
          
    Text operations:(文本操作)
        String concatenation: +
        Literal substitutions: |The name is ${name}|
        
    Arithmetic operations:(数学运算)
        Binary operators: + , - , * , / , %
        Minus sign (unary operator): -
        
    Boolean operations:(布尔运算)
        Binary operators: and , or
        Boolean negation (unary operator): ! , not
        
    Comparisons and equality:(比较运算)
        Comparators: > , < , >= , <= ( gt , lt , ge , le )
        Equality operators: == , != ( eq , ne )
        
    Conditional operators:条件运算(三元运算符)
        If-then: (if) ? (then)
        If-then-else: (if) ? (then) : (else)
        Default: (value) ?: (defaultvalue)
        
    Special tokens:
        No-Operation: _
    
thymeleaf语法
  • 文档第十条里面

  • 在这里插入图片描述

  • 常用:

    • 遍历数据:

      • @RequestMapping("/test")
        public String test(Model model) {
            model.addAttribute("users", Arrays.asList("kami", "dog"));
            return "test";
        }
        
      • <!--遍历出来的每个元素,展现到文本上-->
        <!--第一种写法——推荐使用-->
        <h3 th:each="users : ${users}" th:text="${users}"></h3>
        <!--第二种写法-->
        <!--<h3 th:each="users : ${users}">[[${users}]]</h3>-->
        
      • 前端基本都用的是三元运算符来实现判断

四、装配和扩展SpringMVC【重点】

自定义视图解析器
  • //如果你想自定义一些定制化的功能,只要写这个组件,然后将它交给springboot,springboot就会自动装配
    //全面扩展  springMVC
    @Configuration
    public class MyMvcConfig implements WebMvcConfigurer {
    
        //ViewResolver  实现了视图解析器接口的类,我们就可以把它看做是视图解析器
        @Bean
        public ViewResolver MyViewResolver(){
            return new MyViewResolver();
        }
    
        //自定义了一个自己的试图解析器MyViewResolver(静态内部类)
        public static class MyViewResolver implements ViewResolver{
            @Override
            public View resolveViewName(String s, Locale locale) throws Exception {
                return null;
            }
        }
    }
    

重写页面跳转

  • //如果我们要扩展springMVC,官方建议我们这样做
    @Configuration
    //如果我们要扩展springMVC,不能加下面这个注解
    //@EnableWebMvc   //这玩意就是导入了一个DelegatingWebMvcConfiguration.class类(从容器中获取所有的webMvcConfig)
    public class MyMvcConfig implements WebMvcConfigurer {
    
        //视图跳转
        @Override
        public void addViewControllers(ViewControllerRegistry registry) {
            registry.addViewController("/kami").setViewName("test");
        }
    }
    

总结:

  • 在springboot中,有很多的xxx Configuration—>会帮助我们进行扩展配置,只要看到了这个东西,我们就要注意了!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

狂野小白兔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值