SpringBoot学习(四)——WEB开发
来源:互联网 发布:node v4.6.0 x64.msi 编辑:程序博客网 时间:2024/06/05 08:11
一.WEB开发支持
SpringBoot提供了spring-boot-starter-web为web开发予以支持。而spring-boot-starter-web嵌入了Tomcat及SpringMVC的依赖。查看org.springframework.boot.autoconfigure.web包,其中:
ServerPropertiesAutoConfiguration与ServerProperties自动配置内嵌servlet容器HttpEncodingAutoConfiguration与HttpEncodingProperties用来自动配置http编码JacksonHttpMessageConvertersConfiguration用于配置mappingJackson2HttpMessageConverterConfiguration与mappingJackson2XmlHttpMessageConverterConfigurationWebMvcAutoConfiguration与WebMvcProperties用于配置SpringMVC
二.WEB相关配置
1.SpringMVC自动配置
Spring Boot为Spring MVC提供的auto-configuration适用于大多数应用,并在Spring默认功能上添加了以下特性:
1). 引入 ContentNegotiatingViewResolver 和 BeanNameViewResolver
beans(查看WebMvcAutoConfiguration类)。
2). 对静态资源的支持,包括对WebJars的支持。
①对静态资源的支持
Spring Boot 默认配置的/映射到/static(或/public ,/resources,/META-INF/resources),/webjars/会映射到classpath:/META-INF/resources/webjars/。
注意:上面的目录都是相对classpath:下面。
以下时默认配置:
#该值只能有一个值,不像大多数可以配置多个用逗号隔开的。spring.mvc.static-path-pattern=/** # Path pattern used for static resources.spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/ # Locations of static resources.
如果想增加如/mystatic/**映射到classpath:/mystatic/,可以让配置类继承WebMvcConfigurerAdapter,然后重写如下方法:
@Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/mystatic/**").addResourceLocations("classpath:/mystatic/");}
这种方式会在默认的基础上增加/mystatic/**映射到classpath:/mystatic/,不会影响默认的方式,可以同时使用。
②对WebJars的支持
Web前端使用了越来越多的JS或CSS,如jQuery, 和Bootstrap等。一般情况下,我们是将这些Web资源拷贝到Java的目录下,通过手工进行管理,这种通方式容易导致文件混乱、版本不一致等问题。
WebJars是将这些通用的Web前端资源打包成Java的Jar包,然后借助Maven工具对其管理,保证这些Web资源版本唯一性,升级也比较容易。关于webjars资源,有一个专门的网站http://www.webjars.org/,我们可以到这个网站上找到自己需要的资源,在自己的工程中添加入maven依赖,即可直接使用这些资源了。
以下是引入jquery的例子,在pom文件中添加相关依赖
<dependency> <groupId>org.webjars</groupId> <artifactId>jquery</artifactId> <version>1.11.3</version></dependency>
查看引入的jar包的目录结构
通过访问路径”localhost:8080/webjars/jquery/1.11.3/jquery.min.js”可以查看引入的jquery.min.js文件
3). 自动注册 Converter , GenericConverter , Formatter beans。
4). 对 HttpMessageConverters 的支持。
①自动配置
在WebMvcAutoConfiguration类中的内部类WebMvcAutoConfigurationAdapter有字段messageConverters(HttpMessageConverters类型),而该对象(即messageConverters)在HttpMessageConvertersAutoConfiguration类中定义(其中添加了@ConditionalOnMissingBean注解)
②使用Spring Boot的 HttpMessageConverters 类添加或自定义转换类:
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;import org.springframework.context.annotation.*;import org.springframework.http.converter.*;@Configurationpublic class MyConfiguration { @Bean public HttpMessageConverters customConverters() { HttpMessageConverter<?> additional = ... HttpMessageConverter<?> another = ... return new HttpMessageConverters(additional, another); }}
上下文中出现的所有 HttpMessageConverter bean都将添加到converters列表,
③通过配置类继承WebMvcConfigurerAdapter后重写configureMessageConverters方法:
@Componentpublic class Config extends WebMvcConfigurerAdapter{ @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { StringHttpMessageConverter converter = new StringHttpMessageConverter(Charset.forName("UTF-8")); super.configureMessageConverters(converters); }}
方式②、方式③会覆盖默认的转换器列表(converters)。
5). 自动注册 MessageCodeResolver 。
6). 对静态 index.html 的支持。
默认的欢迎页面检测将切换到您的自定义位置(默认是spring.resources.static-locations),因此,如果您在启动时的任何位置都有一个index.html,它将是应用程序的主页。
7). 对自定义 Favicon 的支持。
8). 自动使用 ConfigurableWebBindingInitializer bean。
2.添加Servlets, Filters和listeners
1).通过@Bean注解添加
想要添加 Servlet , Filter 或Listener ,只需要为它提供一个 @Bean 定义,案例如下:
@Beanpublic TestServlet createTestServlet(){ return new TestServlet();}@Beanpublic TestFilter createTestFilter(){ return new TestFilter();}@Beanpublic TestListener createTestListener(){ return new TestListener();}@Beanpublic ServletRegistrationBean addServlet(TestServlet testServlet){ //参数一:需要注册的Servlet //参数二:Servlet的拦截路径 ServletRegistrationBean registrationBean = new ServletRegistrationBean(testServlet,"/test"); registrationBean.setEnabled(false); return registrationBean;}@Beanpublic FilterRegistrationBean addFilter(TestFilter testFilter){ FilterRegistrationBean registrationBean = new FilterRegistrationBean(testFilter); return registrationBean;}@Beanpublic ServletListenerRegistrationBean<EventListener> addRequestListener(TestListener testListener){ ServletListenerRegistrationBean<EventListener> listener = new ServletListenerRegistrationBean<EventListener>(new TestListener()); return listener;}
2).扫描注解的方式
通过把 @ServletComponentScan 注解到一个 @Configuration 类并指定包含要注册组件的package(s),可以将@WebServlet,@WebFilter和@WebListener 注解的类自动注册到内嵌servlet容器。默认情况下,@ServletComponentScan将从被注解类的package开始扫描。
3).禁止Servlet或Filter的注册
任何 Servlet 或 Filter beans都将自动注册到servlet容器。不过,为特定的 Filter 或 Servlet bean创建一个registration,并将它标记为disabled,可以禁用该filter或servlet。例如:
@Beanpublic ServletRegistrationBean addServlet(TestServlet testServlet){ ServletRegistrationBean registrationBean = new ServletRegistrationBean(testServlet); registrationBean.setEnabled(false); return registrationBean;}
3.SpringMVC添加拦截器
拦截器的功能跟过滤器类似,但是提供更精细的的控制能力:
preHandle方法在调用处理程序之前;
postHandle方法在处理程序之后,呈现视图之前;
afterCompletion在完成呈现视图之后将调用处理程序执行的任何结果,从而允许正确的资源清理。
我们不能通过拦截器修改request内容,但是可以通过抛出异常(或者返回false)来暂停request的执行。
添加拦截器只需要2步:
1).创建拦截器类实现HandlerInterceptor接口
public class MyInterceptor implements HandlerInterceptor{ @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("拦截器MyInterceptor的preHandle方法"); return true;}@Overridepublic void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("拦截器MyInterceptor的postHandle方法");}@Overridepublic void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("拦截器MyInterceptor的afterCompletion方法"); }}
2).创建一个Java配置类继承WebMvcConfigurerAdapter,并重写 addInterceptors 方法。
@Componentpublic class Config extends WebMvcConfigurerAdapter{ @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new MyInterceptor()).addPathPatterns("/*").excludePathPatterns("/properties"); }}
- SpringBoot学习(四)——WEB开发
- SpringBoot学习-(三)SpringBoot开发Web
- SpringBoot学习(四)—AOP切面编程
- SpringBoot学习笔记(四) SpringBoot Web相关的自动配置
- SpringBoot学习(四)SpringBoot和WebSocket
- Web应用开发实用编程指导(四)—学习比经验更重要
- SpringBoot学习-(四)SpringBoot配置Druid连接池
- Springboot学习笔记(四)Repository接口
- springboot学习笔记四
- Springboot学习笔记(四)
- SpringBoot(二)开发WEB应用
- SpringBoot Web开发体验
- springboot web 开发
- SpringBoot的web开发
- SpringBoot入门Web开发
- 添加登录拦截器【springboot mybatis学习与开发(四)】
- SpringBoot学习(三)——SpringBoot的配置
- springboot(四、springboot+mybatis)
- Windows下python连接、操作oracle数据库(cx_Oracle)
- shell的输入输出
- Java中 3*0.1 == 0.3 返回值 false ,1*0.3 == 0.3 返回值 true
- HDU
- 我用过的计算机
- SpringBoot学习(四)——WEB开发
- linux操作系统
- Hive DDL & 内部表与外部表
- caffe源码 链式法则
- caffe源码 全连接层
- caffe源码 卷积层
- hdu5877 Weak Pair(离散化+线段树+dfs)
- 上帝是个程序猿
- 如何将电脑的无线网通过有线分享给其他设备