静态资源目录:/static 或 /resources 或 /META-INF/resources 或 /public
访问:当前项目根目录/ + 静态资源名
原理: 静态映射/**
请求进来,先去找Controller看能不能处理,不能处理的请求又都交给静态资源处理器,静态资源也找不到则响应404页面
#修改网页访问静态资源文件的路径
spring:
mvc:
static-path-pattern: /res/**
#更改默认的静态资源文件夹位置
web:
resources:
static-locations: [classpath:/haha/]
静态资源路径下 index.html–可以配置静态资源路径,但是不可以配置静态资源的访问前缀,否则导致 index.html不能被默认访问
controller能处理/index
静态资源路径下放置favicon.ico图标、重启项目
SpringBoot启动默认加载 xxxAutoConfiguration 类(自动配置类)
SpringMVC功能的自动配置类 WebMvcAutoConfiguration生效
@Configuration(
proxyBeanMethods = false
)
@ConditionalOnWebApplication(
type = Type.SERVLET
)
@ConditionalOnClass({Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class})
@ConditionalOnMissingBean({WebMvcConfigurationSupport.class})
@AutoConfigureOrder(-2147483638)
@AutoConfigureAfter({DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class, ValidationAutoConfiguration.class})
public class WebMvcAutoConfiguration {}
Rest风格支持(使用HTTP请求方式动词来表示对资源的操作)
以前:/getUser 获取用户 /deleteUser 删除用户 /editUser 修改用户 /saveUser 保存用户
现在: /user GET-获取用户 DELETE-删除用户 PUT-修改用户 POST-保存用户
核心Filter;HiddenHttpMethodFilter
用法: 表单method=post,隐藏域 _method=put
@RequestMapping(value = "/user",method = RequestMethod.GET)
public String getUser(){
return "GET-叶子航";
}
@RequestMapping(value = "/user",method = RequestMethod.POST)
public String saveUser(){
return "POST-叶子航";
}
@RequestMapping(value = "/user",method = RequestMethod.PUT)
public String putUser(){
return "PUT-叶子航";
}
@RequestMapping(value = "/user",method = RequestMethod.DELETE)
public String deleteUser(){
return "DELETE-叶子航";
}
@Bean
@ConditionalOnMissingBean(HiddenHttpMethodFilter.class)
@ConditionalOnProperty(prefix = "spring.mvc.hiddenmethod.filter", name = "enabled", matchIfMissing = false)
public OrderedHiddenHttpMethodFilter hiddenHttpMethodFilter() {
return new OrderedHiddenHttpMethodFilter();
}
spring:
mvc:
hiddenmethod:
filter:
enabled: true #开启页面表单的Rest功能
a>表单提交会带上_method=PUT
b>请求过来被HiddenHttpMethodFilter拦截
c>请求是否正常,并且是POST
d>获取到_method的值。
e>兼容以下请求;PUT.DELETE.PATCH
f>原生request(post),包装模式requesWrapper重写了getMethod方法,返回的是传入的值。
过滤器链放行的时候用wrapper。以后的方法调用getMethod是调用requesWrapper的。
//自定义filter
@Bean
public HiddenHttpMethodFilter hiddenHttpMethodFilter(){
HiddenHttpMethodFilter methodFilter = new HiddenHttpMethodFilter();
methodFilter.setMethodParam("_m");
return methodFilter;
}
咕咕咕~Web开发这一部分源码太多了,我先入门,等我回来补源码