天天看點

【SpringBoot實戰】實作WEB的常用功能

前言

通常在 Web 開發中,會涉及靜态資源的通路支援、視圖解析器的配置、轉換器和格式化器的定制、檔案上傳下載下傳等功能,甚至還需要考慮到與Web伺服器關聯的 Servlet相關元件的定制。Spring Boot架構支援整合一些常用Web架構,進而實作Web開發,并預設支援Web開發中的一些通用功能。本文将對Spring Boot實作Web開發中涉及的三大元件Servlet、Filter、Listener以及檔案上傳下載下傳功能以及打包部署進行實作。

SpringMVC整合支援

為了實作并簡化Web開發,Spring Boot為一些常用的Web開發架構提供了整合支援,例如 Spring MVC、Spring WebFlux等架構。使用Spring Boot進行Web開發時,隻需要在項目中引入對應Web開發架構的依賴啟動器即可。

Spring MVC自動配置

在Spring Boot項目中,一旦引入了Web依賴啟動器spring-boot-starter-web,那麼SpringBoot整合 Spring MVC 架構預設實作的一些xxxAutoConfiguration自動配置類就會自動生效,幾乎可以在無任何額外配置的情況下進行Web開發。Spring Boot為整合Spring MVC 架構實作Web開發,主要提供了以下自動化配置的功能特性。

(1)内置了兩個視圖解析器:ContentNegotatingViewResolver和BeanNameViewReso

(2)支援靜态資源以及WebJars。

(3)自動注冊了轉換器和格式化器。

(4)支援Http消息轉換器。

(5)自動注冊了消息代碼解析器。

(6)支援靜态項目首頁index.html。

(7)支援定制應用圖示favicon.ico。

(8)自動初始化Web資料綁定器ConfigurableWebBindinglnitializer。

Spring Boot 整合 Spring MVC進行Web開發時提供了很多預設配置,而且大多數時候使用預設配置即可滿足開發需求。例如,Spring Boot整合Spring MVC進行Web開發時,不需要外配置視圖解析器。

Spring MVC功能擴充實作

Spring Boot 整合 Spring MVC進行Web開發時提供了很多的自動化配置,但在實際開發中還需要開發者對一些功能進行擴充實作。下面我們通過一個具體的案例講解 Spring Boot整合Spring MVC架構實作Web開發的擴充功能。

項目基礎環境搭建

使用Spring Inifializr方式建立名稱為springboot02的Spring Boot項目,并導入Web依賴和Thymeleaf依賴。

讓後我們啟動該項目通路http://localhost:8080/ 可以看到下面的界面就表示通路成功,也代表我們項目建立成功。

【SpringBoot實戰】實作WEB的常用功能

我們在resources下的templates包裡建立一個登入界面login.html

<!DOCTYPE html>
<html>
<head>
    <title>login</title>

</head>
<body>
<form>
    <input type="text" name="username">
    <input type="password" name="password">
    <input type="submit" value="submit">
</form>
</body>
</html>
           

最後在com.hjk包下建立controller包并建立LoginController類

package com.hjk.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.Calendar;


@Controller
public class LoginController {
    /**
     * 擷取并封裝目前年份跳轉到登入頁login.html
     */
    @GetMapping("/toLoginPage")
    public String toLoginPage(Model model){
        model.addAttribute("currentYear", Calendar.getInstance().get(Calendar.YEAR));
        return "login";
    }
}
           

功能擴充實作

接下來使用Spring Boot 整合Spring MVC進行Web開發,實作簡單的頁面跳轉功能,這裡我們将使用Spring Boot提供的WebMvcConfigurer接口編寫自定義配置,并對Web功能進行适當擴充。我們在這裡分别示範視圖管理器和攔截器的實作。

注冊視圖管理器

在springboot項目的 com.hjk下建立config包并建立一個實作WebMvcConfigurer 接口的配置類 MyMVCconfig,用于對 MVC架構功能進行擴充

package com.hjk.config;


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;

import javax.annotation.Resource;

