SpringBoot Web開發


SpringBoot Web開發

jar:webapp!

自動裝配:創建應用,選擇模塊

springboot到底幫我們配置了什么?我們能不能進行修改?能修改哪些東西?能不能擴展?

  • xxxAutoConfiguration.. 向容器中自動配置組件
  • xxxProperties:自動配置類,裝配配置文件中自定義的一些內容

要解決的問題

  • 導入靜態資源 html css
  • 首頁
  • jsp,模板引擎 Thymeleaf
  • 裝備擴展 SpringMVC
  • 增刪改查
  • 攔截器
  • 國際化

靜態資源

WebMvcAutoConfiguration.java

public void addResourceHandlers(ResourceHandlerRegistry registry) {
	if (!this.resourceProperties.isAddMappings()) {
		logger.debug("Default resource handling disabled");
		return;
	}
	Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
	CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
	if (!registry.hasMappingForPattern("/webjars/**")) {
		customizeResourceHandlerRegistration(registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/").setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
	}
	String staticPathPattern = this.mvcProperties.getStaticPathPattern();
	if (!registry.hasMappingForPattern(staticPathPattern)) {
		customizeResourceHandlerRegistration(registry.addResourceHandler(staticPathPattern).addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations())).setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
	}
}
  • 第一種方法:添加webjars依賴拿到靜態資源
<dependency>
	<groupId>org.webjars</groupId>
	<artifactId>jquery</artifactId>
	<version>3.4.1</version>
</dependency>

  • 第二種方法

ResourceProperties.java 類定義了幾個資源地址的靜態全局變量

按照源碼新建文件夾,在該文件下的所有目錄都直接能被取到

  • 總結
  1. 在springboot,我們可以使用以下方式處理靜態資源
    • webjars: localhost:8080/webjars/
    • public,static,resource,/**,這些都在 localhost:8080/
  2. 優先級:resource>static(默認的)>public

首頁定制

看源碼,意思是在任意資源目錄添加 index.html 文件,我這里添加到了 public 文件夾下

啟動后

經測試,index.html 文件放到三個子目錄下都能生成主頁,直接放到上級的 resources 里不生效,一般主頁都放到 static 文件夾下

模板引擎 Thymeleaf

導入依賴

<!--thymeleaf 以后都是基於3.x開發-->
<dependency>
	<groupId>org.thymeleaf</groupId>
	<artifactId>thymeleaf-spring5</artifactId>
</dependency>
<dependency>
	<groupId>org.thymeleaf.extras</groupId>
	<artifactId>thymeleaf-extras-java8time</artifactId>
</dependency>

源碼

public class ThymeleafProperties {
   private static final Charset DEFAULT_ENCODING = StandardCharsets.UTF_8;
   public static final String DEFAULT_PREFIX = "classpath:/templates/";
   public static final String DEFAULT_SUFFIX = ".html";
   ...
}

從源碼看出,Thymeleaf 能取到 templates 目錄下的 .html 文件

結論:只要需要使用 thymeleaf,只需要導入對應的依賴就可以了!我們將html放在我們的 templates 目錄下即可!

<!DOCTYPE html>
<!--特定的聲明-->
<html lang="en" xmlns:th="http://www.thymeleaf.org"/>
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>test</h1>

<!--所有的htmL元素都可以被thymeleaf 替換接管 th:元素名-->
<div th:text="${msg}"></div>

</body>
</html>
@Controller
public class IndexController {

    @RequestMapping("/test")
    public String test(Model model){
        model.addAttribute("msg","hello,springboot");
        return "test";
    }
}

  • thymeleaf語法

測試

<!DOCTYPE html>
<!--特定的聲明-->
<html lang="en" xmlns:th="http://www.thymeleaf.org"/>
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>test</h1>

<!--所有的htmL元素都可以被thymeleaf 替換接管 th:元素名-->
<div th:text="${msg}"></div>
<!--utext表示轉義-->
<div th:utext="${msg}"></div>

<hr>

<!--遍歷取值-->
<h3 th:each="user:${users}" th:text="${user}"></h3>
<h3 th:each="user:${users}">[[${user}]]</h3><!--不建議使用-->

</body>
</html>
@Controller
public class IndexController {

    @RequestMapping("/test")
    public String test(Model model){
        model.addAttribute("msg","<h1>hello,springboot</h1>");
        model.addAttribute("users", Arrays.asList("peng1","peng2"));
        return "test";
    }
}

結果

更多語法







SpringMVC自動配置

自己擴展的mvc配置類 com.peng.config/WebMvcConfigurer.java

//如果,你想diy一些定制化的功能,只要寫這個組件,
//然后將它交給springboot, springboot會幫我們自動裝配!
//擴展  springmvc       dispatchservlet
@Configuration
public class MyConfig implements WebMvcConfigurer {

    //ViewResolver實現了視圖解析器按口的類,我們就可以把它看做視圖解析器
    @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;
        }
    }
}

在 springboot 中, 有非常多的xxxx Configuration幫助我們進行擴展配置,只要看見了這個東西,我們就要注意了!

一個例子:

	//視圖跳轉
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/peng").setViewName("test");
    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM