1、静态资源映射规则
静态资源:可以理解为前端的固定页面,这里面包含HTML、CSS、JS、图片等等,不需要查数据库也不需要程序处理,直接就能够显示的页面,如果想修改内容则必须修改页面,但是访问效率相当高。
动态资源:需要程序处理或者从数据库中读数据,能够根据不同的条件在页面显示不同的数据,内容更新不需要修改页面但是访问速度不及静态页面。
application.properties文件中静态资源存放地址修改:
#静态资源SpringResources配置 # spring.web.resources.static-locations:静态文件目录(支持多个,逗号分隔) # 默认值:classpath:/meta-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/ spring.web.resources.static-locations=classpath:static,classpath:html
2、Thymeleaf模板引擎(替代jsp页面展示的数据传输)
pom依赖:
org.springframework.boot spring-boot-starter-thymeleaf
application.properties中的thymlef配置:
# Thymeleaf配置 # spring.thymeleaf.cache : 是否启用缓存 # spring.thymeleaf.check-template : 在展示前检查模板是否存在 # spring.thymeleaf.check-template-location : 检查模板位置是否存在 # spring.thymeleaf.encoding : 模板文件编码 # spring.thymeleaf.mode : 模板模式 # spring.thymeleaf.prefix : 模板文件路径前缀 # spring.thymeleaf.reactive.max-chunk-size : 模板可使用的最大缓冲区 # spring.thymeleaf.servlet.content-type : 模板内容类型 # spring.thymeleaf.suffix : 模板文件后缀 spring.thymeleaf.cache=false spring.thymeleaf.check-template=true spring.thymeleaf.check-template-location=true spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.mode=HTML spring.thymeleaf.prefix=classpath:/html/ spring.thymeleaf.reactive.max-chunk-size=0 spring.thymeleaf.servlet.content-type=text/html spring.thymeleaf.suffix=.html
thymeleaf语法实例:html&demoController
demo
123
时间格式为"yyyy-MM-dd HH:mm:ss.SSS zzzz":
时间格式为"EEE,MMM d, yy":
时间格式为"EEEE,MMMM dd, yyyy,hh:mm:ss a (zzz)":
原数字:123456789.987654321
Number:
String:
String:
String:
List Start
:
List Ends
IF Start
是
否
IF Ends
package org.yuan.controller;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import java.util.ArrayList;
import java.util.Date;
@RestController
public class demo {
//Thymeleaf模板引擎数据传输demo
@RequestMapping("/demo")
public ModelAndView demo() {
ModelAndView mv = new ModelAndView();
//Time
mv.addObject("time", new Date());
//Number
mv.addObject("number", 123456789.987654321);
//String
mv.addObject("string", "Mr.yuan");
//JSonobject
JSonObject json = new JSonObject();
json.put("key_a", "value_a");
json.put("key_b", "value_b");
mv.addObject("json", json);
//List
ArrayList
3、错误页面自定义
继承ErrorPageRegistrar类,实现方法,当发生错误被HttpStatus捕捉。示例:
package org.yuan.init;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.ErrorPageRegistrar;
import org.springframework.boot.web.server.ErrorPageRegistry;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
@Configuration
public class ErrorPageConfig implements ErrorPageRegistrar {
@Override
public void registerErrorPages(ErrorPageRegistry registry) {
ErrorPage err400 = new ErrorPage(HttpStatus.BAD_REQUEST, "/err/400.html");
ErrorPage err401 = new ErrorPage(HttpStatus.UNAUTHORIZED, "/err/401.html");
ErrorPage err404 = new ErrorPage(HttpStatus.NOT_FOUND, "/err/404.html");
ErrorPage err500 = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/err/500.html");
ErrorPage err505 = new ErrorPage(HttpStatus.HTTP_VERSION_NOT_SUPPORTED, "/err/505.html");
registry.addErrorPages(err400, err401, err404, err500, err505);
}
}
4、SpringMVC扩展
interceptor拦截器:
package org.yuan.interceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Interceptor implements HandlerInterceptor {
//继承HandlerInterceptor,重写preHandle方法
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
return true;
}
}
package org.yuan.init;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.yuan.interceptor.Interceptor;
@Configuration
public class WebMvcConf implements WebMvcConfigurer {
//继承WebMvcConfigurer类,重写addInterceptors方法,添加自定义的拦截器并指定拦截和放过的地址
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new Interceptor()).addPathPatterns("/**").excludePathPatterns("/plugins/**", "/img/**", "/js/**", "/css/**");
}
}
在扩展的addInterceptors方法中设置拦截范围。



