当前位置: 首页 > news >正文

网站建设 图片问题asp源码-漂亮企业源码大气公司网站模版

网站建设 图片问题,asp源码-漂亮企业源码大气公司网站模版,个体户可以备案网站吗,appache wordpressSpringBoot3-Web开发 SpringBoot的Web开发能力#xff0c;由SpringMVC提供。 Web开发的三种方式 方式处理过程注意事项实现效果全自动直接编写控制逻辑全部使用自动给配置默认效果手自一体Configuration、 配置WebMvcConfigurer、 配置WebMvcRegistrations不要标注 EnableWeb…SpringBoot3-Web开发 SpringBoot的Web开发能力由SpringMVC提供。 Web开发的三种方式 方式处理过程注意事项实现效果全自动直接编写控制逻辑全部使用自动给配置默认效果手自一体Configuration、 配置WebMvcConfigurer、 配置WebMvcRegistrations不要标注 EnableWebMvc手动配置效果 手动设置部分功能 定义MVC底层组件全手动Configuration 配置WebMvcConfigurer标注EnableWebMvc禁用自动配置效果 全手动设置 给容器中放一个配置类使用Configuration标注让配置类继承WebMvcConfigurer但是不要标注EnableWebMvc注解可以实现手自一体的效果。 1、Web场景 自动配置 ①整合web场景 dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId /dependency ②starter的spring-boot-starter引入了autoconfigure功能 ③EnableAutoConfiguration注解使用Import(AutoConfigurationImportSelector.class)批量导入组件加载META-INF/spring/%s.imports文件中配置的所有组件。 ④在自动导入的配置类中绑定了配置文件的一堆配置项比如 SpringMVC的所有配置以spring.mvc开头 Web场景通用的配置在spring.web 文件上传配置在spring.servlet.multipart 服务器的配置server比如编码方式 默认效果 默认配置 包含了ContentNegotiatingViewResolver和BeanNameViewResolver组件方便视图解析。 默认的静态资源处理机制静态资源放在static文件夹下即可直接访问。 自动注册了Converter,GenericConverter, Formatter组件适配常见的数据类型转换和格式化需求。 支持HttpMessageConverters,可以方便返回json等数据类型。 注册MessageCodesResolver,方便国际化及错误消息处理。 支持静态index.html。 自动使用ConfigurableWebBindingInitializer实现消息处理、数据绑定、类型转换、数据校验等功能。 特别提示 ①如果想保持boot mvc的默认配置并且自定义更多的mvc配置如interceptors,formatters, view controllers等可以使用Configuration注解添加一个WebMvcConfigurer类型的配置类但不要标注EnableWebMvc。 ②如果想保持boot mvc的默认配置但要自定义核心组件实例比如 RequestMappingHandlerMapping, RequestMappingHandlerAdapter或ExceptionHandlerExceptioonResolver,给容器中放一个WebMvcRegistrations组件即可。 ③如果想全面接管Spring MVCConfiguration标注一个配置类并加上EnableWebMvc注解实现WebMvcConfigurer接口。 2、静态资源 WebMvcAutoConfiguration 生效条件 效果 放入了两个filter HiddenHttpMethodFilter页面表单提交Rest请求(GET、POST、PUT、DELETE) FormContentFilter表单内容FilterGET(数据放URL后面)、POST(数据放请求体)请求携带数据但是PUT和DELETE的请求数据会被忽略FormContentFilter是为了解决这个问题的。 Bean ConditionalOnMissingBean(HiddenHttpMethodFilter.class) ConditionalOnProperty(prefix spring.mvc.hiddenmethod.filter, name enabled) public OrderedHiddenHttpMethodFilter hiddenHttpMethodFilter() {return new OrderedHiddenHttpMethodFilter(); } ​ Bean ConditionalOnMissingBean(FormContentFilter.class) ConditionalOnProperty(prefix spring.mvc.formcontent.filter, name enabled, matchIfMissing true) public OrderedFormContentFilter formContentFilter() {return new OrderedFormContentFilter(); } 给容器中放入了WebMvcConfigurer组件给SpringMVC添加各种定制功能所有的功能最终会和配置文件进行绑定 WebMvcProperties以spring.mvc开头的配置文件 WebProperties以spring.web开头的配置文件 Configuration(proxyBeanMethods false) Import(EnableWebMvcConfiguration.class)//额外导入了其他配置 EnableConfigurationProperties({ WebMvcProperties.class, WebProperties.class }) Order(0) public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer, ServletContextAware { } WebMvcConfigurer接口 它提供配置SpringMVC底层的所有组件入口。 静态资源规则源码 Override public void addResourceHandlers(ResourceHandlerRegistry registry) {if (!this.resourceProperties.isAddMappings()) {logger.debug(Default resource handling disabled);return;}addResourceHandler(registry, this.mvcProperties.getWebjarsPathPattern(),classpath:/META-INF/resources/webjars/);addResourceHandler(registry, this.mvcProperties.getStaticPathPattern(), (registration) - {registration.addResourceLocations(this.resourceProperties.getStaticLocations());if (this.servletContext ! null) {ServletContextResource resource new ServletContextResource(this.servletContext, SERVLET_LOCATION);registration.addResourceLocations(resource);}}); } 规则一 访问/webjars/**路径就去classpath:/META-INF/resources/webjars/下找资源(查看依赖中的内容) 规则二 规则二 访问/**路径就去静态资源的四个默认位置下找资源 classpath:/META-INF/resources/ classpath:/resources/ classpath:/static/ classpath:/public/ registration.addResourceLocations(this.resourceProperties.getStaticLocations() 规则三 静态资源默认都有缓存规则的设置 i、cachePeriod:缓存周期(以s为单位)多久不再需要给服务器重新请求 ii、cacheControlHTTP缓存控制 iii、useLastModified是否使用最后一次修改。配合HTTP cache规则 浏览器访问了一个静态资源如果这个服务没有发生变换下次访问的时候可以直接让浏览器用自己缓存中的内容而不用给服务器发请求。 registration.setCachePeriod(getSeconds(this.resourceProperties.getCache().getPeriod())); registration.setCacheControl(this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl()); registration.setUseLastModified(this.resourceProperties.getCache().isUseLastModified()); EnableWebMvcConfiguration源码 属于WebMvcAutoConfiguration的一个内部类。 //SpringBoot 给容器中WebMvcConfigurationSupport组件 //如果我们自己放了WebMvcConfigurationSupport组件Boot的WebMvcAutoConfiguration都会失效 Configuration(proxyBeanMethods false) EnableConfigurationProperties(WebProperties.class) public static class EnableWebMvcConfiguration extends DelegatingWebMvcConfiguration implements ResourceLoaderAware { ​ } HandlerMapping:根据请求路径找哪个handler处理请求。 WelcomePageHandlerMapping: 问/**路径下的所有请求都在以前四个静态资源路径下找欢迎页也是。 找index.html只要静态资源的位置有一个index.html页面项目启动就默认访问。 在容器中加入WebMvcConfigurer就能配置底层行为的原因 WebMvcConfiguration是一个自动配置类它里面有一个EnableWebMvcConfiguration EnableWebMvcConfiguration继承于DelegatingWebMvcConfiguration,这两个都生效 DelegatingWebMvcConfiguration利用DI把容器中所有WebMvcConfiguration注入进来 别人调用DelegatingWebMvcConfiguration的方法配置底层规则而它调用所有WebMvcConfiguration的配置底层方法。 默认规则 ①静态资源映射 静态资源映射规则在WebMvcAutoConfiguration中进行了定义 /webjars/**的所有路径资源都在classpath:/META-INF/resources/webjars /**的所有路径资源都在classpath:/META/resources/、classpath:/resources/、calsspath:/static/、calsspath:/public/ 所有静态资源都定义了缓存规则。浏览器访问过一次就会缓存一段时间但此功能参数无默认值 period:缓存间隔。默认0s; cacheControl缓存控制。默认无 useLastModified是否使用lastModified头。默认是false ②静态资源缓存 如①所述所有静态资源都定义了缓存规则。浏览器访问过一次就会缓存一段时间但此功能参数无默认值 测试缓存机制 在calsspath/static下放置一个静态资源(图片) 配置application.properties #1、spring.web # 配置国际化的区域信息 #配置静态资源策略(开启、处理链、缓存) #开启静态资源映射规则 spring.web.resources.add-mappingstrue #设置缓存 spring.web.resources.cache.period3600 #缓存详细合并项控制覆盖period配置 # 浏览器第一次请求服务器服务器告诉浏览器此资源缓存7200秒7200秒以内的所有此资源访问不用发给服务器请求 spring.web.resources.cache.cachecontrol.max-age7200 #使用资源last-modified时间来对此服务器和浏览器的资源是否相同相同返回304 spring.web.resources.cache.use-last-modifiedtrue 注意 spring.web.resources.cache.period 是用来设置静态资源在浏览器中的缓存过期时间。这个配置项告诉浏览器在多长时间内可以使用缓存的资源而不需要向服务器发送请求。在这个例子中缓存过期时间为1小时。 spring.web.resources.cache.cachecontrol.max-age 是用来设置服务器告诉浏览器此资源的缓存时间。这个配置项告诉浏览器在多长时间内可以使用缓存的资源而不需要向服务器发送请求。在这个例子中服务器会告诉浏览器缓存时间为2小时。 自定义静态资源规则 自定义静态资源路径自定义缓存规则 ①配置application.properties方式 #2、spring.mvc ##2.1.自定义webjars路径前缀 spring.mvc.webjars-path-pattern/webjars/** ##2.2.静态资源访问路径前缀 spring.mvc.static-path-pattern/** #静态资源的文件夹 spring.web.resources.static-locationsclasspath:/a/, classpath:/b/ spring.mvc:静态资源访问前缀路径 spring.web:静态资源目录、静态资源缓存策略 ②代码方式 容器中只要有一个WebMvcConfigurer组件。配置的底层行为都会生效。 Configuration//这是一个配置类 public class MyConfig implements WebMvcConfigurer {Beanpublic WebMvcConfigurer webMvcConfigurer(){return new WebMvcConfigurer() {Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {//保留之前的配置WebMvcConfigurer.super.addResourceHandlers(registry);//自己添加一些registry.addResourceHandler(static/**).addResourceLocations(classpath:/static/, classpath:/templates/).setCacheControl(CacheControl.maxAge(7200, TimeUnit.SECONDS));}};} } ③欢迎页 欢迎页规则在WebMvcAutoConfiguration中进行了定义 a、在静态资源目录下找index.html b、没有就在templates下找index模板页 ④Favicon favicon.ico 通常显示在浏览器标签页。在这段代码中在静态资源目录下寻找名为 favicon.ico 的文件。 3、路径匹配 Spring5.3之后加入了更多的请求路径匹配的实现策略。以前只支持AntPathMatcher策略现提供了PathPatternParser策略。并且可以让我们指定到底使用哪种策略。 ①Ant风格路径语法 ant风格的路径模式语法具有以下规则 规则含义*表示任意的0个或多个字符表示任意一个字符**表示任意数量的目录{}表示一个命名的模式占位符[]表示字符集合例如[a-z]表示小写字母 示例 *.html:匹配任意名称扩展名为.html的文件 /folder1/*/*.java匹配在folder1目录下的任意两级目录下的.java文件 /folder2/**/*.jsp匹配在folder2目录下任意目录深度的.jsp文件 /{type}/{id}.html匹配任意文件名为{id}.html在任意命名的{type}目录下的文件 注意: Ant风格的路径模式语法中的特殊特殊字符需要转义如 要匹配文件路径中的星号则需要转义为\* 要匹配文件路径中的问好则需要转义为\? ②模式切换 AntPathMatcher与PathPatternParser是Spring框架中用于处理路径模式匹配的工具类。 PathPatternParser在jmh基准测试下有6~8被吞吐量提升降低30%~40%的空间分配率 PathPatternParser兼容AntPathMatcher语法并支持更多类型的路径模式 PathPatternParser**多端匹配的支持仅允许在模式末尾使用Slf4j RestController public class HelloController {/*** springBoot默认使用新版的路径匹配(PathPatternParser)不能匹配双星在中间的情况剩下的和AntPathMatcher使用方法相同可以在配置文件中使用spring.mvc.pathmatch.matching-strategyant_path_matcher改为老版策略* param request* param path* return*/GetMapping(/a*/b?/{p1:[a-f]}/**)public String hello(HttpServletRequest request, PathVariable(p1) String path){log.info(路径变量p1:{}, path);String url request.getRequestURI();return url;} } 总结 使用默认的路径匹配规则是由PathPatternParser提供的 如果路径中间出现问题需要有**替换成Ant风格路径。 4、内容协商 一套系统适配多端数据返回。 ①多端内容适配 默认规则 基于请求头的内容协商(默认开启) 客户端向服务端发送请求携带HTTP标准的Accept请求头Accept:application/json、text/xml、text/yml 服务端根据客户端请求头期望的数据类型进行动态返回。 基于请求参数内容协商(需要手动开启) a、发送请求GET/projects/spring-boot?formatjson b、匹配到GetMapping(/projects/spring-boot) c、根据参数协商优先返回json类型数据(需要开启参数匹配设置) d、发送请求GET/projects/spring-boot?formatxml,优先返回xml类型数据。 效果演示 场景 请求同一个接口可以返回json和xml不同格式的数据使用官方Postman进行测试。 实体类Person Data NoArgsConstructor AllArgsConstructor public class Person {private Long id;private String name;private String email;private Integer age; } Controller 返回JSON格式 Slf4j RestController public class HelloController {/*** json适配默认支持把对象转化为json,web场景默默人导入了jackson处理json的包jackson-core* jackson也支持把数据转化为xml。但需要导入xml相关依赖* return*/GetMapping(/person)public Person person(){Person person new Person();person.setId(1L);person.setName(Louie);person.setEmail(xxxqq.com);person.setAge(23);return person;} } 返回XML格式 ①引入支持xml内容的依赖 dependencygroupIdcom.fasterxml.jackson.dataformat/groupIdartifactIdjackson-dataformat-xml/artifactId /dependency ②实体类中标注注解 JacksonXmlRootElement//可以写出为xml文档 Data NoArgsConstructor AllArgsConstructor public class Person {private Long id;private String name;private String email;private Integer age; } 注意 在浏览器中不能正常显示可以将浏览器的Accept复制在Postman中新加一个Accept关闭原来的Accept 配置协商规则与支持类型 修改内容协商方式 #开启基于请求参数的内容协商功能。默认参数名是format spring.mvc.contentnegotiation.favor-parametertrue #自定义参数名默认为format spring.mvc.contentnegotiation.parameter-namemyParam 效果 ②内容协商原理-HttpMessageConverter 熟悉HttpMessageConverter怎么工作,何时工作,就可以定制HttpMessageConverter来实现多端内容协商。 实现 编写WebMvcConfigurer提供的configureMessageConverters底层修改底层的MessageConverter ResponseBody由HttpMessageConverter处理标注了ResponseBody的返回值将会由支持它的HttpMessageConverter写给浏览器。 原理 a、controller方法的返回值标注了ResponseBody注解 b、请求进来先来到DispartureServlet的doDispatch()进行处理 c、找到HandlerAdapter适配器 d、利用适配器执行目标方法RequestMappingHandlerAdapter来执行 e、调用involeHandlerMethod执行目标方法。目标方法执行之前HandlerMethodArgumentResolver(参数解析器)确定目标方法每一个参数值。HandlerMethodReturnValueHandler(返回值处理器)确定目标方法的返回值该增么处理。 f、incokeAndHandle()真正执行目标方法。目标方法执行完成会导出返回值对象。 g、找到一个合适的返回值处理器HandlerMethodReturnValueHandler h、最终找到RequestResponseBodyMethodProcessor能处理标注了RsponseBody注解的方法 i、RequestResponseBodyMethodProcessor调用writerWithMessageConverters,利用MessageConverter把返回值写出去。 总结 ResponseBody由HttpMessageConverter处理HttpMessageConverter会先进行内容协商。 WebMvcAutoConfiguration通过addDefaultHttpMessageConverters添加了默认的MessageConverter如下 ByteArrayHttpMessageConverter支持字节数据读取 StringHttpMessageConverter支持字符串读写 ResourceHttpMessageConverter:支持资源读写 ResourceRegionHttpMessageConverter支持分区资源写出 AllEncompassingFormHttpMessageConverter支持表单xml/json读写 MappingJackson2HttpMessageConverter支持请求响应Json读写 遍历所有的MessageConverter看谁支持这种内容类型的数据,将支持的类型写出。 系统默认的MessageConverter功能有限仅用于json或普通返回数据。额外增加新的内容协商功能必须增加新的HttpMessageConverter。 ③WebMvcConfigurationSupport 该类提供了很多的默认设置。判断系统中是否有相应的类如果有就加入相应的HttpMessageConverter jackson2Present ClassUtils.isPresent(com.fasterxml.jackson.databind.ObjectMapper, classLoader) ClassUtils.isPresent(com.fasterxml.jackson.core.JsonGenerator, classLoader);jackson2XmlPresent ClassUtils.isPresent(com.fasterxml.jackson.dataformat.xml.XmlMapper, classLoader);jackson2SmilePresent ClassUtils.isPresent(com.fasterxml.jackson.dataformat.smile.SmileFactory, classLoader);jackson2CborPresent ClassUtils.isPresent(com.fasterxml.jackson.dataformat.cbor.CBORFactory, classLoader); ④自定义内容返回 场景增加yaml返回支持 步骤 导入依赖 dependencygroupIdcom.fasterxml.jackson.dataformat/groupIdartifactIdjackson-dataformat-yaml/artifactId /dependency 把对象写出成一个yaml Slf4j RestController public class HelloController {public static void main(String[] args) throws JsonProcessingException {Person person new Person();person.setId(1L);person.setName(Louie);person.setEmail(xxxqq.com);person.setAge(23); // new YAMLFactory().disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER);可以去掉三个横线ObjectMapper mapper new ObjectMapper(new YAMLFactory());String s mapper.writeValueAsString(person);System.out.println(s);} } 编写配置 #增加一种新的内容类型 spring.mvc.contentnegotiation.media-types.yamltext/yaml MyYamlHttpMessageConverter 增加HttpMessageConverter组件专门负责把对象写出为yaml格式 public class MyYamlHttpMessageConverter extends AbstractHttpMessageConverterObject {private ObjectMapper objectMapper null;//实例化public MyYamlHttpMessageConverter(){//告诉SpringBoot这个MessageConverter支持哪种媒体类型super(new MediaType(text, yaml, Charset.forName(UTF-8)));//把对象转化为yamlthis.objectMapper new ObjectMapper(new YAMLFactory());}Overrideprotected boolean supports(Class? clazz) {//只要是对象类型不是基本类型return true;}Override//RequestBodyprotected Object readInternal(Class? clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {return null;}Override//ResponseBody 把对象怎么写出去protected void writeInternal(Object methodReturnValue, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {//try-with写法自动关流try(OutputStream os outputMessage.getBody()){this.objectMapper.writeValue(os, methodReturnValue);}} } 编写配置类 Configuration//这是一个配置类 public class MyConfig implements WebMvcConfigurer {Beanpublic WebMvcConfigurer webMvcConfigurer(){return new WebMvcConfigurer() {Override//配置一个能把对象转为yaml的messageConverterpublic void configureMessageConverters(ListHttpMessageConverter? converters) {WebMvcConfigurer.super.configureMessageConverters(converters);converters.add(new MyYamlHttpMessageConverter());}};} ​ } 总结 1、配置媒体类型支持 spring.mvc.contentnegotiation.media-types.yamltext/yaml 2、编写对应的HttpMessageConverter告诉Boot这个支持的媒体类型 3、把MessageConverter组件加入到底层。容器中放一个WebMvcConfigurer组件并配置底层的MessageConverter 5、模板引擎 由于SpringBoot使用了嵌入式Servlet容器所以JSP默认不能使用如果需要服务端页面渲染优先考虑使用模板引擎。 SpringBoot模板引擎的自动配置包括FreeMarker、Groovy、Thymeleaf、Mustache Thymeleaf整合 Thymeleaf官网 基础语法 ①核心用法 th:xxx动态渲染指定的html标签属性值、或者th指令(遍历、判断等) th:text标签体内文本值渲染 h3 th:text${msg}哈哈/h3 !--th:text:替换标签体的内容-- h3 th:utext${msg}呵呵/h3 !--th:utext:不会转义html标签显示html该有的样式-- th:属性标签指定属性渲染 img th:src${url}/ !--访问动态路径-- th:attr标签任意属性渲染 img srcpicture.jpg stylewidth: 300px th:attrsrc${url}, style${modelStyle}/ !--访问动态路径, 和动态的样式-- th:if、th:each...其他th指令 img src${url} th:if${show}/ !--访问动态路径, 并且由控制器传入的show来判断它是否显示-- 表达式用来动态取值 ${}:变量取值使用model共享给页面的值都直接用${} {}:url路径 #{}:国际化消息 ~{}:片段引用 *{}:变量选择(需要配合th:object绑定对象) 系统工具和内置对象可以查看详细文档。 param:请求参数对象 p参数是否存在: ${param.containsKey(parameterName)}/p session:session对象 pSession属性: ${session.getAttribute(attributeName)}/p application:application对象 pApplication属性: ${application.getAttribute(attributeName)}/p #execInfo:模板执行信息 p模板名称: ${#execInfo.templateName}/p p模板行号: ${#execInfo.templateLine}/p #messages:国际化消息 p国际化消息: #{messageKey}/p #uris:uri/url工具 a href${#uris.encodeUrl(/path)}链接/a #conversions:类型转换工具 p转换为数字: ${#conversions.stringToNumber(10)}/p #dates:日期工具是java.util.Date对象的工具类 p当前日期: ${#dates.format(#dates.createNow(), yyyy-MM-dd)}/p #calendars:类似dates,只不过是java.util.calendar对象的工具类 #temporals:JDK8java.time API工具类 #numbers:数字操作工具 #strings:字符串操作 #objects:对象操作 #bools:bool操作 #arrays:array工具 #lists:list工具 #sets:set工具 #maps:map工具 #aggrregates:集合聚合工具(sum、avg) #ids:id生成工具 ②语法示例 表达式 变量取值${...} url取值{...} 国际化消息:#{} 变量选择:*{...} 片段引用~{...} 文本操作 拼串 文本替换|The name is ${name| 布尔操作 二进制运算and、or 取反!、not 比较运算 比较、、、(gt,lt,ge,le) 等值运算、(eq、ne) 条件运算 if-then:(if)?(then) if-then-else:(if)?(then):(else) default:(value)?:(defaultValue) 特殊语法 无操作_ 示例 属性设置 ①th:href{/product/list} ②th:attrcalss${active} ③th:attrsrc{/images/gtvglogo.peng},title${logo}, alt#{logo} ④th:checked${user:active} 遍历 语法th:each元素名 迭代状态${集合} 迭代状态(iterStat)包含下面的属性 index:当前遍历元素的索引从0开始 count:当前遍历元素的索引从1开始 size:需要遍历元素的总数量 current:当前正在遍历的元素对象 event/odd:是否偶数/奇数行 first:是否第一个元素 last:是否最后一个元素 使用方式 导入场景 dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-thymeleaf/artifactId /dependency 实体类Person Data NoArgsConstructor AllArgsConstructor public class Person {private Long id;private String name;private String email;private Integer age;private String pwd; } 编写测试页面 list.html !DOCTYPE html html langen headmeta charsetUTF-8 xmlns:thhttp://www.thymeleaf.orgtitleperson列表页/title /headbody table border1px cellpadding0 trth#/thth名字/thth邮箱/thth年龄/thth密码/thth状态信息/th/trtr th:eachperson, status:${persons}td/tdtd th:text${person.name}/tdtd[[${person.email}]]/tdtd th:text${person.age}/tdtd th:text${person.pwd}/tdtdindex: [[${status.index}]]br/count:[[${status.count}]]br/size: [[${status.size}]]br/current(当前对象): [[${status.current}]]br/even: [[${status.even}]]br/odd: [[${status.odd}]]br/first: [[${status.first}]]br/last: [[${status.last}]]br//td/tr /table /body /html controller GetMapping(/list) public String list(Model model){ListPerson person Arrays.asList(new Person(1L, 张三01, aaa1qq.com, 12, asd),new Person(2L, 张三02, aaa2qq.com, 18, akd),new Person(3L, 张三03, aaa3qq.com, 19, ajd),new Person(4L, 张三04, aaa4qq.com, 22, aad),new Person(5L, 张三05, aaa5qq.com, 23, afd),new Person(6L, 张三06, aaa6qq.com, 23, abd));model.addAttribute(persons, person);return list; } 判断th:if td th:text|${person.age} / ${person.age18?成年:未成年}|/td td th:if${#strings.isEmpty(person.email)} th:text已失联/td !--删除两-- td th:if${not #strings.isEmpty(person.email)} th:text${person.email}/td th:switch 属性优先级 优先级特点属性1片段包含th:insert th:replace2遍历th:each3判断th:if th:unless th:switch th:case4定义本地变量th:object th:with5通用方式属性修改th:attr th:attrprepend th:attrappend6指定属性修改th:value th:href th:src7文本值th:text th:utext8片段指定th:fragment9片段移除th:remove 注意 在行内使用的时候可以有如下的两种写法 [[...]]或[(...)] #如 size: [[${status.size}]]br/ 变量选择 模板布局 将通用的内容抽取出来。 属性含义th:fragment定义模板~{templatename::selector}(~{模板名 :: 片段名})引用模板th:insert、th:replace插入模板 footer表示公共页面的名称。 自动配置原理 ①开启了org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration自动配置 ②属性绑定在ThymeleafProperties中对应配置文件前缀spring.thymeleaf内容 ③所有的模板页面默认在classpath:/templates文件夹下 ④默认效果 a、所有的模板页面在classpath:/templates下面找 b、后缀名为.html devtools SpringBoot提供的工具在修改页面后。在IDEA中ctrlF9重新编译。 使用方式 方式一 !--热启动功能-- dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-devtools/artifactId /dependency java代码的修改如果使用devtools热启动了可能会引起一些BUG,建议使用重启。 方式二 也可以在setting的Compiler中勾选Build project automatically然后点击ctrl alt shift /打开Register,勾选其中的compiler.automake.allow.when.app.running。(当idea失去焦点5秒时自动构建。不用一直点) 热部署范围配置 默认不触发重启的目录列表 /META-INF/maven /META-INF/resource /resources /static /public /templates 可以通过修改配置文件的方式修改这个范围。 spring:devtools:restart:exclude: static/**, public/**, config/application.yaml 关闭热启动的功能 可以在application.yaml中设置enabledfalse,但可能存在的情况是其他配置文件相同内容的覆盖所以可在主程序中设置 public static void main(String[] args) {System.setProperty(spring.devtools.restart.enabled, false);SpringApplication.run(SSMPApplication.class, args); } 6、国际化 国际化的自动配置参照MessageSourceAutoConfiguration 实现步骤 SpringBoot在类路径下查找message资源绑定文件。文件名为messages.properties 多语言可以定义多个消息文件命名为message_区域代码.properties。如 messages.properties:默认 messages_en_US.properties:英文环境 messages_zh_CN.properties:中文环境 在程序中可以自动注入Message组件获取国际化的配置项值 在页面中可以使用表达式#{}获取国际化的配置选项 实现方式 配置文件方式 配置文件 messages.properties loginLogin signSign-Up messages_en_US.properties loginLogin signSign-Up messages_zh_CN.properties login登录 sign注册 展示页面earth.html !DOCTYPE html html langen headmeta charsetUTF-8 xmlns:thhttp://www.thymeleaf.orgtitleTitle/title /head body !--国际化:配置页面的值去哪个文件取到key-- divbutton th:text#{login}/buttonbutton th:text#{sign}/button /div /body /html controller GetMapping(/internationalize) public String test(){return earth; } 代码方式 需要使用RestController Autowired//国际化取消息用的组件 MessageSource messageSource; GetMapping(/haha) public String haha(HttpServletRequest req){Locale locale req.getLocale();//利用代码的方式获取国际化配置文件中指定的值String login messageSource.getMessage(login, null, locale);return login; } 7、错误处理 错误处理的自动配置都在ErrorMvcAutoConfiguration中有两大核心机制 1、SpringBoot会自动适应处理错误(你设置的format是什么它就可以给你响应什么数据)响应页面或JSON数据 2、SpringMvc的错误处理机制依然保留MVC处理不了才会交给boot进行处理。 测试 ExceptionHandler Controller public class ExceptionController {GetMapping(/exception)public String testException(){int i 10 / 0;return index;} ​ ​ ​/*** 1、ExceptionHandler标识一个方法处理错误默认只能处理这个类发生的的指定错误* 2、ControllerAdvice统一处理所有错误* param e* return*/ResponseBodyExceptionHandler(Exception.class)public String handlerException(Exception e){return OHH~, 错误原因 e.getMessage();} } ControllerAdvice 控制器 Controller public class ExceptionController {GetMapping(/exception)public String testException(){int i 10 / 0;return index;} } handler ControllerAdvice public class GlobalExceptionHandler {/*** 1、ExceptionHandler标识一个方法处理错误默认只能处理这个类发生的的指定错误* 2、ControllerAdvice统一处理所有错误* param e* return*/ResponseBodyExceptionHandler(Exception.class)public String handlerException(Exception e){return OHH~同一处理, 错误原因 e.getMessage();} } 错误处理类BasicErrorController 表示它从server.error.path读取配置文件如果没有配置就会读取/error 发生错误以后转发给/error路径SpringBoot在底层写好一个BasicErrorController的组件专门处理这个请求。 错误页面解析方式 //1、解析错误的视图地址 ModelAndView modelAndView resolveErrorView(request, response, status, model); //2、如果解析不到错误页面的地址默认的错误页就是error页 return (modelAndView ! null) ? modelAndView : new ModelAndView(error, model); 容器中专门有一个错误视图解析器(在ErrorMvcAutoConfiguration) Bean ConditionalOnBean(DispatcherServlet.class) ConditionalOnMissingBean(ErrorViewResolver.class) DefaultErrorViewResolver conventionErrorViewResolver() {return new DefaultErrorViewResolver(this.applicationContext, this.resources); } DefaultErrorViewResolver Override public ModelAndView resolveErrorView(HttpServletRequest request, HttpStatus status, MapString, Object model) {ModelAndView modelAndView resolve(String.valueOf(status.value()), model);if (modelAndView null SERIES_VIEWS.containsKey(status.series())) {modelAndView resolve(SERIES_VIEWS.get(status.series()), model);}return modelAndView; } ​ private ModelAndView resolve(String viewName, MapString, Object model) {String errorViewName error/ viewName;TemplateAvailabilityProvider provider this.templateAvailabilityProviders.getProvider(errorViewName,this.applicationContext);if (provider ! null) {return new ModelAndView(errorViewName, model);}return resolveResource(errorViewName, model); } ​ private ModelAndView resolveResource(String viewName, MapString, Object model) {for (String location : this.resources.getStaticLocations()) {try {Resource resource this.applicationContext.getResource(location);resource resource.createRelative(viewName .html);if (resource.exists()) {return new ModelAndView(new HtmlResourceView(resource), model);}}catch (Exception ex) {}}return null; } 容器中有一个默认的名为error的view;提供了默认白页功能。 Bean(name error) ConditionalOnMissingBean(name error) public View defaultErrorView() {return this.defaultErrorView; } 规则 解析一个错误页 如果发生了5xx,4xx ①、如果有模板引擎默认会在classpath:/templates/error/精确码.html ②、如果没有模板引擎在静态资源文件夹下找精确码.html(模糊匹配) 如果匹配不到精确码.html有模板引擎就去找classpath:/templates/error/5xx/4xx.html,没有模板引擎在静态资源文件夹下找5xx.html4xx.html。 如果模板引擎路径下面有error.html页面就直接渲染。 测试 templates下error !DOCTYPE html html langen headmeta charsetUTF-8 xmlns:thhttp://www.thymeleaf.orgtitleerror/title /head body 模板引擎 ERROR /body /html 最佳处理错误方式 前后端分离 后台发生的所有错误使用 ControllerAdvice ExceptionHandler进行异常处理 服务端页面渲染 - HTTP状态码规定错误 给classpath:/templates/error/放入常用精确的错误码页面500.html/404.html 给classpath:/templates/error/放通用模糊匹配的错误页面。4xx.html/5xx.html - 业务错误 核心业务每种错误都因该代码控制跳转到定制错误页面。 通用业务在classpath:/templates/error.html页面显示错误信息。(出现的错误信息通过SpringBoot都将它们放在了ModelAndView只需要通过Thymeleaf方式取值即可) 8、嵌入式容器 Servlet容器管理运行Servlet组件(ServletFilter, Listener)的环境一般值服务器。 自动配置原理 SpringBoot默认嵌入了Tomcat作为Servlet容器。自动配置类是ServletWebServerFactoryAutoConfiguration,EnbeddedWenServerFactoryCustomizerAutoConfiguration AutoConfiguration(after SslAutoConfiguration.class) AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE) ConditionalOnClass(ServletRequest.class) ConditionalOnWebApplication(type Type.SERVLET) EnableConfigurationProperties(ServerProperties.class) Import({ ServletWebServerFactoryAutoConfiguration.BeanPostProcessorsRegistrar.class,ServletWebServerFactoryConfiguration.EmbeddedTomcat.class,ServletWebServerFactoryConfiguration.EmbeddedJetty.class,ServletWebServerFactoryConfiguration.EmbeddedUndertow.class }) public class ServletWebServerFactoryAutoConfiguration { } a、ServletWebServerFactoryAutoConfiguration配置类绑定了所有和服务器有关的配置server b、绑定了自动配置类嵌入式容器场景。 c、ServletWebServerFactoryAutoConfiguration导入了嵌入式的三大服务器Tomcat、Jetty、Undertow i、导入Tomcat、Jetty、Undertow都有条件注解系统中有这个类才行(相关依赖) ii、默认Tomcat配置生效 iii、它们都给容器中ServletWebServerFactory放了一个web服务器工厂(造web服务器) iV、web服务器工厂都有一个功能getWebServer获取web服务器 d、ServletWebServerApplicationContextioc容器启动的时候会调用(createWebServer)创建web服务器而createWebServer会在onRefresh启动。Spring容器刷新的时候会预留一个时机刷新子容器。onRefresh 总结 Web场景的Spring容器启动在onRefresh的时候会调用Web服务器的方法。 Web服务器的创建是通过WebServerFactory实现容器中又会根据导入的依赖的条件注解启动相关服务器。 用法 修改server下的相关配置即可修改服务器参数通过给容其中放入一个ServletWebServerFactory来禁用SpringBoot默认放的工厂实现自定义嵌入任意服务器。 9、全面接管SpringMVC SpringBoot默认配置好了SpringMVC的所有常用特性如果我么需要全面接管SpringMVC的所有配置并禁用默认配置仅需要编写一个WebMvcConfigurer配置类并标注EnableWebMvc即可。 全手动模式 EnableWebMvc禁用默认设置 WebMvcConfigurer组件定义MVC的底层行为 EnableWebMvc原理 ①WebMvcAutoConfigurationweb场景的自动配置类。 SpringMVC自动场景配置了如下所有的默认行为 a、支持RESTful的filter:HiddenHttpMethodFilter b、支持非POST请求请求体携带数据FormContentFilter c、导入了Import(EnableWebMvcConfiguration): i、WelcomePageHandlerMapping:欢迎页功能支持(模板引擎目录金泰资源目录放index.html)项目访问/就默认展示这个页面 ii、LocaleResolver:国际化解析器 iii、ThemeResolver:主题解析器 iv、FlashMapManager:临时数据共享 v、FormattingConversionService:数据格式化、类型转化 vi、Validator:数据校验JSR303提供的数据校验功能 vii、RequestMappingHandlerMapping找每个请求由谁处理的映射关系 viii、WebBindingInitializer:请求参数的封装与绑定 ix、ExceptionHandlerExceptionResolver:默认的异常解析器 x、ContentNegotiationManager内容协商管理器 d、WebMvcAutoConfigurationAdapter配置生效它是一个WebMvcConfigurer,定义mvc底层组件 ​ i、定义好WebMvcConfigurer底层组件的默认功能 ​ ii、视图解析器:InternalResourceViewResolver ​ iii、视图解析器:BeanNameViewResolver,视图名(controller方法返回值字符串)就是组件名 ​ iV、内容协商解析器ContentNegotiatingViewResolver ​ v、请求上下文的过滤器RequestContextFilter:任意位置直接获取当前请求(RequestContextHolder) ​ vi、静态资源链规则 ​ vii、ProblemDetailsExceptionHandler错误详情 ​ e、定义了默认的底层行为WebMvcConfigurer ②EnableWebMvc禁用默认行为 a、给容器中导入DelegatingWebMvcConfiguration组件它是WebMvcConfigurationSupport b、WebMvcAutoConfiguration有一个核心的注解ConditionalOnMissingBean(WebMvcConfigurationSupport.class)容器中没有WebMvcConfigurationSupport,WebMvcAutoConfiguration才生效 c、EnableWebMvc导入WebMvcConfigurationSupport导致WebMvcAutoConfiguration失效。导致禁用了。 10、Web新特性 Problemdetails 它是RFC 7807定义的规范用于定义错误信息返回格式。 文档RFC 7807: Problem Details for HTTP APIs 原理 Configuration(proxyBeanMethods false) //配置过一个属性spring.mvc.problemdetails.enabledtrue ConditionalOnProperty(prefix spring.mvc.problemdetails, name enabled, havingValue true) static class ProblemDetailsErrorHandlingConfiguration { ​BeanConditionalOnMissingBean(ResponseEntityExceptionHandler.class)ProblemDetailsExceptionHandler problemDetailsExceptionHandler() {return new ProblemDetailsExceptionHandler();} ​ } a、ProblemDetailsExceptionHandler是一个ControllerAdvice集中处理系统异常 b、处理以下指定异常如果系统出现以下异常会被SpringBoot支持RFC 7807的方式返回异常 ExceptionHandler({HttpRequestMethodNotSupportedException.class,HttpMediaTypeNotSupportedException.class,HttpMediaTypeNotAcceptableException.class,MissingPathVariableException.class,MissingServletRequestParameterException.class,MissingServletRequestPartException.class,ServletRequestBindingException.class,MethodArgumentNotValidException.class,NoHandlerFoundException.class,AsyncRequestTimeoutException.class,ErrorResponseException.class,ConversionNotSupportedException.class,TypeMismatchException.class,HttpMessageNotReadableException.class,HttpMessageNotWritableException.class,BindException.class}) 函数式web SpringMVC5.2之后允许我们使用函数式的方式定义web的请求处理流程。 Web请求处理的方式 a、Controller RequestMapping耦合式(路由、业务耦合) b、函数式Web分离式(路由、业务分离) 使用场景实例 场景 User RESTful-CRUD GET/user/1 获取1号用户 GET/user 获取所有用户 POST/user 请求体携带JSON,新增一个用户 PUT/user/1 请求体携带JSON,修改1号用户 DELETE/user/1删除1号用户 实现步骤 核心类 RouterFunction RequestPredicate ServerRequest ServerResponse ①实体类User Data NoArgsConstructor AllArgsConstructor public class User {private Long id;private String name;private String email;private Integer age;private String pwd; } ②编写路由请求WebFunctionConfig package com.louis.config; ​ import com.louis.business.UserBusinessHandler; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.MediaType; import org.springframework.web.servlet.function.*; ​ /*** author XRY* date 2023年07月13日15:15*/ ​ /***     GET/user/1       获取1号用户*     GET/user         获取所有用户*     POST/user       请求体携带JSON,新增一个用户*     PUT/user/1       请求体携带JSON,修改1号用户*     DELETE/user/1   删除1号用户*/ Configuration public class WebFunctionConfig {/*** 函数式web* 1、给容器中放一个Bean RouterFunctionServerResponse* 2、每个业务准备一个自己的handler,但它的声明需要使用HandlerFunction类型** 函数式web核心四大对象* 1、RouterFunction:定义路由信息。发什么请求、谁来处理* 2、RequestPredicate:请求谓语。请求方式(Get、POST)、请求参数* 3、ServerRequest:封装请求完整数据* 4、ServerResponse:封装响应完整数据*//*   Beanpublic RouterFunctionServerResponse userRoute(){//s表示某个类的工具类RouterFunctions.route() //开始定义路由信息.GET(/user/{id}, RequestPredicates.accept(MediaType.ALL), request - {//业务处理 ​//构造响应return ServerResponse.ok().build();}) //RequestPredicates.accept(MediaType.ALL)表示接收任意类型的参数return null;}*///Bean给容器中放组件的时候如果参数是一个对象它会默认从容器中取相当于自动注入Beanpublic RouterFunctionServerResponse userRoute(UserBusinessHandler userBusinessHandler){ ​return RouterFunctions.route().GET(/user/{id}, RequestPredicates.accept(MediaType.ALL), userBusinessHandler::getUser/*相当于调用userBusinessHandler的方法*/).GET(/users, userBusinessHandler::getUsers).POST(/user, RequestPredicates.accept(MediaType.APPLICATION_JSON), userBusinessHandler::saveUser).PUT(/user/{id}, RequestPredicates.accept(MediaType.APPLICATION_JSON), userBusinessHandler::updateUSer).DELETE(/user/{id}, userBusinessHandler::deleteUser).build();} } ③业务处理类UserBusinessHandler /*** 专门处理User有关的业务*/ Slf4j Component public class UserBusinessHandler {/*** 查询指定id的user* 他要代替原来的handlerFunction* public interface HandlerFunctionT extends ServerResponse {* T handle(ServerRequest request) throws Exception;** }* param request* return*/public ServerResponse getUser(ServerRequest request) throws Exception{String id request.pathVariable(id);log.info(查询{}用户信息, id);//业务处理User user new User(1L, louis, aaqq.com,23, 1234);//构造响应return ServerResponse.ok().body(user);} ​/*** 获取所有用户* param request* return* throws Exception*/public ServerResponse getUsers(ServerRequest request) throws Exception{log.info(查询所有用户信息完成);//业务处理ListUser users Arrays.asList(new User(1L, 张三01, aaa1qq.com, 12, asd),new User(2L, 张三02, , 18, akd),new User(3L, 张三03, , 19, ajd),new User(4L, 张三04, aaa4qq.com, 22, aad),new User(5L, 张三05, aaa5qq.com, 23, afd),new User(6L, 张三06, aaa6qq.com, 23, abd));return ServerResponse.ok().body(users);//凡是body中的数据就是以前的ResponseBody原理利用HttpMessageConverter写出为json} ​/*** 保存用户* param request* return*/public ServerResponse saveUser(ServerRequest request) throws ServletException, IOException {//提取请求体User body request.body(User.class);log.info(保存信息{}, body);return ServerResponse.ok().build();} ​ ​/*** 更新用户* param request* return*/public ServerResponse updateUSer(ServerRequest request) throws ServletException, IOException {User body request.body(User.class);log.info(用户信息更新:{}, body);return ServerResponse.ok().build();} ​ ​/*** 删除用户* param request* return*/public ServerResponse deleteUser(ServerRequest request) {String id request.pathVariable(id);log.info(删除用户-{}-信息, id);return ServerResponse.ok().build();} } ④测试 getUser getUsers saveUser updateUSer deleteUser
http://www.sadfv.cn/news/63237/