@Configuration
public class MyMVCconfig implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry){
        registry.addViewController("/toLoginPage").setViewName("login");
        registry.addViewController("/login.html").setViewName("login");
    }

}
           
  • MMVCconig實作了接口 WebMvcConigurer 的addViewControllerse(ViewControllerRegistry registry)方法。在addViewControllers()方法内部,使用ViewControllerRegistry的 addviewController()方法分别定義了“tologinPage”和“login.html”的請求控制,并使setViewName("login")方法将路徑映射為login.html頁面。

    定制完MVC的視圖管理功能後,

  • 就可以進行效果測試了。為了示範這種定制效果,重新開機chapter05項目,項目啟動成功态,在浏覽器上分别通路http://localhost:8080/toLoginPage和http://localhost:8080/login.htm 都可以通路login.html頁面
  • 使用WebMvcConfigurer接口定義的使用者請求控制方法也實作了使用者請求控制跳轉的效果,相比于傳統的請求處理方法而言,這種方法更加簡潔、直覺和友善。同時也可以看出,使用這種方式無法擷取背景處理的資料。需要說明的是,使用WebMvcConfigurer 接口中的addViewControllers(ViewControllelRegistry registry)方法定制視圖控制,隻适合較為簡單的無參數視圖Get方式請求,有參數或需要業務處理的跳轉需求,最好還是采用傳統方式處理請求。
注冊自定義攔截器

WebMvcConfigurer接口提供了許多MVC開發相關方法,添加攔截器方法addInterceptors(),添加格式化的器的方法addFormatters()我們這裡實作攔截器的方法。

我們在config包下建立一個自定義攔截器類MyInterceptor,代碼如下。

package com.hjk.config;

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.io.IOException;
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){
            try {
                response.sendRedirect("/toLoginPage");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return false;
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
       System.out.println("攔截器攔截");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
           
  • 自定義攔截器類Mylnterceptor實作了HandlerInterceptor接口。在preHandle()方法方法中,如果使用者請求以“/admin”開頭,即通路如http://localhost:8080/admin 的位址則判斷使用者是否登入,如果沒有登入,則重定向到“hoLoginPage”請求對應的登入頁面。
  • 在postHandle()方法中,在控制台列印攔截器攔截。

然後在config包下自定義配置類MyMVCconfig中,重寫addlnterceptors()方法注冊自定義的攔截器。添加以下代碼。

@Autowired
private MyInterceptor myInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(myInterceptor).addPathPatterns("/**").excludePathPatterns("/login.html");
}
           
  • 先使用@Autowired注解引入自定義的 Mylnterceptor攔截器元件,然後重寫其中的 addinterceptors()方法注冊自定義的攔截器。在注冊自定義攔截器時,使用addPathPatterns("/**)方法攔截所有路徑請求,excludePathPatterns("/login.htm")方法對“login.html”路徑的請求進行了放行處理。

測試:我們可以通路http://localhost:8080/admin 可以發現它重定向大toLoginPage界面了。

Spring整合Servlet三大元件

在這裡我們使用元件注冊方式對Servlet、Filter、Listener三大元件進行整合,我們隻需要将自定義的元件通過ServletRegistrationBean、FilterRegistrationBean、ServletListenerRegistrationBean類注冊到容器中即可。

使用注冊方式整合

使用元件注冊方式整合Servlet

我們在com.hjk包下建立servletComponent的包,在該包下建立MyServlet類并繼承HttpServlet類。

package com.hjk.servletCompont;

import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Component
public class MyServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().write("hello MyServlet");
    }
}
           
  • @Component注解将MyServlet類作為元件注入Spring容器。MySeret類繼承自HttpServlet,通過HttpServletResponse對象向頁面輸出“hello MyServlet”。

建立 Servlet元件配置類。在項目com.hjk.confg包下建立一個Servlet元件配置類servietConfig,用來對 Servlet相關元件進行注冊,

package com.hjk.config;

import com.hjk.servletCompont.MyServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ServletConfig {
    @Bean
    public ServletRegistrationBean getServlet(MyServlet myServlet){
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(myServlet, "/myServlet");
        return registrationBean;
    }

}
           
  • 使用@Configuration 注解将ServletConfig标注為配置類,ServletConfig類内部的 getServlet()方法用于注冊自定義的MyServlet,并傳回 ServletRegistrationBean類型的Bean對象。

