SpringBoot--Web开发常见配置二

  1. 拦截器
    1.  
    2. package com.model.config;
      
      import org.springframework.web.servlet.HandlerInterceptor;
      import org.springframework.web.servlet.ModelAndView;
      
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;
      import javax.servlet.http.HttpSession;
      
      /**
       * @Description:测试类
       * @Author: 张紫韩
       * @Crete 2021/8/4 18:31
       */
      public class MyHandlerInterceptor implements HandlerInterceptor {
          @Override
          public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
              HttpSession session = request.getSession();
              return session.getAttribute("loginUser") != null;
          }
      
          @Override
          public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
      
          }
      
          @Override
          public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
      
          }
      }
    3. package com.model.config;
      
      import org.springframework.context.annotation.Bean;
      import org.springframework.context.annotation.Configuration;
      import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
      import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
      
      /**
       * @Description:测试类
       * @Author: 张紫韩
       * @Crete 2021/8/4 18:30
       */
      @Configuration
      //配置类实现扩展类接口
      public class MyWebMvcConfig implements WebMvcConfigurer {
      
          @Override
          public void addInterceptors(InterceptorRegistry registry) {
              registry.addInterceptor(new MyHandlerInterceptor())
                      .addPathPatterns("/**")
                      .excludePathPatterns("/index","/","/toLogin","/doLogin","/css/**","/js/**","/images/**","/fonts/**");
          }
      }

       

  2. 文件上传与下载
    1. <!DOCTYPE html>
      <html lang="en" xmlns:th="http://www.thymeleaf.org">
      <head>
          <meta charset="UTF-8">
          <title>Title</title>
      </head>
      <body>
      
      <form th:action="@{/doLogin}" method="post" enctype="multipart/form-data">
          头像:<input type="file" name="picture">
          <br>
          生活照:<input type="file" name="photos" multiple>
          <br>
          <input type="submit" th:value="提交">
      </form>
      </body>
      </html>
    2. package com.model.controller;
      
      import org.springframework.stereotype.Controller;
      import org.springframework.web.bind.annotation.GetMapping;
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.bind.annotation.RequestParam;
      import org.springframework.web.bind.annotation.RequestPart;
      import org.springframework.web.multipart.MultipartFile;
      
      import java.io.File;
      import java.io.IOException;
      
      /**
       * @Description:测试类
       * @Author: 张紫韩
       * @Crete 2021/8/4 18:43
       */
      @Controller
      public class PageController {
      
          @RequestMapping("/toLogin")
          public String toForm(){
              return "form";
          }
          @RequestMapping("/doLogin")
          public String foForm(@RequestPart("picture")MultipartFile picture,
                               @RequestPart("photos")MultipartFile[] photos) throws IOException {
      
              if (!picture.isEmpty()) {
                  String filename = picture.getOriginalFilename();
                  picture.transferTo(new File("D:\\qq\\IDEA\\IdeaProjects\\technical_framework\\SpringBooot\\springboot11\\src\\main\\resources\\static\\images\\" + filename));
      
              }
              if (photos.length>0){
                  for (MultipartFile photo : photos) {
                      if (!photo.isEmpty()) {
                          String filename = photo.getOriginalFilename();
                          photo.transferTo(new File("D:\\qq\\IDEA\\IdeaProjects\\technical_framework\\SpringBooot\\springboot11\\src\\main\\resources\\static\\images\\" + filename));
                      }
                  }
              }
              return "redirect:/index";
          }
      
      
      }

       

        
  3. 异常处理:
    1. 我们在templates 中自定义 error/404.html,5xx.html 。springboot会对这两个页面进行解析,遇到错误时,会自动跳转到这两个界面

  4.  Web原生组件的注入(Servlet,listener,Filter)

    1. package com.model.servlet;

      import org.springframework.boot.autoconfigure.web.ServerProperties;

      import javax.servlet.ServletException;
      import javax.servlet.annotation.WebServlet;
      import javax.servlet.http.HttpServlet;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;
      import java.io.IOException;

      /**
      * @Description:测试类
      * @Author: 张紫韩
      * @Crete 2021/8/4 20:33
      * 注入源生组件Servlet
      */
      //第一种使用注解,将我们自定义的组件放入到容器中
      // 第二中我们使用配置文件将组件放到容器中
      //@WebServlet(urlPatterns = "/my")
      public class MyServlet extends HttpServlet {
      @Override
      protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
      resp.getWriter().write("6666");
      }
      }
       
    2. package com.model.servlet;
      
      import org.springframework.boot.web.servlet.FilterRegistrationBean;
      import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
      import org.springframework.boot.web.servlet.ServletRegistrationBean;
      import org.springframework.context.annotation.Bean;
      import org.springframework.context.annotation.Configuration;
      
      import java.util.Arrays;
      import java.util.EventListener;
      
      /**
       * @Description:测试类
       * @Author: 张紫韩
       * @Crete 2021/8/4 20:52
       */
      
      @Configuration(proxyBeanMethods = true)//保证我们依赖的组件是单实例的
      public class MyRegistrationBeanConfig {
      
          @Bean
          public ServletRegistrationBean<MyServlet> servletRegistrationBean(){
              MyServlet servlet = new MyServlet();
              ServletRegistrationBean<MyServlet> bean = new ServletRegistrationBean<>(servlet, "/my");
              return bean;
          }
          @Bean
          public FilterRegistrationBean<MyFilter> filterRegistrationBean() {
              MyFilter filter = new MyFilter();
              FilterRegistrationBean<MyFilter> registration = new FilterRegistrationBean<>(filter);
              registration.setUrlPatterns(Arrays.asList("/css/*"));
              return registration;
          }
      
          @Bean
          public ServletListenerRegistrationBean<MyListener> listerRegistrationBean() {
              MyListener listener = new MyListener();
              ServletListenerRegistrationBean<MyListener> bean = new ServletListenerRegistrationBean<>(listener);
              return bean;
          }
      
      }

       

       

  5. 定制化原理
    1.  

    2. 自动配置类会使用import(xxxconfiguration.class)导入配置类,时配置类中的组件生注入到容器中,xxxconfiguration.class (有许多的Bean)

    3.  

       自动配置类会使用@enableconfiguraionpriperties(xxxx.properties)使xxxproperties.class类生效,默认值全部注入到容器中

    4. 一个装有默认值的类:xxxproperties.class(装有所有组件的默认值),且通过configurationproperties(prefix="")绑定主配置文件 

posted @ 2021-08-07 13:40  张紫韩  阅读(54)  评论(0编辑  收藏  举报