SpringBoot无废话入门03:SpringMVC支持

时间:2022-01-15 16:30:29

1.默认配置

Springboot对于路径的默认位置为:

spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/

我们可以将资源文件放置上面的目录中。我们也可以将资源文件放置到webapp中,比如让我们放置一个图片到webapp的static目录下,可以看到,

SpringBoot无废话入门03:SpringMVC支持

2.配置jsp支持

Springboot默认支持Thymeleaf模版引擎,我们可以关闭它转而默认支持jsp。

在application.properties中配置如下,

#jsp 支持

spring.mvc.view.suffix=.jsp

spring.mvc.view.prefix=/WEB-INF/views/

#关闭默认模板引擎

spring.thymeleaf.cache=false

spring.thymeleaf.enabled=false

加入对servlet和jsp的pom依赖,如下:

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<packaging>war</packaging>

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>2.1.3.RELEASE</version>

</parent>

<name>testboot01</name>

<groupId>com.zuikc</groupId>

<artifactId>testboot01</artifactId>

<version>1.0-SNAPSHOT</version>

<dependencies>

<!--jsp支持, servlet 依赖. -->

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>javax.servlet-api</artifactId>

<scope>provided</scope>

</dependency>

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>jstl</artifactId>

</dependency>

<!-- tomcat 的支持.-->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-tomcat</artifactId>

</dependency>

<dependency>

<groupId>org.apache.tomcat.embed</groupId>

<artifactId>tomcat-embed-jasper</artifactId>

<scope>provided</scope>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

</dependencies>

<build>

<plugins>

<plugin>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

</plugin>

</plugins>

</build>

</project>

让我们创建一个新的controller,如StudentController,

package com.zuikc;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.Map;

@Controller

public class StudentController {

@RequestMapping(value = {"test01"})

public String view(Map<String, Object> map) {

map.put("name", "zuikc.com");

map.put("date", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));

return "views/test01";

}

}

继而,让我们去前台创建jsp界面,

SpringBoot无废话入门03:SpringMVC支持

搞定,执行以下吧,看看结果如何:http://localhost:9090/test01

3.自定义消息转换器

可以直接在HelloApplicaiton中直接加入,

@Bean

public StringHttpMessageConverter stringHttpMessageConverter(){

StringHttpMessageConverter converter  = new StringHttpMessageConverter(Charset.forName("UTF-8"));

return converter;

}

4.自定义拦截器

定义一个的WebMvcConfigurerAdapter子类,重点是要将其配置configuration,如下,

package com.zuikc;

import org.springframework.context.annotation.Configuration;

import org.springframework.web.servlet.HandlerInterceptor;

import org.springframework.web.servlet.ModelAndView;

import org.springframework.web.servlet.config.annotation.InterceptorRegistry;

import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

@Configuration

public class MyConfig extends WebMvcConfigurerAdapter {

@Override

public void addInterceptors(InterceptorRegistry registry) {

HandlerInterceptor handlerInterceptor = new HandlerInterceptor() {

@Override

public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)

throws Exception {

System.out.println("自定义拦截器............");

return true;

}

@Override

public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

}

@Override

public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

}

};

registry.addInterceptor(handlerInterceptor).addPathPatterns("/**");

}

}

然后运行之,看看自定义拦截器执行没有哦。

5.自定义配置

在WebMvcConfigurerAdapter的子类中,我们发现还可以override其它的一些方法,比如override configureMessageConverters,这是自定义消息管理器的第二种实现,

@Override

public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {

StringHttpMessageConverter converter  =

new StringHttpMessageConverter(Charset.forName("UTF-8"));

converters.add(converter);

}

通过override这些方法,就可以实现更多的自定义配置。这里就不再一一举例的。

感谢关注“码农星球”。本文版权属于“码农星球”。我们提供咨询和培训服务,关于本文有任何困惑,请关注并联系我们。