SpringBoot之旅第四篇-web開發

1、引言

有了自動配置,springboot使web開發變得簡單,這個在springboot之旅中的第一篇中就有體現,實際的開發中固然不會這麼簡單,不少時候咱們都須要本身去定製一些東西。web開發的東西比較多, 咱們先掌握一些必要知識點,剩下的就是CRUD開發。html

2、靜態資源的映射規則

如今大部分公司都是先後端分離的開發模式,通常做爲後臺開發不用關心前端,只須要提供相應接口,可是有關前端的知識咱們最好仍是能基本掌握一些。咱們先了一套bootstrap框架,而後開始進行開發。前端

在以前的web開發中,在main目錄下面會有webapp文件夾,咱們將全部的靜態資源放在裏面,可是springboot的默認生成中並無這個文件夾,那麼springboot是怎麼映射靜態資源。java

ctrl+N快捷鍵,找到WebMvcAutoConfiguration類,再找到裏面的addResourceHandlers 方法jquery

public void addResourceHandlers(ResourceHandlerRegistry registry) {
   if (!this.resourceProperties.isAddMappings()) {
      logger.debug("Default resource handling disabled");
      return;
   }
   Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
   CacheControl cacheControl = this.resourceProperties.getCache()
         .getCachecontrol().toHttpCacheControl();
   //webjar形式
   if (!registry.hasMappingForPattern("/webjars/**")) {
      customizeResourceHandlerRegistration(registry
            .addResourceHandler("/webjars/**")
            .addResourceLocations("classpath:/META-INF/resources/webjars/")
            .setCachePeriod(getSeconds(cachePeriod))
            .setCacheControl(cacheControl));
   }
    //匹配/**
   String staticPathPattern = this.mvcProperties.getStaticPathPattern();
   if (!registry.hasMappingForPattern(staticPathPattern)) {
      customizeResourceHandlerRegistration(
            registry.addResourceHandler(staticPathPattern)
                  .addResourceLocations(getResourceLocations(
                      //映射的資源文件夾
                        this.resourceProperties.getStaticLocations()))
                  .setCachePeriod(getSeconds(cachePeriod))
                  .setCacheControl(cacheControl));
   }
}

2.1 webjars

這裏的代碼告訴咱們:若是是訪問/webjars/**下的請求 ,都去 classpath:/META-INF/resources/webjars/ 找資源。webjars是指以jar包的方式引入靜態資源。打開https://www.webjars.org/ ,能夠找到咱們前端開發經常使用的一些組件,咱們選擇相應的版本,例:web

 

<dependency>
  <groupId>org.webjars</groupId>
  <artifactId>jquery</artifactId>
  <version>3.3.1-1</version>
</dependency>

引入後能夠看到jquer文件被引入了:spring

若是順利的話,此時訪問http://localhost:8080/webjars/jquery/3.3.1-1/jquery.js能夠獲得文件,結果以下:json

2.2 本身的靜態文件

另外當訪問當前項目的任何資源,都去(靜態資源的文件夾)找映射,資源文件夾是一個數組,包括:bootstrap

"classpath:/META-INF/resources/", "classpath:/resources/","classpath:/static/", "classpath:/public/" ,後端

"/":當前項目的根路徑。只要將靜態文件放入其中,那麼springboot就能找到。數組

2.3 首頁

在訪問"/**",會去找靜態資源文件夾下的全部index.html頁面。

2.4 圖標

全部的 **/訪問都是靜態資源文件下找favicon.ico。

咱們將一些靜態文件放在static下,並將index.html放入public文件夾下,如圖:

訪問http://localhost:8080/index.html ,可獲得正確返回

 

3、模板引擎

模板引擎有不少,如JSP、Velocity、Freemarker、Thymeleaf,springboot推薦的是Thymeleaf,那咱們就來簡單看看Thymeleaf語法。導入starter:

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

進入以後能夠看到默認版本,咱們也能夠改爲本身須要的版本。

<thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
<!-- 佈局功能的支持程序  thymeleaf3主程序  layout2以上版本 -->
<!-- thymeleaf2   layout1-->
<thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version>

3.1 Thymeleaf使用

經過源碼咱們知道,只要咱們把HTML頁面放在classpath:/templates/,thymeleaf就能自動渲染

