资讯专栏INFORMATION COLUMN

springboot学习(二)——springmvc配置使用

hiyayiji / 1234人阅读

摘要:中添加拦截器配置如下拦截所有请求,也就是,只拦截开头的请求。在中并没有提供配置文件的方式来配置拦截器,因此需要使用的代码式配置,配置如下这个属性通常并不需要手动配置,高版本的会自动检测第四点讲下静态资源映射。

以下内容,如有问题,烦请指出,谢谢

上一篇讲解了springboot的helloworld部分,这一篇开始讲解如何使用springboot进行实际的应用开发,基本上寻着spring应用的路子来讲,从springmvc以及web开发讲起。
官方文档中在helloworld和springmvc之间还有一部分内容,主要讲了spring应用的启动、通用配置以及日志配置相关的内容,其中关于通用配置的部分对于springboot来说是个很重要的内容,这部分等到后面在细说下,有了一定的应用能力,到时候理解起来轻松些。

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


    dispatcherServlet
    org.springframework.web.servlet.DispatcherServlet
    1


    dispatcherServlet
    /

这一点在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配置中添加下列内容



    
    
    

低版本的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中添加新的依赖。


    org.apache.tomcat.embed
    tomcat-embed-jasper
    provided

代码也相应的改写下,返回视图时,不能使用@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"%>




helloworld


  

${msg}

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

第三点讲下比较重要的springmvc拦截器(HandlerInterceptor)。
拦截器在springmvc中有重要的作用,它比servlet的Filter功能更强大(拦截器中可以编程的地方更多)也更好使用,缺点就是它只能针对springmvc,也就是dispatcherServlet拦截的请求,不是所有servlet都能被它拦截。
springmvc中添加拦截器配置如下:


      
    
        
        
        
    

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中配置如下:

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中的配置,试运行时可以切换下。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/70699.html

相关文章

  • Java后端

    摘要:,面向切面编程,中最主要的是用于事务方面的使用。目标达成后还会有去构建微服务,希望大家多多支持。原文地址手把手教程优雅的应用四手把手实现后端搭建第四期 SpringMVC 干货系列:从零搭建 SpringMVC+mybatis(四):Spring 两大核心之 AOP 学习 | 掘金技术征文 原本地址:SpringMVC 干货系列:从零搭建 SpringMVC+mybatis(四):Sp...

    joyvw 评论0 收藏0
  • Spring Boot 《一》开发一个“HelloWorld”的 web 应用

    摘要:一概括,如果使用开发一个的应用创建一个项目并且导入相关包。创建一个编写一个控制类需要一个部署应用的服务器如,特点设计目的是用来简化新应用的初始搭建以及开发过程。启动器可以和位于同一个包下,或者位于的上一级包中,但是不能放到的平级以及子包下。 一,Spring Boot 介绍 Spring Boot不是一个新的框架,默认配置了多种框架使用方式,使用SpringBoot很容易创建一个独立运...

    chaosx110 评论0 收藏0
  • 慕课网_《2小时学会SpringBoot学习总结

    摘要:小时学会学习总结时间年月日星期六说明本文部分内容均来自慕课网。慕课网教学示例源码暂无。数据库操作下第六章事务管理事务管理只有查询的时候不加事务,其它任何操作都要加事务。第七章课程回顾课程回顾总结介绍安装配置的使用数据库操作 《2小时学会SpringBoot》学习总结 时间:2017年2月18日星期六说明:本文部分内容均来自慕课网。@慕课网:http://www.imooc.com教学示...

    aisuhua 评论0 收藏0
  • springboot学习(三)——使用HttpMessageConverter进行http序列化和反

    摘要:序列化反序列化主要体现在程序这个过程中,包括网络和磁盘。如果是开发应用,一般这两个注解对应的就是序列化和反序列化的操作。协议的处理过程,字节流内部对象,就涉及这两种序列化。进行第二步操作,也就是序列化和反序列化的核心是。 以下内容,如有问题,烦请指出,谢谢! 对象的序列化/反序列化大家应该都比较熟悉:序列化就是将object转化为可以传输的二进制,反序列化就是将二进制转化为程序内部的...

    stackfing 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<