对静态资源的映射规则
@ConfigurationProperties(prefix = "spring.resources", ignoreUnknownFields = false)
public class ResourceProperties implements ResourceLoaderAware {
//设置和静态资源有关的参数
所有 /webjars/** ,以jar包的方式引入静态资源,都去 classpath:/META-INF/resources/webjars/ 找资源。
/**访问当前项目的任何资源,都去静态资源的文件夹找映射,静态资源文件夹下的所有index.html页面,都被/**映射。
国际化
编写.properties文件,如test_US.properties、test_CN.properties。
SpringBoot自动配置好了管理国际化资源文件的组件,使用ResourceBundleMessageSource管理国际化资源文件 。
@ConfigurationProperties(prefix = "spring.messages")
public class MessageSourceAutoConfiguration {
@Bean
public MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
去设置Default_Settings设置,在页面中获取国际化的值。
注册拦截器
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**").excludePathPatterns("/index.html","/","/user/login");
}
错误处理机制
DefaultErrorAttributes组件共享信息,BasicErrorController组件处理error请求 ,ErrorPageCustomizer组件定制错误的响应规则。
Servlet三大组件
三大组件是Servlet、Filter、Listener。SpringBoot默认是以jar包的方式启动嵌入式的Servlet容器来启动SpringBoot的web应用。这种情况是没有web.xml文件的。
@Bean
public ServletRegistrationBean myServlet(){
ServletRegistrationBean registrationBean = new ServletRegistrationBean(new MyServlet(),"/myServlet");
return registrationBean;
}
@Bean
public FilterRegistrationBean myFilter(){
FilterRegistrationBean registrationBean = new FilterRegistrationBean();
registrationBean.setFilter(new MyFilter());
registrationBean.setUrlPatterns(Arrays.asList("/hello","/myServlet"));
return registrationBean;
}
@Bean
public ServletListenerRegistrationBean myListener(){
ServletListenerRegistrationBean<MyListener> registrationBean = new ServletListenerRegistrationBean<>(new MyListener());
return registrationBean;
}
嵌入式Servlet容器有:Tomcat(默认使用) 、Jetty、Undertow。优点是简便,缺点是默认不支持JSP等。
外置的Servlet容器需要安装Tomcat,然后使用war包的方式打包项目,放入wabapp文件夹,然后启动tomcat。