学习来源–>B站 传送门–> 【狂神说Java】SpringBoot最新教程IDEA版通俗易懂
文章目录- 用springboot进行web开发
- 1.1关于静态资源的导入与访问
- 1.2关于首页和图标定制
- 1.3thymeleaf 模板引擎
- 语法
- 1.4 spring MVC的装配
- 1.5 springmvc的扩展
文档–>2.5.6文档
用springboot进行web开发在之前的学习,大概了解到
其中的自动装配时;xxxAutoConfigurationxx去完成向容器中配置组件,
xxxproperties去自动配置类;装配配置文件;
新建项目;
选择web模块;
首先测试项目的配置;
创建controller作为控制层;
创建DemoController类
package com.lzq.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DemoController {
//处理get请求;
@GetMapping("/demo")
public String demo(){
return "正在测试显示.......";
}
}
启动主程序,访问http://localhost:8080/demo;测试成功
1.1关于静态资源的导入与访问访问方式;
(1)localhost:8080/webjars/…
(2)localhost:8080 @Controller public class GetDemoController { @GetMapping("/demo0") public String getDemo(){ return "demo0"; } }启动项目主程序;访问http://localhost:8080/demo0
那么,实际上这个thymeleaf如何使用呢;
首先需要加入约束;xmlns:th="http://www.thymeleaf.org"案例
先在controller中处理时,存入一个参数;
还是在刚才的类中package com.lzq.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @Controller public class GetDemoController { @GetMapping("/demo0") public String getDemo(Model model){ //存入参数; model.addAttribute("message","你好!!!!"); return "demo0"; } }demo0.html页面取值;
测试模板 测试模板文件夹下的文件显示
显示信息-->启动项目,存入的参数取到了
语法th:text 不转义;显示得到的数据;
th:utext 会进行转义比如说,我现在在controller写处理请求时,响应的参数赋值时拼接了标签
package com.lzq.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @Controller public class GetDemoController { @GetMapping("/demo0") public String getDemo(Model model){ //存入参数; model.addAttribute("message","你好!!!!"); return "demo0"; } }然后我分别按转义和不转义的方式取值;
测试模板 测试模板文件夹下的文件显示
显示信息(不转义)--> 显示信息(进行转义)-->重启项目,
关于遍历取值
现在GetDemoController类中定义方法;
package com.lzq.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import java.util.Arrays; @Controller public class GetDemoController { @GetMapping("/demo1") public String getDemo1(Model model){ //存入参数; model.addAttribute("lists", Arrays.asList("小智","小明","小杰","阿猫")); return "demo1"; } }在templates下定义demo1.html文件
测试遍历 启动项目;访问http://localhost:8080/demo1
1.4 spring MVC的装配文档–>2.5.6文档
实现接口 ViewResolver 的类,就可当做视图解析器;
比如说;ContentNegotiatingViewResolver重写该方法;
获取候选视图的大概实现getCandidateViews()
这个获取较好视图的实现也差不多;
找到DispatcherServlet类
在它的方法doDispatch处打个断点创建config包;创建DemoMVCConfig类
package com.lzq.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; //在这个类里面去扩展spring mvc @Configuration public class DemoMVCConfig implements WebMvcConfigurer { //实现接口 ViewResolver 的类,就可当做视图解析器; //配置注入到容器中; @Bean public ViewResolver myViewResolver(){ return new MyViewResolver(); } //自定义视图解析器;静态内部类的方式; public static class MyViewResolver implements ViewResolver{ @Override public View resolveViewName(String viewName, Locale locale) throws Exception { return null; } } }debug调试启动项目,访问http://localhost:8080/
回到IDEA,看看控制台,点入this;自定义的视图解析器有显示被加载了
OK,测试完,记得把源码中打的断点去掉;
1.5 springmvc的扩展在WebMvcProperties类中;
可看到它的内部类Format ;时间日期都有默认的格式实际上,也可以在application.properties配置文件中进行更改;
比如说:# 更改日期格式 spring.mvc.format.date=dd/MM/yyyy
注意在扩展时;不要使用注解@EnableWebMvc
因为该注解实际上导入了DelegatingWebMvcConfiguration类而DelegatingWebMvcConfiguration这个类继承了WebMvcConfigurationSupport
这个WebMvcConfigurationSupport和这些有什么关系呢?
注意–>再次回到WebMvcAutoConfiguration类;
有一个很关键的条件@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
什么意思呢?
就是说如果WebMvcConfigurationSupport这个类没有被注入到容器中时;WebMvcAutoConfiguration类下的配置才会生效;
要是这个类存在了;那么自动配置就失效了;所以呢,你要是正在自定义配置扩展时;用了注解@EnableWebMvc;它默认导入了DelegatingWebMvcConfiguration,它引发了WebMvcConfigurationSupport类–>导致WebMvcAutoConfiguration出问题!!!
OK,最后简单地试试扩展一个视图跳转吧;
在templates下创建re0.html文件
RE0 你来啦先把之前写的DemoMVCConfig类注释掉;
在config包下创建Demo2MVCConfig类;package com.lzq.config; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class Demo2MVCConfig implements WebMvcConfigurer { //添加视图跳转; @Override public void addViewControllers(ViewControllerRegistry registry) { //添加视图控制; 且设置视图名 registry.addViewController("/xiaozhi").setViewName("re0"); } }启动项目;访问http://localhost:8080/xiaozhi



