框架学习随笔5-SpringBoot web相关

对静态资源的映射规则

@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。 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值