Spring Boot Notes 3 (Web Development)

One, web development support

   Spring-boot-starter-web provides dependencies for embedding tomcat and SpringMVC

 

Two, Thymeleaf template engine

 1), basic knowledge

     Thymeleaf is a class library of java, an xml/xhtml/html5 template engine, which can be used as a view layer

      Can be used to replace jsp

  2), import

     <html xmlns:th="http://www.thymeleaf.org"><!-- 1-->

         <meta content="text/html;charset=utf-8"/>

        <link th:src="@{xx/xx/bootstrap.css}" rel="stylesheet" />

       <script th:src="@{xx/xx/bootstrap.js}"></script>

    </html>

     Remarks: 1), introduce namespace

               2), Introduce static resources through "@{}", which is very error-prone under jsp

  3), access the data in the model

     Access properties in web through "${}"

    e.g. <span th:text="${person.name}"></span>

  4), data iteration in model

    e.g. <li  th:each="person:${people}">

              <span th:text="${person.age}"></span>

          </li>

  5), data judgment

    Support <,>==,!=, etc. springEL expressions

  e.g. <div th:if="${not #lists.isEmpty(people)}"></div>

  6). Access model in javascript

   eg,

       <script th:inline="javascript">

           var single= [[${singlePerson}]];

       </script>

     Remarks : 1), add it to the script tag through th:inline="javascirpt"

               2), use [[${}]] to get the value

               3), more: http://www.thymeleaf.org

  7), integration with springMVC

     thymeleaf defines 2 support classes:

            org, thymeleaf.spring4.view.ThymeleafView

            org.thymeleaf.spring4.view.ThymeleafViewResolver

    SpringTemplateEngine: used to use the Thymeleaf template engine under springMVC

   TemplateResolver: used to set the template engine (prefix, suffix)

   eg

    @Bean

   public TemplateResovler templateResolver(){

    TemplateResovler  templateResolver = new ServletContextTemplateResolver();

     templateResolver.setPrefix("/WEB-INF/templates");

     templateResolver.setSuffix(".html");

     templateResolver.setTemplateMode("HTML5");

    return templateResolver;

   }

 

    @Bean

    public SpringTemplateEngine templateEngine(){

       SpringTemplateEngine templateEngine = new templateEngine();

       templateEngine.setTemplateResolver(templateResolver());

      return templateEngine

    }

 

   @Bean

   public ThymeleafViewResolver thymeleafViewResolver () {

      ThymeleafViewResolver thymeleafViewResolver = new ThymeleafViewResolver ();

       ThymeleafViewResolver.setTemplateEngine (templateEngine ());

        return thymeleafViewResolver;

    }

  8). Spring Boot support

  Spring Boot autowires Thymeleaf through the org.springframework.boot.autoconfigure.thymeleaf package.

   To configure thymeleaf through ThymeleafProperties, just start with spring.thymeleaf in application.properties to configure

  ThymeleafProperties source code, post-fill

    Example in application:

       #thymeleaf start

       spring.thymeleaf.mode=HTML5

       spring.thymeleaf.encoding=UTF-8

       spring.thymeleaf.content-type=text/html

       #Close the cache during development, otherwise you won't be able to see the real-time page

       spring.thymeleaf.cache=false

       #thymeleaf end

 

Three, Web related configuration

  1, Automatic configuration provided by spring boot

     1), Automatically configure ViewResolver

       ContentNegotiatingViewResolver: A special ViewResolver with the highest priority

                                                              Not handle it yourself, but handle it to other viewResolvers

       BeanNameViewResolver: to find the view that the bean returns a string to render the view

       InternalResourceViewResolver: mainly returns the view name by setting the prefix, suffix, and method in the controller

    2), Automatically configure static resources

    The automatic configuration of the following static resources is defined in the addResourceHandlers method of the automatic configuration class

      Classpath files: Directly map the static files in /static, /public, /resources, /META-INF/resources to /**.

                           It can be accessed through localhost:8080/**

      webjar: Map the static files of /META-INF/resources/webjars/ of webjar to /webjars/**

   3), automatically configure Formatter and Converter 

   4), automatically configure HttpMessageConverters

   5), support for static homepage

      The static home page index.html is placed in the directory. META-INF/resources/, resources/, public/, statuc/ can be.

  6), take over the web configuration of spring boot

   If the default configuration does not meet the requirements, you can implement your own configuration through @Configuration, @EnableWebMvc

   7), register Servlet, Filter, Listener

    Declare it as a spring bean to achieve the effect. That is: @Bean

Four, Tomcat configuration

   All properties about Tomcat are defined in org.springframework.boot.autoconfigure.web.ServerProperties.

  1, Configure Tomcat

  applicaiton.properties add the following configuration

    server.port=8080 #The default is 8080

    server.session-timeout=600 #Expiration time, in seconds

    server.context-path= #Configure the access path, the default is /

    server.tomcat.uri-encoding= #Configure the encoding of tomcat, the default is UTF-8

    server.tomcat.compression= #Whether tomcat enables compression, the default is off off

  .....

   2, code configuration tomcat

    Register a Bean that implements the EmbeddedServletContainerCustomizer interface

   3, replace tomcat

     In pom.xml, change the dependency of spring-boot-starter-web from spring-boot-starter-web to the corresponding

    e.g. spring-boot-starter-jetty  (undertow)

 

  3, SSL(Secure Sockets Layer) 配置

     1), Generate a certificate: It can be generated with the keytool that comes with JDK.

      eg keytool -genkey -alias tomcat and follow the prompts

     2), spring boot configure SSL

          server.port=8443

          server.ssl.key-store=.keystore

          server.ssl.key-store-password=123

          server.ssl.keyStoreType=JKS

     3), http转https

       Configure TomcatEmbeddedServletContainerFactory and add Tomcat's connector to implement

Five, Favicon configuration

 关闭:  spring.mvc.favicon.enabled=false

Set up your own favicon: put your own favicon.ico (the file name cannot be moved) and put it in the root directory of the classpath (/, resources/ public/, static)

                                 You can wait in some directories

 

Six, WebSocket

1, Preparation: Add websocket dependencies

2, broadcast

  //everything

3, point to point

  /everything

Seven, applications based on Bootstrap and AngularJs 

 Bootstrap download address: http://getbootstrap.com/getting-started/

Page component support: http://getbootstrap.com/components

Lots of js plugins: http://getbootstrap.com/javascript/

angularJs: https://angularjs.org/

 For an example, see the network disk: projects/springboot

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326154014&siteId=291194637