
???博主介绍:大家好,我是,一名在校大学生,热爱分享知识,很高兴在这里认识大家??
??擅长领域:Java、大数据、运维、电子
???如果本文章各位小伙伴们有帮助的话,??关注+???点赞+??评论+??收藏,相应的有空了我也会回访,互助!!!
??另本人水平有限,旨在创作简单易懂的文章,在文章描述时如有错,恳请各位大佬指正,在此感谢!!!
SpringBoot不支持JSP
Thymeleaf:现代化、服务端Java模板引擎
引入Thymeleaf支持
org.springframework.boot
spring-boot-starter-thymeleaf
基本语法:
文本值: **‘one text’ , ‘Another one!’ ,…**数字: **0 , 34 , 3.0 , 12.3 ,…**布尔值: true , false
空值: null
变量: one,two,… 变量不能有空格
字符串拼接: +
变量替换: |The name is ${name}|
运算符: + , - , * , / , %
运算符: and , or
一元运算: ! , not
比较: **> , < , >= , <= ( gt , lt , ge , le )**等式: == , != ( eq , ne )
If-then: (if) (then)
If-then-else: (if) (then) : (else)
Default: (value) : (defaultvalue)
无操作: _
注意:当文本不在一个标签中的时候·,需要使用行内写法
比如
John Doe
行内写法
[[${session.loginUser.userName}]]
视图解析
Thymeleaf使用
SpringBoot已经配置好了基本的Thymeleaf的属性
@Configuration(
proxyBeanMethods = false
)
@EnableConfigurationProperties({ThymeleafProperties.class})
@ConditionalOnClass({TemplateMode.class, SpringTemplateEngine.class})
@AutoConfigureAfter({WebMvcAutoConfiguration.class, WebFluxAutoConfiguration.class})
ThymeleafProperties.java
public class ThymeleafProperties {
private static final Charset DEFAULT_ENCODING;
public static final String DEFAULT_PREFIX = "classpath:/templates/";
public static final String DEFAULT_SUFFIX = ".html";
private boolean checkTemplate = true;
private boolean checkTemplateLocation = true;
private String prefix = "classpath:/templates/";
private String suffix = ".html";
private String mode = "HTML";
代码实现:
success.html
Welcome Page!
占位符1
进入百度!
进入百度!
ViewController.java
@Controller
public class ViweController {
/**
*将参数写入请求域中,发给模版引擎
* @param model
* @return 页面字符串和thymeleaf的前缀/template 以及后缀.html进行拼接
*/
@GetMapping("/aitu")
public String success(Model model){
model.addAttribute("msg","你好世界!");
model.addAttribute("link","https://www.baidu.com");
return "success";
}
}
公共代码块抽取-三种方式
首先选择使用footer标签抽取,还是使用选择器抽取
抽取:
选择器抽取
选择公共代码块使用的方式
- 将footer以及其内部内容放到div内部,也就是当前的div也包括
- 将footer以及其内部内容放到div所在的位置,div将没有
- 将footer内容放到当前的div中,包括div不包括footer
拦截器
定制拦截器需要实现HandlerInterceptor 接口
@Slf4j
public class LoginInterceptor implements HandlerInterceptor {
配置拦截器
@Configuration
public class WebConfig implements WebMvcConfigurer {
/**
拦截器原理

先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