@ConfigurationProperties(prefix = "spring.thymeleaf")
public class ThymeleafProperties {
private static final Charset DEFAULT_ENCODING = Charset.forName("UTF-8");
private static final MimeType DEFAULT_CONTENT_TYPE = MimeType.valueOf("text/html");
public static final String DEFAULT_PREFIX = "classpath:/templates/";
public static final String DEFAULT_SUFFIX = ".html";

 

咱們能夠去官網查看教程,這裏只是簡單的進行介紹,主要步驟

第一步:導入命名空間,導入以後會有相應提示

<html lang="en" xmlns:th="http://www.thymeleaf.org">

第二步:使用語法

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>成功!</h1>
  <!--th:text 將div裏面的文本內容設置爲 -->
<div><th th:text="${hello}"></th>這是顯示歡迎信息</div>
</body>
</html>

 

更具體的使用方法,能夠去查看官網教程,這種若是沒有使用到的話不建議花太多時間去學,不少公司都是先後端分離,即便不是先後端分離,也有不少前端框架給咱們使用。這些能夠再咱們使用的時候再去學習,速度也是很快的。

 

4、SpringMVC自動配置

4.1 自動配置

springboot默認將爲咱們配置以下一些SpringMvc的必要組件:

  1. 必要的ViewResolver(視圖解析器:根據方法的返回值獲得視圖對象(View)),如ContentNegotiatingViewResolver和BeanNameViewResolver

  2. 將必要的ConverterGenericConverterFormatter 等bean註冊到ioc容器中。

  3. 添加了一系列的HttpMessageConverters以便支持對web請求和相應的類型轉換。

  4. 自動配置和註冊MessageCodesResolver

任什麼時候候,咱們對默認提供的組件設定不滿意,均可以註冊新的同類型的bean定義來替換,web的全部自動場景都在org.springframework.boot.autoconfigure.web包中,咱們能夠參照進行配置。

固然徹底靠自動配置在實際開發時不夠的,咱們常常須要本身配置一些東西,好比攔截器,視圖映射規則。

4.2 擴展配置

在sprinboot2.0以前 配置類繼承WebMvcConfigurerAdapter,可是如今這個方法已通過時,如今可使用兩種方式,繼承WebMvcConfigurer接口或者繼承WebMvcConfigurationSupport類,推薦使用的是WebMvcConfigurationSupport。

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/yuan").setViewName("success");
    }
}

 

這段代碼就實現了自定義的視圖映射。上面這種寫法使SpringMVC的自動配置和咱們的擴展配置都會起做用

咱們甚至能夠全面接管springmvc,只要在配置類中增長@EnableWebMv註解,這樣全部的SpringMVC的自動配置都失效了。固然,通常狀況下咱們不會這麼作。

 

5、登錄

web系統通常少不了登陸頁面,咱們先設定默認頁面爲登陸頁。

registry.addViewController("/").setViewName("login");
registry.addViewController("/index.html").setViewName("login");

5.1 登陸方法

具體登陸html的代碼就不貼了,能夠下載源碼查看,新建controller

@Controller
public class LoginController {

    @PostMapping(value = "/user/login")
    public String login(@RequestParam("username") String username,
                        @RequestParam("password") String password,
                        Map<String,Object> map, HttpSession httpSession){
        if(!StringUtils.isEmpty(username)&&
        "123456".equals(password)){
             //設置session
             httpSession.setAttribute("loginUser",username);
            //重定向到主頁
            return "redirect:/main.html";
        }else {
            map.put("msg","用戶名密碼錯誤");
            return "login";
        }
    }
}

 

5.2 登陸攔截器

登陸操做完成以後,爲了對每一個頁面進行登陸驗證,咱們還須要設置登陸攔截器。先建立登陸攔截器

@Component
public class LoginHandlerInterceptor implements HandlerInterceptor {

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Object user = request.getSession().getAttribute("loginUser");
        if(user == null){
            //未登錄,返回登錄頁面
            request.setAttribute("msg","沒有權限請先登錄");
            request.getRequestDispatcher("/index.html").forward(request,response);
            return false;
        }else{
            //已登錄,放行請求
            return true;
        }
    }
}

 

而後再加入配置

@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Autowired
    private LoginHandlerInterceptor loginHandlerInterceptor;
    
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginHandlerInterceptor).addPathPatterns("/**")
                .excludePathPatterns("/index.html","/","/user/login");
    }
 }

這樣在訪問其餘頁面時都會進行登陸攔截操做

 

6、錯誤處理機制

在進行開發時,錯誤處理是很是重要的,不論是直接顯示給用戶,或者返回給前端,都須要儘可能友好和清晰。

6.1 默認的錯誤處理機制

springboot有自身的默認錯誤處理機制,分爲兩種

第一種:瀏覽器,瀏覽器會返回一個默認的錯誤頁面,如:

第二種:客戶端,客戶端默認返回的是一個響應一個json數據

若是咱們用postman訪問,則返回:

6.2 定製錯誤響應

定製錯誤響應也分爲兩種,一種是定製錯誤頁面,第二種是定製錯誤json數據。

6.2.1 定製錯誤頁面