相关文章:

  • 在线切图网站石河子建设网站
  • 巴中建设机械网站怎么找做网站的
  • php网站开发安全手机扁平化网站模版
  • wordpress建两个网站吗哪个网站上做ppt比较好看的图片
  • 外贸建站深圳安徽省建设干部学校网站关停
  • 做网站需要租服务器吗出售app软件的平台
  • 做网站模板平台python网站开发入门
  • 国内设计的企业网站网站seo招聘
  • 网站建设制作专业开发定制数字广东网络建设有限公司地址
  • 商城网站建设计划书公司高管培训课程
  • 公司网站在哪里做肇庆网络营销外包公司
  • 织梦网站根目录在哪里wordpress程序安装包
  • 怎样做网站亮照亮标广州有专做网站
  • 广州大型网站制作公司专业的网站服务公司
  • cms官方网站网站制作和设计需要多少钱
  • 做二手车的网站玉溪市建设厅官方网站
  • 建设很行官方网站做网站什么商品好
  • 网站如何在手机端做适配北京 网站建设 知乎
  • 江苏网站建设公司排名wordpress用户角色插件
  • 网站建设找客户渠道在北京网站建设的岗位职责
  • 平台网站建设协议书东莞销售网站设计
  • 建设集团网站的作用网站建设公司权威排名
  • 网站备案流程实名认证凡科网微信小程序制作
  • 物流网站功能互联网平台建设方案
  • 阿里云虚拟主机建网站星裕建设网站
  • ui中国设计网站页面推广宝
  • 宁波高端网站开发net网站开发学什么
  • 北京国贸网站建设宁夏建设监督网站
  • 视频网站开发难点佛山新网站制作特色
  • 邢台企业做网站找谁seo优化排名方法