springboot學習(二)——springmvc配置使用

如下內容,若有問題,煩請指出,謝謝html

上一篇講解了springboot的helloworld部分,這一篇開始講解如何使用springboot進行實際的應用開發,基本上尋着spring應用的路子來說,從springmvc以及web開發講起。
官方文檔中在helloworld和springmvc之間還有一部份內容,主要講了spring應用的啓動、通用配置以及日誌配置相關的內容,其中關於通用配置的部分對於springboot來講是個很重要的內容,這部分等到後面在細說下,有了必定的應用能力,到時候理解起來輕鬆些。java

先來回顧下springmvc是怎麼使用的。
首先須要配置DispatcherServlet,通常是在web.xml中配置python

<servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

這一點在springboot中就不須要手動寫xml配置了,springboot的autoconfigure會默認配置成上面那樣,servlet-name並非一個特別須要注意的屬性,所以能夠不用太關心這個屬性是否一致。
具體的初始化是在 org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration中完成的,對應的一些配置在 org.springframework.boot.autoconfigure.web.ServerProperties 以及
org.springframework.boot.autoconfigure.web.WebMvcProperties,後面講一些經常使用的配置,不經常使用的就本身看源碼瞭解吧,就不細說了。git

而後是ViewResolver,也就是視圖處理的配置。在springmvc中,通常是在springmvc的xml配置中添加下列內容github

<!-- ViewResolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <property name="suffix" value=".jsp"/>
</bean>

低版本的spring須要加上viewClass,高版本的spring會自動檢測是否使用JstlView,所以這個屬性一般並不須要手動配置,主要關心prefix和suffix。另外高版本的springmvc也不須要手動指定 HandlerMapping 以及
HandlerAdapter ,這兩個也不須要顯式聲明bean。web

如何在springboot中配置ViewResolver,主要有兩種方法。
一種是在application.properties中配置,這是springboot中標準的配置方法。spring

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

另一種是springmvc中的代碼式配置,這種也是如今比較流行的配置方式,不顯式指定配置文件,配置即代碼的思想,腳本語言python js scala流行的配置方式。
代碼是配置的主要操做就是本身寫代碼來爲mvc配置類中的某個方法注入bean或者直接覆蓋方法,以下:apache

package pr.study.springboot.configure.mvc;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
public class SpringMvcConfigure extends WebMvcConfigurerAdapter {

    @Bean
    public InternalResourceViewResolver viewResolver(){
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/jsp/");
        viewResolver.setSuffix(".jsp");
        // viewResolver.setViewClass(JstlView.class); // 這個屬性一般並不須要手動配置,高版本的Spring會自動檢測
        return viewResolver;
    }
}

上面的視圖使用的是jsp,這時須要在pom.xml中添加新的依賴。編程

<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <scope>provided</scope>
</dependency>

代碼也相應的改寫下,返回視圖時,不能使用@ResponseBody,也就是不能使用@RestControllertomcat

package pr.study.springboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

//@RestController
@Controller
public class HelloWorldController {

    @RequestMapping("/hello")
    public ModelAndView hello() {
        ModelAndView mv = new ModelAndView();
        mv.addObject("msg", "this a msg from HelloWorldController");
        mv.setViewName("helloworld");;
        return mv;
    }
}

還要新建jsp文件,路徑和使用普通的tomcat部署同樣,要在src/main/webapp目錄下新建jsp的文件夾,這裏路徑不能寫錯。

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>helloworld</title>
</head>
<body>
  <p>${msg}</p>
</body>
</html>

springboot使用嵌入式Servlet容器,對jsp支持有限,官方是推薦使用模板引擎來代替jsp。

第三點講下比較重要的springmvc攔截器(HandlerInterceptor)。
攔截器在springmvc中有重要的做用,它比servlet的Filter功能更強大(攔截器中能夠編程的地方更多)也更好使用,缺點就是它只能針對springmvc,也就是dispatcherServlet攔截的請求,不是全部servlet都能被它攔截。
springmvc中添加攔截器配置以下:

<mvc:interceptors>
    <bean class="pr.study.springboot.aop.web.interceptor.Interceptor1"/>  
    <mvc:interceptor>
        <mvc:mapping path="/users" />
        <mvc:mapping path="/users/**" />
        <bean class="pr.study.springboot.aop.web.interceptor.Interceptor2"/>
    </mvc:interceptor>
</mvc:interceptors>

Interceptor1攔截全部請求,也就是/**,Interceptor2只攔截/users開頭的請求。
在springboot中並無提供配置文件的方式來配置攔截器(HandlerInterceptor),所以須要使用springmvc的代碼式配置,配置以下:

package pr.study.springboot.configure.mvc;

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.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import pr.study.springboot.aop.web.interceptor.Interceptor1;
import pr.study.springboot.aop.web.interceptor.Interceptor2;

@Configuration
public class SpringMvcConfigure extends WebMvcConfigurerAdapter {

    @Bean
    public InternalResourceViewResolver viewResolver(){
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/jsp/");
        viewResolver.setSuffix(".jsp");
        // viewResolver.setViewClass(JstlView.class); // 這個屬性一般並不須要手動配置,高版本的Spring會自動檢測
        return viewResolver;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new Interceptor1()).addPathPatterns("/**");
        registry.addInterceptor(new Interceptor2()).addPathPatterns("/users").addPathPatterns("/users/**");
        super.addInterceptors(registry);
    }
}

第四點講下靜態資源映射。
一些簡單的web應用中都是動靜混合的,包含許多靜態內容,這些靜態內容並不須要由dispatcherServlet進行轉發處理,不須要進行攔截器等等的處理,只須要直接返回內容就行。springmvc提供了靜態資源映射這個功能,在xml中配置以下:

<resources mapping="/**" location="/res/" />

springboot中有兩種配置,一種是經過配置文件application.properties指定

# default is: /, "classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"
#spring.resources.static-locations=classpath:/res/
# the 'staticLocations' is equal to 'static-locations'
#spring.resources.staticLocations=classpath:/res/

# default is /**
#spring.mvc.staticPathPattern=/**

另一種是springmvc的代碼式配置

@Configuration
public class SpringMvcConfigure extends WebMvcConfigurerAdapter {

    @Bean
    public InternalResourceViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/jsp/");
        viewResolver.setSuffix(".jsp");
        // viewResolver.setViewClass(JstlView.class); // 這個屬性一般並不須要手動配置,高版本的Spring會自動檢測
        return viewResolver;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new Interceptor1()).addPathPatterns("/**");
        registry.addInterceptor(new Interceptor2()).addPathPatterns("/users").addPathPatterns("/users/**");
        super.addInterceptors(registry);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // addResourceHandler指的是訪問路徑,addResourceLocations指的是文件放置的目錄  
        registry.addResourceHandler("/**").addResourceLocations("classpath:/res/");
    }
}

兩種方式效果同樣,配置後訪問正確的靜態文件都不會被攔截器攔截。
固然,靜態文件不被攔截的方法還有不少,好比使用其餘的servlet來轉發靜態文件,攔截器(HandlerInterceptor)的exclude,dispatcherServlet攔截/*.do等等方式,這裏就不細說了。

今天就到此爲止,springmvc以及Web的還有很多內容,下期在說
由於Demo比較簡單,這裏就沒有貼運行結果的圖,相關代碼以下:
https://gitee.com/page12/stud...
https://github.com/page12/stu...

一些有既能夠經過application.properties配置,又可使用代碼式配置的,都註釋掉了application.properties中的配置,試運行時能夠切換下。

相關文章
相關標籤/搜索