測試:項目啟動成功後,在浏覽器上通路“http://localhost:8080/myServlet"myServlet并正常顯示資料,說明 Spring Boot成功整合Servlet元件。

使用元件注冊方式整合Filter

在servletCompont包下建立一個MyFilter類并實作Filter接口,這個Filter的包别導錯了

package com.hjk.servletCompont;


import org.springframework.stereotype.Component;

import javax.servlet.*;
import java.io.IOException;
@Component
public class MyFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("hello MyFilter");
    }

    @Override
    public void destroy() {

    }
}

           

在config包下的ServletConfig類中進行注冊,即在該類中添加方法。

@Bean
public FilterRegistrationBean getFilter(MyFilter myFilter){
    FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(myFilter);
    filterRegistrationBean.setUrlPatterns(Arrays.asList("/toLogin","/myFilter"));
    return filterRegistrationBean;
}
           
  • 使用 setUrilPatterns(Arrays.asList("/toLoginPage",/myFilter')方法定義了過濾的請求路徑

    “/toLoginPage”和“/myFilter”,同時使用@Bean 注解将目前組裝好的FilterRegistrationBea對象作為Bean元件傳回。

測試:在浏覽器上通路“http://localhost:8080/myFilter”檢視控制台列印效果(由于沒有編寫對應路徑的請求處理方法,是以浏覽器會出現404 錯誤頁面,這裡重點關注控制台即可),浏覽器通路“http://localhost:8080/

myFilter”時,控制台列印出了自定義 Filter中定義 圖5-6 使用元件注冊方式整合Filter的運作結果的輸出語句“hello MyFilter”,這也就說明Spring Boot 整合自定義Filter 元件成功。

使用元件注冊方式整合 Listener

(1)建立自定義Listener類。在com.itheima.senleiComponent包下建立一個類MyListener實作ServletContextListener接口

package com.hjk.servletCompont;

import org.springframework.stereotype.Component;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

@Component
public class MyListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("contextnitialized...");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("contextDestroyed...");

    }
}
           

在servletConfig添加注冊

@Bean
public ServletListenerRegistrationBean getServletListener(MyListener myListener){
    ServletListenerRegistrationBean servletListenerRegistrationBean = new ServletListenerRegistrationBean(myListener);
    return servletListenerRegistrationBean;
}
           

需要說明的是,Servlet 容器提供了很多 Listener 接口,例如 ServletRequestListener、ritpSessionListener、ServletContextListener等,我們在自定義Listener類時要根據自身需求選擇實作對應接口即可。

測試:程式啟動成功後,控制台會列印出自定義Listener元件中定義的輸出語句“contextlnitialized..”。單擊圖中的【Exit】按鈕關閉目前項目(注意,如果直接單擊紅色按鈕會強制關閉程式,浏覽器就無法列印關閉監聽資訊),再次檢視控制台列印效果。

程式成功關閉後,控制台列印出了自定義Listener元件中定義的輸出語句“contextDestroyed..”。通過效果示範,說明了Spring Boot整合自定義Listener元件成功。

檔案上傳與下載下傳

開發web應用時,檔案上傳是很常見的一個需求,浏覽器通過表單形式将檔案以流的形式傳遞給伺服器,伺服器在對上傳的資料解析處理。

檔案上傳

編寫上傳表單界面

這個表單界面名為upload.html,在templates檔案夾下

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>檔案上傳</title>
</head>
<body>
<div style="text-align: center">
    <form action="/uploadFile" method="post" enctype="multipart/form-data">
        上傳:<input type="file" name="filename"/>
        <input type="submit" value="submit"/>
    </form>
</div>
</body>
</html>
           

我們通過表單上傳檔案,表單送出給uploadFile控制器,送出方式為post必須為這種方式,因為get上傳比較少,必須包含enctype="multipart/form-data".

我們通過送出的位址也應該清楚,我們肯定會寫一個uploadFile的控制器。

添加檔案上傳的相關配置

