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

一些有既能够经过application.properties配置,又可使用代码式配置的,都注释掉了application.properties中的配置,试运行时能够切换下。

相关文章
相关标签/搜索