Spring Boot实现Web的常用功能
Spring MVC整合支持
Spring MVC自动配置介绍
在SpringBoot项目中,一旦引入了Web依赖启动器spring-boot-starter-web那么SpringBoot整合Spring MVC框架默认实现的一些AutoConfiguration自动配置类会自动生效,刻几乎在无任何额外配置的情况下进行进行Web开发。Spring Boot整合SpringMVCWeb开发主要提供了以下自动配置的功能特性。
(1)内置了两个视图解析器
(2)支持静态资源以及WebJars
(3)自动注册了转换器和格式化器
(4)支持Http消息转换器
(5)自动注册了消息代码解析器
(6)支持静态项目首页index/html
(7)支持定制应用图标favicon.ico
(8)自动初始化Web数据绑定器ConfigurableWebBindindInitializer
Spring MVC功能扩展实现
Spring Boot整合Spring MVC进行Web开发时提供了许多自动化配置,开发时还需要开发者对一些功能进行扩展实现。
示例:
项目基础环境搭建
(1)创建chapter05的spring Boot项目,并在依赖中选择Web模块下的Web依赖启动和TemplateEnginess模块下的Thymeleaf依赖启动器,chapter05的内容与项目shitu一样。
功能呢扩展实现
使用Spring Boot整合Spring MVC进行Web开发,实现简单的页面跳转功能。
(1)注册视图管理器在项目的com.itheima.config包下创建一个实现WebMVCConfigure接口的配置类MyMVCconfig。
package com.itheima.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class MyMVCconfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry){
registry.addViewController("/toLoginPage").setViewName("login");
registry.addViewController("/login.html").setViewName("login");
}
访问http://localhost:8080/toLoginPage和http://localhost:8080/login.html
在此之前先注释掉LoginController代码。
可以看出WebMVCConfigure接口定义的用户请求控制方法也实现了用户请求控制跳转的效果。
(2)注册自定义拦截器,在项目com.itheima.config包下创建一个自定义拦截器类MyInterceptor,
package com.itheima.config;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Calendar;
@Component
public class MyInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,Object handler)throws Exception{
String uri=request.getRequestURI();
Object loginUser=request.getSession().getAttribute("loginUser");
if(uri.startsWith("/admin") && null==loginUser){
response.sendRedirect("/toLoginPage");
return false;
}
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handle, @Nullable ModelAndView modelAndView)throws Exception{
request.setAttribute("currentYear", Calendar.getInstance().get(Calendar.YEAR));
}
@Override
public void afterCompletion(HttpServletRequest request,HttpServletResponse response,Object handler,@Nullable Exception ex)throws Exception{
}
}
然后再自定义配置类MyMVCconfig中,重写addInterceptor()方法注册自定义拦截器
@Autowired
private MyInterceptor myInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry){
registry.addInterceptor(myInterceptor).addPathPatterns("/**").excludePathPatterns("/login,html");
}
在注册自定义拦截器是,使用add