SPIRNG Boot Web Configuration development

Source: Internet
Author: User

Spring-booter-starter-web is the core of the Spring-boot Web, and the automatic configuration information is stored in the Web directory below Spring-boot-autoconfigure.jar, which contains various web autoconfiguration, such as

Serverpropertiesautoconfiguration serverproperties automatic Configuration servlet container

Httpencodingautoconfiguration and Httpencodingproperties to automatically configure HTTP encoding

Multipartautoconfiguration Multipartproperties Configuring the properties of the upload file

Webmvnautoconfiguration webmvcproperties Configuring Spring MVC

1 Thymeleaf Template engine

Spring-boot can use JSP, but inline Tomcat and jetty do not support running as a jar Jsp,undertow does not support JSP, while Spring-boot provides many template engines, including commonly used Freemaker, The most recommended practical is the Thymeleaf, which provides the perfect SPRINGMVC support. Thymeleaf is a Java class library, a XML/XHTML/HTML5 template engine that can be viewed as the MVC Web application's view layer, which can replace JSP completely.

2 Integrated Spring MVC

In Springmvc, when requests are made to SPRINGMVC-controlled resources, these requests are processed by SPRINGMVC Dispatherservlet, parsing the appropriate mappings in handermapping, and then obtaining Handleradapter Processing the handler, the processing is done to return a Modelandview object, and finally renders the object as a view returned to the user. The process of rendering is done by viewresolver and view, Modelandview does not contain a real attempt to return only a logical attempt name, Viewresolver will parse out the real view object according to the logical view name. The last view object is rendered to the browser.  Viewresolver are commonly used in projects to configure path prefixes and suffixes. such as Freemaker's Viewresolver

<bean class= "Org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver" >
<property name= "ContentType" value= "text/html; Charset=utf-8 "/>
<property name= "Cache" value= "true"/>
<property name= "prefix" value= "web-inf/pages/"/>
<property name= "suffix" value= ". FTL"/>
</bean>

In Spring-boot, Thymeleaf defines Thymeleafview (the default view) and Thymeleafviewresolver as well as the general Springtemplateengine ( The templateresolver is placed in the engine, and Templateresolver is used to set up a generic template engine (such as setting the path prefix without distinguishing between the templates).

The thymeleaf is automatically configured in the auto-inject autoconfigure.thymeleaf without manual configuration, including the above Templateresolver Thymeleafviewresolver Springtemplateengine, in the corresponding properties file, you can see the various default property configurations

The code can be set according to the parameters as follows, and the action uses the same model as spring MVC.

spring.thymeleaf.prefix=classpath:/pages/
Spring.thymeleaf.suffix=.html

(1) Springboot will automatically configure the SPRINGMVC resolver,webmvcautoconfiguration inside the automatic configuration.

1) Contentnegotiatinviewresolver A special resolver, do not deal with the view itself, but to other resolver processing, with the highest priority, the principle is to get and traverse all resolver, According to ContentType select the required resolver,resolver can handle what Yang ContentType, can be set in spring.thymeleaf.content-type=text/html, the default is this type of page. If more than one resolver can handle the same contenttype, pick the one that has the highest priority.

2) Beannameviewresolver Select the resolver to use by name. The action returns a string that corresponds to the method name that generated the resolver bean, such as. In action, return "Jsonview"

3) internalresourceviewresolver Spring MVC is the most commonly used resolver, which is usually for setting the prefix and so on. But there was a pit in the spring-boot. Because this resolver jump (forward dispatcher) is based on Webinf, and the Spring-boot root is classpath, this resolver jump does not go to the configured Classpath page. If you really want to use, you need to add WEB-INF,MVN set to fight the war package, the path is written under Webinf, using an external Tomcat container to start the line. It is highly recommended to use Thymeleaf's own resolver. This is also why Spring-boot does not recommend using JSP, it must be made into a war package, and other templates are jar war.

(2) If spring MVC comes with a configuration that does not meet the requirements, you can fully control your MVC configuration with a configuration class (@Configuration) plus @enablewebmvc annotations. If you only want to extend the additional configuration of SPRINGMVC, you can define the configuration class inheritance Webmvcconfigureradapter, without using @enablewebmvc, such as overriding Addviewcontrollers, For non-controller direct jump page, springboot configuration '/' map to ' index.html ', the jump of their own configuration is also valid, recommend this way of writing.

(3) Registration Servlet,filter,listener

Spring Boot provides two ways to configure these, one is to add @WebServlet @WebListener @WebFilter before the class (both configure the path), and then configure a scan, @Import ( Servletcomponentscanregistrar.class), you can configure the package to be scanned, and finally add @servletcomponentscan at the entrance to scan the contents of the configuration.

Another recommended configuration is the @component implementation of filter, and then in a configuration class @Bean register a Filterregistrationbean, the Bean configuration name, URL path and so on. It is important to note that the order of execution of the filter and the configuration class @bean from top to bottom, similar to the order in XML from top to bottom.

(4) Configuring Tomcat

The servlet information can be configured in code, and if you want to configure a common servlet, you can register a bean that implements the Embeddedservletcontannercustomizer interface, and if you want to configure Tomcat individually, jetty, Undertow, a bean will be returned to Tomcatembeddedservletcontannerfactory,jettyembeddedservletcontannerfactory, Undetowembeddedservletcontannerfactory, the inside can configure the port, session time and so on. However, it is more convenient to configure the Server.port directly in the external file, and note that if configured at the same time, the configuration priority in the file is higher than the code priority level. If you like to replace Tomcat, then you can exclude Spring-boot-starter-tomcat dependencies in Spring-boot-starter-web, plus spring-boot-starter-jetty dependencies.

SPIRNG Boot Web Configuration development

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.