我們在application.properties檔案中添加配置,上傳檔案的大小限制。

## 檔案最大限制為10mb,預設為1mb
spring.servlet.multipart.max-file-size=1MB
           
  • 如果檔案超過限制大小,會報錯。

編寫控制器

我們在com.hjk.controller包下船艦一個名為FileController的類,用于實作檔案上傳的控制器。

我們這個檔案上傳隻是實作一個簡單的檔案上傳,并沒有考慮上傳檔案重名的情況,實際上重名的話會覆寫之前的檔案。要實作檔案上傳,我們肯定要給它一個唯一名稱這個可以使用uuid實作,這裡也沒考慮檔案存放位置問題,都是我自己把位址寫死了,這裡我們就不實作了。

實作曆程:寫這個控制器的時候,我的代碼是正确的,前端檔案也能送出,但是後端擷取的檔案就是null,我也看了很多部落格,有的說是沒有注冊multipartResolver這個Bean,有的說是版本問題等等,但是都沒有解決。最後一個不經意的小細節導緻了我這次的代碼不能擷取到檔案。那就是我們有在

(@RequestParam("filename") MultipartFile file)

前面加@RequestParam這個注解。反正我的這個是加上之後就能用了,我的這個springboot版本是2.6.6.至于真正原因現在不想思考了,等以後遇到再改吧。

  • @RequestPara("filename")必須擷取參數名為filename的file參數
  • @RequestParam()預設為必傳屬性,可以通過@RequestParam(required = false)設定為非必傳。因為required值預設是true,是以預設必傳
  • @RequestParam("filename")或者@RequestParam(value = "filename")指定參數名
  • @RequestParam(defaultValue = "0")指定參數預設值
package com.hjk.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@Controller
public class FileController {
    @GetMapping("/toUpload")
    public String toUpload(){
        return "upload";
    }


    @RequestMapping(value = "/uploadFile",method = RequestMethod.POST)
    public String uploadFile(@RequestParam("filename") MultipartFile file){
        String filename = file.getOriginalFilename();
        String dirPath = "D:/file/";
        File filePath = new File(dirPath);

        if (!filePath.exists()){
            filePath.mkdir();
        }
        try {
            file.transferTo(new File(dirPath+filename));
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "upload";
    }

}
           

在這裡我們送出三張圖檔用于下面的檔案下載下傳

【SpringBoot實戰】實作WEB的常用功能

檔案下載下傳

檔案下載下傳很多架構都沒有進行封裝處理,不同的浏覽器解析處理不同,有可能出現亂碼情況。

在添加完依賴之後我們建立一個名為filedownload.html的html,一會用于編寫下載下傳界面。

添加依賴

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.6</version>
</dependency>
           

下載下傳處理控制器

我們還是再FileController類裡添加下載下傳處理方法。直接在裡面添加就行。

@GetMapping("/toDownload")
public String toDownload(){
    return "filedownload";
}


@GetMapping("/download")
public ResponseEntity<byte[]> fileDownload(String filename){
    //指定下載下傳位址檔案路徑
    String dirPath = "D:/file/";
    //建立檔案下載下傳對象
    File file = new File(dirPath + File.separator + filename);
    //設定響應頭
    HttpHeaders httpHeaders = new HttpHeaders();
    //通知浏覽器以下載下傳方式打開
    httpHeaders.setContentDispositionFormData("attachment",filename);
    //定義以流的形式下載下傳傳回檔案
    httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);

    try {
        return new ResponseEntity<>(FileUtils.readFileToByteArray(file),httpHeaders, HttpStatus.OK);
    } catch (IOException e) {
        e.printStackTrace();
        return new ResponseEntity<byte[]>(e.getMessage().getBytes(), HttpStatus.EXPECTATION_FAILED);
    }

}
           

編寫前端代碼

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>檔案下載下傳</title>
</head>
<body>
<div style="margin-bottom: 10px">檔案下載下傳清單</div>
<table>
    <tr>
        <td>0000001.jpg</td>
        <td><a th:href="@{/download(filename='0000001.jpg')}">下載下傳檔案</a> </td>
    </tr>
    <tr>
        <td>0000002.jpg</td>
        <td><a th:href="@{/download(filename='0000002.jpg')}">下載下傳檔案</a> </td>
    </tr>
    <tr>
        <td>0000003.jpg</td>
        <td><a th:href="@{/download(filename='0000003.jpg')}">下載下傳檔案</a> </td>
    </tr>
