web project development Spring Boot (five) Spring Boot (2) Related

Public concern number [procedures in the workplace], focusing on Spring Boot + micro-services, applets, flutter, Android, regular articles and video tutorials to share, as well as career planning, operations management, attention to post-return Java data, receive learning for your well-prepared dry!

Hello everyone, I am a "snail Dream", you can reply "Java Data" in the background No public access to information skills upgrading, it is absolutely dry.

This article is the fourth to understand the previous article contribute to a better understanding of this article Spring Boot series:


1.Spring Boot (a) acquaintance Spring Boot frame
2.Spring Boot (two) Spring Boot basic configuration
3.Spring Boot (three) Spring Boot automatically configure the principles
4.Spring Boot (four) Spring Boot web project development


Foreword

(A). Spring MVC related configuration

(B). Tomcat configuration

(Three). Tomcat replaced

(Four). Favicon Configuration

The article to you about the web project development Spring Boot, but we are missing a part of the configuration; then this article on article content continue to introduce relevant configuration functions web project development.

 

 

(A). Spring MVC related configuration

 

Spring MVC Spring Boot normally provided by default configured basically in line with our needs, and if there are special circumstances, we can (annotation has @Configuration class) through a configuration class plus @EnableWebMvc comment to implement MVC configuration complete control of their own .

If we need to keep both facilities offered by Spring Boot also want to add extra time to your configuration, you can define a configuration class and inherit WebMvcConfigurationAdapter without the use of @EnableWebMvc.

code show as below:

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");    }}

Code explanation:
1. It should be noted here that a method addViewControllers rewritable addViewController not cover the method WebMvcAutoConfiguration, Spring should be noted that in this method the Boot "/" mapping value index.html.

This shows that our own automatic configuration and spring Boot configuration at the same time effective.

(B). Tomcat configuration

Spring Boot default embedded Tomcat is the servlet container.

1. Profile Configuration tomcat

Spring Boot Tomcat defines all the attributes of the class in org.springframework.boot.autoconfigure.web.ServerProperties. We can configure the properties in application.properties file. Tomcat-specific configurations are prefixed with server.tomcat

Examples are as follows:
## arranged servlet container

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

## Tomcat configuration

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

2. The code configuration
 

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);    }}

Custom class that implements

ErrorPageRegistrar interface, and then set the port, setting a bad request page,

Set session timeout time, here's 404 pages into src / main / resources / static folder, and then with this, when I visit a page that does not exist when it will jump to the page 404.html.

(C). Alternatively Tomcat

above mentioned configuration of the Tomcat, not only the development of the Tomcat web also undetow jetty and the vessel.

If the servlet is to be used as a container jetty, you need to modify the spring-boot-starter-web can be dependent.

code show as below:

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

After modifying the startup project, console output will become the jetty.

(Four). Favicon Configuration


We open each tab page will see an icon that is the favicon, SpringBoot provides a default, the time of the visit can see the effect, if we need to block out the configuration can be achieved by adding
 

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

 

If you need to customize a Favicon, we just want to put your own favicon.ico file can be re-run under src / main / resources directory project, look at the top left corner of the browser icon will be changed

 

ok, Spring Boot project related to the development of web configuration is complete here, and if small partner still in doubt, you can add the public group number, we progress together

This case Download:

https://github.com/ProceduralZC/itcxzc/tree/master/springboot_5

Public concern number [procedures in the workplace], focusing on Spring Boot + micro-services, applets, flutter, Android, regular articles and video tutorials to share, as well as career planning, operations management, attention to post-return Java data, receive learning for your well-prepared dry!

 

 

Published 55 original articles · won praise 101 · views 340 000 +

Guess you like

Origin blog.csdn.net/jianpengxuexikaifa/article/details/102946672