码迷,mamicode.com
首页 > 编程语言 > 详细

SpringBoot Web开发

时间:2020-07-11 16:46:07      阅读:62      评论:0      收藏:0      [点我收藏+]

标签:地址   mvc   jquer   mvcc   问题   html   coding   loading   his   

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");
    }

SpringBoot Web开发

标签:地址   mvc   jquer   mvcc   问题   html   coding   loading   his   

原文地址:https://www.cnblogs.com/peng8098/p/java_22.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!