230124_50_SpringBoot入门
- thymeleaf语法
-
1.th:utext,转义文本
- controller
model.addAttribute("msg","<h1>hello,springboot!</h1>");
- html
<div th:text="${msg}"></div>
<div th:utext="${msg}"></div>
测试结果:
- 2.遍历数据
- controller
model.addAttribute("users", Arrays.asList("bill","tom"));
- html
<h1 th:each="user:${users}" th:text="${user}"></h1>
测试结果
-
mvc配置原理
-
自定义配置类MyMvcConfig
package com.bill.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.Locale;
/**
* @Auther: wangchunwen
* @Date: 2023/1/24 - 01 - 24 - 20:39
* @Description: com.bill.config
* @version: 1.0
*/
// 如果需要一些定制化功能,只要写一个组件,然后将它交给springboot,springboot就会帮我们自动装配。
// 配置类,扩展springmvc
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
// ViewResolver 实现了视图解析器接口的类,我们就可以把它看做视图解析器
@Bean
public ViewResolver myViewResolver(){
return new MyViewName();
}
// 自定义一个自己的视图解析器myViewResolver
public static class MyViewName implements ViewResolver {
@Override
public View resolveViewName(String s, Locale locale) throws Exception {
return null;
}
}
}
-
扩展springmvc
-
自定义MyMvcConfig,视图跳转
package com.bill.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* @Auther: wangchunwen
* @Date: 2023/1/24 - 01 - 24 - 20:39
* @Description: com.bill.config
* @version: 1.0
*/
// 配置类,扩展springmvc
// 如果要扩展springmvc,自定义配置类
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/bill").setViewName("get");
}
}
测试结果: