前言

(一). Spring MVC的相关配置

(二). Tomcat配置

(三). Tomcat替换

(四). Favicon配置

 

上篇文章为大家讲述了 Spring Boot的web项目开发,但是我们缺少了一部分配置;本篇文章接着上篇内容继续为大家介绍web项目开发的相关配置功能。

 

Spring Boot(四)Spring Boot的web项目开发(2)相关配置_ico

 

 

(一). Spring MVC的相关配置

 

正常情况下 Spring Boot提供的Spring MVC默认配置基本符合我们的需求了,如果出现特殊情况,我们可以通过一个配置类(注解有@Configuration的类)加上@EnableWebMvc注解来实现完全自己控制的 MVC配置。

 

如果我们既需要保留Spring Boot 提供的便利还想要增加自己的额外配置的时候,可以定义一个配置类并继承 WebMvcConfigurationAdapter 无需使用@EnableWebMvc。

代码如下:

  •  
package org.cxzc.myyoung.springboot_4;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration//@EnableWebMvc//无需使用该注解,否则会覆盖掉SpringBoot的默认配置值public class WebMVCConfig extends WebMvcConfigurerAdapter { @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/hello").setViewName("/hello");    }}

代码解释:
1. 需要指出的是 在这里重写的 addViewControllers 方法,不会覆盖WebMvcAutoConfiguration中的addViewController方法,需要注意的是 在此方法中 Spring Boot 将 “/”映射值index.html。

 

这就说明 我们自己的配置和spring Boot的自动配置同时有效。

 

(二). Tomcat配置

Spring Boot 默认内嵌的Tomcat为servlet容器。

1. 配置文件配置tomcat

Spring Boot 在org.springframework.boot.autoconfigure.web.ServerProperties 类中 定义了Tomcat的全部属性。我们可以在 application.properties文件中配置属性。Tomcat特有配置都以 server.tomcat 作为前缀

 

实例如下:
##配置servlet容器

  •  
#配置服务器端口,默认为8080server.port=8081#用户回话session过期时间,以秒为单位server.session-timeout=1000000#配置访问路径,默认为servlet.context-path=/index

##配置Tomcat

  •  
#配置Tomcat编码,默认为UTF-8server.tomcat.uri-encoding=UTF-8#Tomcat是否开启压缩,默认为关闭 on   offserver.tomcat.compression=on

 

2. 代码配置

  •  
package org.cxzc.myyoung.springboot_4;import org.springframework.boot.web.server.ErrorPage;import org.springframework.boot.web.server.ErrorPageRegistrar;import org.springframework.boot.web.server.ErrorPageRegistry;import org.springframework.http.HttpStatus;import org.springframework.stereotype.Component;
@Componentpublic class ErrorPageConfig implements ErrorPageRegistrar { @Override public void registerErrorPages(ErrorPageRegistry registry) { ErrorPage error400Page=new ErrorPage(HttpStatus.BAD_REQUEST,"/error400" ); ErrorPage error401Page=new ErrorPage(HttpStatus.UNAUTHORIZED,"/error401"); ErrorPage error404Page=new ErrorPage(HttpStatus.NOT_FOUND,"/404" ); ErrorPage error500Page=new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR,"/error500"); registry.addErrorPages(error400Page,error401Page,error404Page,error500Page); }}

 

自定义类实现

ErrorPageRegistrar接口,然后设置端口、设置错误请求页面、

设置会话超时时间等,这里的404页面放在src/main/resources/static文件夹下,有了这个之后,当我访问一个不存在的页面的时候就会跳转到404.html页面了。


 

(三). Tomcat替换

上面讲到了Tomcat的配置,开发web中不仅仅只使用Tomcat 还有 jetty和undetow 容器。

 

如果要使用jetty为servlet作为容器,需要修改spring-boot-starter-web的依赖即可。

代码如下:

  •  
<dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-tomcat</artifactId>    <scope>provided</scope></dependency>
修改成
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> <scope>provided</scope></dependency>

修改后启动项目,控制台输出会成为jetty。


(四). Favicon配置


我们打开每一个网页页签 都会看到一个 图标,这个图标就是favicon,SpringBoot提供了一个默认的,访问的时候可以看到效果,如果我们需要屏蔽掉可以通过添加配置实现

  •  
#开启和关闭Favicon  true falsespring.mvc.favicon.enabled=true

 

如果需要自定义一个 Favicon,我们只需要将自己的favicon.ico文件放置到src/main/resources目录下即可,重新运行项目,再看浏览器左上角图标就会变了

Spring Boot(四)Spring Boot的web项目开发(2)相关配置_tomcat_02



 

ok,Spring Boot的web项目开发相关配置 到这里就完成了,

 

 

 

Spring Boot(四)Spring Boot的web项目开发(2)相关配置_mvc_03