</table>
</body>
</html>
           

我們這次使用了thymeleaf寫前端代碼。

實際上我們可能會遇到下載下傳中文檔案的問題,那樣可能會亂碼。

我麼在這裡寫一個解決中文亂碼的例子。例如:我把0000001.jpg改為"你好jpg"再重新部署下載下傳,會發現名字為_.jpg

下面我們直接在我們在fileController類的裡面加一個getFileName方法,并修改fileDownload方法上做修改。

public String getFileName(HttpServletRequest request,String filename) throws Exception {
    String[] IEBrowserKeyWords = {"MSIE","Trident","Edge"};
    String userAgent = request.getHeader("User-Agent");
    for (String ieBrowserKeyWord : IEBrowserKeyWords) {
        if (userAgent.contains(ieBrowserKeyWord)){
            return URLEncoder.encode(filename,"UTF-8").replace("+"," ");
        }
    }
    return new String(filename.getBytes(StandardCharsets.UTF_8),"ISO-8859-1");
}



   @GetMapping("/download")
    public ResponseEntity<byte[]> fileDownload(HttpServletRequest request,String filename) throws Exception {
        //指定下載下傳位址檔案路徑
        String dirPath = "D:/file/";
        //建立檔案下載下傳對象
        File file = new File(dirPath + File.separator + filename);
        //設定響應頭
        HttpHeaders httpHeaders = new HttpHeaders();
        //通知浏覽器下載下傳七千及性能轉碼
        filename = getFileName(request,filename);
        //通知浏覽器以下載下傳方式打開
        httpHeaders.setContentDispositionFormData("attachment",filename);
        //定義以流的形式下載下傳傳回檔案
        httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);

        try {
            return new ResponseEntity<>(FileUtils.readFileToByteArray(file),httpHeaders, HttpStatus.OK);
        } catch (IOException e) {
            e.printStackTrace();
            return new ResponseEntity<byte[]>(e.getMessage().getBytes(), HttpStatus.EXPECTATION_FAILED);
        }

    }
           

SpringBoot的打包部署

springboot使用的嵌入式Servlet容器,是以預設是以jar包打包的。也可以進行war包打包,但是需要進行一些配置。

jar包形式打包

我們在建立springboot項目是預設會給我們導入maven的打包插件,如果沒有我們手動加上即可。

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
           

輕按兩下package等待即可

【SpringBoot實戰】實作WEB的常用功能

等待完成,可以看到打包時間,存放jar包位置等資訊。我們也可以在target包下檢視打成的jar包。

【SpringBoot實戰】實作WEB的常用功能

啟動jar包

我們可以在關閉已啟動的springboot項目後,在idea控制台輸入指令啟動。

java -jar target\springboot02-0.0.1-SNAPSHOT.jar
           

我們也可以在系統自帶的終端視窗啟動

war包形式打包

我們首先要把預設打包方式修改為war包

<name>springboot02</name>
<description>Demo project for Spring Boot</description>
<packaging>war</packaging>
<properties>
    <java.version>1.8</java.version>
</properties>
           

導入外部Tomcat伺服器

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>
           

打開啟動類,繼承springbootServletInitializer類

package com.hjk;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@ServletComponentScan
@SpringBootApplication
public class Springboot02Application extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(Springboot02Application.class, args);
    }


    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(Springboot02Application.class);
    }
}
           

然後就和jar包方式一樣了,輕按兩下package,等待打包完成。

war包的部署

war包的部署相比于jar包比較麻煩,我們需要外部的伺服器,我們需要把war包複制到tomcat安裝目錄下的webapps目錄中,執行目錄裡的startup.bat指令啟動war包,這樣我們就完成了。

總結

我們對MVC進行了功能擴充和定制、servlet三大元件定制、檔案上傳和下載下傳、以及兩種方式打包部署。