springboot themleaf

springboot themleaf,第1张

springboot themleaf

文章目录

如何阅读与分析源码1:如何导入静态资源2:访问html以及更改图标3:通过controller跳转到首页4:如何使用thymeleaf5:thymeleaf语法6:自动装配原理,告诉MVC我想定制一些什么。7:视图跳转
xxxxAutoConfiguration:向容器中自动配置组件

xxxxProperties:自动配置类,装配配置文件中自定义的一些内容。

如何阅读与分析源码

Ctrl+n表示搜索全局包括类

1:如何导入静态资源

引入web-jars与jQuery

   
            org.webjars
            jquery
            3.4.1
        

访问webjars下的路径

访问localhost:8081/webjars/jquery/3.1.4/jquery.js

可以存放静态资源的目录:

访问http://localhost:8081/1.js

所以访问静态资源可以在

webjars

resource/static/public/ /**

优先级:resources>static>public

2:访问html以及更改图标

只需要将index.html放入上述任意的文件中即可

更改图标:

找到图片将后缀改为a.ico,并将图标放入html同级目录

在html中引入

访问localhost:8081/index.html

3:通过controller跳转到首页

导入Thymeleaf

因为是2.6.2的springboot,所以导入的依赖为:

 
            org.springframework.boot
            spring-boot-starter-thymeleaf
        

选择目录创建html文件,并在controller中写上方法

访问链接

4:如何使用thymeleaf

1:需要在html开头导入约束


变量:${…}

选择表达式:*{}

message:#{}

链接 @{}

fragment:-{}

使用:如果爆红检查注解一定要是Controller

注ctrl+f5可以去除缓存

5:thymeleaf语法

1:转义识别标签 th:utext

2:遍历数组

model.attriubute(“user”,Array.aslist(“sheepbotany”,"woodwhale));//将字符串转化为数组

 

其他方式:

[[${users}]]

装配扩展:SpringMVC

· 简单表达式 (simple expressions)

${…} 变量表达式

*{…} 选择变量表达式

#{…} 消息表达式

@{…} 链接url表达式

· 字面量

‘one text’,‘another one!’,… 文本

0,34,3.0,12.3,… 数值

true false 布尔类型

null 空

one,sometext,main 文本字符

· 文本 *** 作

+ 字符串连接

|The name is ${name}| 字符串连接

· 算术运算

+ , - , * , / , % 二元运算符

- 负号(一元运算符)

· 布尔 *** 作

and,or 二元 *** 作符

!,not 非(一元 *** 作符)

· 关系 *** 作符

> , < , >= , <= (gt , lt , ge , le)

== , != (eq, ne)

· 条件判断

(if) ? (then) if-then

(if) ? (then) : (else) if-then-else

6:自动装配原理,告诉MVC我想定制一些什么。
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
    
    @Bean
    public ViewResolver myViewResolver(){
        return new MyViewResolver();
    }

    public static class MyViewResolver implements ViewResolver{

        @Override
        public View resolveViewName(String viewName, Locale locale) throws Exception {
            return null;
        }
    }

}

7:视图跳转
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {

   @Override
    public void addViewControllers(ViewControllerRegistry registry){
       registry.addViewController("/demo02").setViewName("test");
   }

}

figuration
public class MyMvcConfig implements WebMvcConfigurer {

@Override
public void addViewControllers(ViewControllerRegistry registry){
registry.addViewController("/demo02").setViewName(“test”);
}

}

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5712718.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-17
下一篇2022-12-17

发表评论

登录后才能评论

评论列表(0条)

    保存