若是咱們想要展現更加詳細的信息,就將頁面放在模板引擎文件夾下,路徑名爲 error/狀態碼,【將錯誤頁面命名爲錯誤狀態碼.html 放在模板引擎文件夾裏面的 error文件夾下】,發生此狀態碼的錯誤就會來到 對應的頁面。在這個頁面咱們能夠獲取到一些錯誤信息,如:

  • timestamp:時間戳

  • status:狀態碼

  • error:錯誤提示

  • exception:異常對象

  • message:異常消息

  • errors:JSR303數據校驗的錯誤都在這裏

咱們能夠根據這些錯誤信息來展現錯誤,通常不須要這麼作,拋出的錯誤不該該讓用戶去分析,咱們只須要返回靜態頁面便可,返回錯誤靜態頁面是作法也是同樣的,只是咱們不用將文件放在模板引擎文件夾下。

6.2.2 定製錯誤的json數據

在實際的開發中咱們會對咱們的錯誤碼進行規範處理,根據錯誤會返回相應的錯誤碼,因此咱們會本身進行json數據包裝處理。

@ControllerAdvice
public class GlobalDefaultExceptionHandler {

    @ExceptionHandler(value = RequestException.class)
    public String requestExceptionHandler(RequestException e,HttpServletRequest request){
        Map<String,Object> map = new HashMap<>();
        //傳入咱們本身的錯誤狀態碼  4xx 5xx,不然就不會進入定製錯誤頁面的解析流程
        request.setAttribute("javax.servlet.error.status_code",500);
        map.put("code","user.notexist");
        map.put("message",e.getMessage());
        //轉發到/error
        return "forward:/error";
    }
}

7、配置嵌入式Servlet容器

springboot默認使用Tomcat做爲嵌入式的Servlet容器,咱們既能夠修改Tomcat的一些屬性配置,也可使用其餘的Servlet容器,咱們這篇就來學習嵌入式Servlet容器的配置。

7.1 、定製和修改Servlet容器的相關配置

servlet的配置類爲ServerProperties,進入代碼能夠看到server可以配置的屬性,咱們能夠對此進行修改。

@ConfigurationProperties(prefix = "server", ignoreUnknownFields = true)
public class ServerProperties {

咱們既能夠修改通用的Servlet容器設置,如:

server:
  port: 8089

也能夠修改某一種容器的配置,如:

server:
  tomcat:
    uri-encoding: utf-8

7.2 、註冊Servlet三大組件【Servlet、Filter、Listener】

註冊三大組件用如下方式:

Servlet:ServletRegistrationBean

建立一個MyServlet類:

public class MyServlet extends HttpServlet {
    @Override
   public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
    @Override
   public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       resp.getWriter().write("hello MyServlet");
    }
}

注入容器:

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

Filter:FilterRegistrationBean

建立MyFilter:

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("My filter process");
        filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void destroy() {

    }
}

 

注入容器:

@Bean
public FilterRegistrationBean myFilter(){
    FilterRegistrationBean registrationBean = new FilterRegistrationBean();
    registrationBean.setFilter(new MyFilter());
    registrationBean.setUrlPatterns(Arrays.asList("/hello","/myServlet"));
    return registrationBean;
}

 

Listener:ServletListenerRegistrationBean

建立MyListener:

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

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

 

注入容器

@Bean
public ServletListenerRegistrationBean myListener(){
    ServletListenerRegistrationBean<MyListener> registrationBean = new ServletListenerRegistrationBean<>(new MyListener());
    return registrationBean;
}

7.三、 替換爲其餘嵌入式Servlet容器

springboot默認爲tomcat容器,要替換其餘容器就必須修改pom依賴

Jetty:

<!-- 引入web模塊 -->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
   <exclusions>
      <exclusion>
         <artifactId>spring-boot-starter-tomcat</artifactId>
         <groupId>org.springframework.boot</groupId>
      </exclusion>
   </exclusions>
</dependency>

<!--引入其餘的Servlet容器-->
<dependency>
   <artifactId>spring-boot-starter-jetty</artifactId>
   <groupId>org.springframework.boot</groupId>
</dependency>

 

Undertow:

<!-- 引入web模塊 -->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
   <exclusions>
      <exclusion>
         <artifactId>spring-boot-starter-tomcat</artifactId>
         <groupId>org.springframework.boot</groupId>
      </exclusion>
   </exclusions>
</dependency>

<!--引入其餘的Servlet容器-->
<dependency>
   <artifactId>spring-boot-starter-undertow</artifactId>
   <groupId>org.springframework.boot</groupId>
</dependency>

 

以上是咱們在web開發須要先掌握的一些基本技術,有了這些基本知識以後,咱們就能夠進行CRUD開發,固然在實際的開發中,不論是登陸攔截仍是錯誤處理都比這個要複雜,咱們之後再詳講。

相關文章
相關標籤/搜索