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


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

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

先來回顧下springmvc是怎么使用的。
首先需要配置DispatcherServlet,一般是在web.xml中配置

<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,后面講一些常用的配置,不常用的就自己看源碼了解吧,就不細說了。

然后是ViewResolver,也就是視圖處理的配置。在springmvc中,一般是在springmvc的xml配置中添加下列內容

<!-- 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。

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

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

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

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,也就是不能使用@RestController

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/study-springboot/tree/springboot-2/
https://github.com/page12/study-springboot/tree/springboot-2

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM