SpringBoot-05 Web開發

SpringBoot-05 Web開發

1

2

靜態資源

要解決的第一個問題,靜態資源存放問題,靜態資源放在哪兒能查找到。html

首先查看WebMvcAutoConfiguration.class(能夠直接全局查找)前端

protected void addResourceHandlers(ResourceHandlerRegistry registry) {
    
        //是否有自定義配置,有的話這個方法失效
    super.addResourceHandlers(registry);
    if (!this.resourceProperties.isAddMappings()) {
        logger.debug("Default resource handling disabled");
    } else {
        
        //添加這個webjars,添加以後路徑爲:classpath:/META-INF/resources/webjars/
        ServletContext servletContext = this.getServletContext();
        this.addResourceHandler(registry, "/webjars/**", "classpath:/META-INF/resources/webjars/");
        
        this.addResourceHandler(registry, this.mvcProperties.getStaticPathPattern(), (registration) -> {
            //其他能夠識別的靜態資源位置
            registration.addResourceLocations(this.resourceProperties.getStaticLocations());
            if (servletContext != null) {
                registration.addResourceLocations(new Resource[]{new ServletContextResource(servletContext, "/")});
            }

        });
    }
}

那麼,什麼是webjarsjava

WebJars是一個很神奇的東西,可讓你們以jar包的形式來使用前端的各類框架、組件。WebJars是將客戶端(瀏覽器)資源(JavaScript,Css等)打成jar包文件,以對資源進行統一依賴管理。jquery

3

個人理解就是:以 依賴的形式導入前端資源git

結合上面的路徑,咱們導入一個jqury嘗試:web

<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>jquery</artifactId>
    <version>3.5.1</version>
</dependency>
4

能夠看出 一一對應,也能夠正常訪問。spring

5

除了上面這些,還有其餘能夠識別靜態資源的位置:瀏覽器

//其他能夠識別的靜態資源位置
registration.addResourceLocations(this.resourceProperties.getStaticLocations());//getStaticLocations()  點擊
if (servletContext != null) {
    registration.addResourceLocations(new Resource[]{new ServletContextResource(servletContext, "/")});
}

public String[] getStaticLocations() {
    return this.staticLocations;  //staticLocations  點擊
}

 private static final String[] CLASSPATH_RESOURCE_LOCATIONS = 
 new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};
              //  出現了路徑

6

  • 在springboot,咱們可使用一下方式處理靜態資源
    • webjars --------> localhost:8080/webjars/
    • public , static , /** , resources ------>localhost:8080/
  • 優先級 : resources > static > public,能夠按照本身的需求,放入文件

至於 templates文件夾,其中的文件只能用 Controller 控制類,才能夠進入。springboot

首頁設置

在SpringBoot中,默認自動識別在靜態資源文件夾下的 index.htmlmvc

注意:

  • 靜態資源文件夾爲:
    • public
    • static
    • resources
    • 優先級:resources > static > public
  • 默認的首頁文件爲:index.html,不要弄錯。

下面測試放在了public,其他方法你們能夠自行測試:

7

8

Thymeleaf模板引擎

1.導入依賴

模板引擎有什麼用呢? 可讓你使用 Controller類 調用頁面,沒有Thymeleaf就是不能夠。

首先!!,最重要的是導入這兩個依賴:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
    <version>2.4.3</version>
</dependency>

緣由:本身建立的SpingBoor項目,自帶的Thymeleaf版本爲2.xxx,而咱們要使用的這個版本爲3.xx

你們導入依賴後肯定是否成功:

9

10

以後更改一下pom.xml中的配置:

<properties>
    <java.version>1.8</java.version>
    <!-- 加入下面這兩句 -->
    <thymeleaf.version>3.0.11.RELEASE</thymeleaf.version>
    <thymeleaf-layout-dialect.version>3.0.4</thymeleaf-layout-dialect.version>
</properties>

2.Controller類測試

建立一個Controller類

注意:必定要在 xxxApplication啓動器的同級目錄下建立,否則不可能成功!!(踩了30分鐘的坑)

13
@Controller
public class ThymeleafController {
    @RequestMapping("/a")
    public String test(){
        return "tt";
    }
}

建立一個html頁面

注意:SpringBoot內部規定要在templates文件夾下建立xxx.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>ThymeleafController跳轉</h1>
</body>
</html>

以後運行測試:

12

3.Thymeleaf語法

14

th:text

修改controller類,使用model傳一個數據:

@RequestMapping("/a")
public String test(Model model){
    model.addAttribute("msg","Thymeleaf語法測試");
    return "tt";
}

修改html:

<!DOCTYPE html>
<html lang="en"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>ThymeleafController跳轉</h1><br> 
    <!--以往都是直接${msg},在Thymeleaf中須要如下使用方法:-->
    <!--以前html能夠用的標籤,都變爲:  th:屬性名  -->
    <div th:text="${msg}"></div>
</body>
</html>
13

th:utext

修改controller類,使用model傳一個數據:

@RequestMapping("/a")
public String test(Model model){
    model.addAttribute("msg","<h2>Thymeleaf語法測試</h2>");
    return "tt";
}

修改html:

<!DOCTYPE html>
<html lang="en"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>ThymeleafController跳轉</h1><br>
    <div th:text="${msg}"></div><br>
    <div th:utext="${msg}"></div><br>
</body>
</html>
15

th:each

修改controller類,使用model傳一個數據:

@RequestMapping("/a")
public String test(Model model){
    model.addAttribute("users", Arrays.asList("user1","user2","user3"));
    return "tt";
}

修改html:

<!DOCTYPE html>
<html lang="en"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>ThymeleafController跳轉</h1><br>
<!--    兩種寫法,推薦第一種   -->
    <h3 th:each="user:${users}" th:text="${user}"></h3>
    <div th:each="user:${users}" >[[ ${user} ]]</div>
</body>
</html>
16

擴展SpringMVC

1.MVC擴展原理

diy視圖解析器

@Configuration
public class MyMvcConfig {

    @Bean
    public ViewResolver MyViewResolver(){
        return new MyViewResolver();
    }


    public static class MyViewResolver implements ViewResolver{

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

2.SpringMVC擴展

@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
//        這句話的意思就是訪問 ...8080/test, 跳轉到tt.html頁面
        registry.addViewController("/test").setViewName("tt");
    }
}

在SpringBoot中,有很是多的 xxxConfiguration 幫助咱們進行擴展配置,只要看見了這個東西,咱們就要注意了,多是增長了一些新的功能。

我的博客爲:
MoYu's HomePage
MoYu's Gitee Blog

相關文章
相關標籤/搜索