Spring Boot Web preparation

Posted by drranch on Thu, 27 Jan 2022 07:16:29 +0100

Spring Boot Web preparation

  • Import static resources
  • Custom home page
  • jsp
  • Assembly extension spring MVC
  • Add, delete, modify and check
  • Interceptor
  • Internationalization (switching between Chinese and English)

Import static resources





Static resources under the five paths in the figure can be read

/**,classpath:/WEB-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/

The resources under classpath:/resources / have the highest read priority, followed by classpath:/static /, and finally classpath:/public/

Custom home page

private Resource getWelcomePage() {
   for (String location : this.resourceProperties.getStaticLocations()) {
      Resource indexHtml = getIndexHtml(location);
      if (indexHtml != null) {
         return indexHtml;
      }
   }
   ServletContext servletContext = getServletContext();
   if (servletContext != null) {
      return getIndexHtml(new ServletContextResource(servletContext, SERVLET_LOCATION));
   }
   return null;
}

private Resource getIndexHtml(String location) {
   return getIndexHtml(this.resourceLoader.getResource(location));
}

private Resource getIndexHtml(Resource location) {
   try {
      Resource resource = location.createRelative("index.html");
      if (resource.exists() && (resource.getURL() != null)) {
         return resource;
      }
   }
   catch (Exception ex) {
   }
   return null;
}

Return the index. In the static resource directory HTML file as home page

Thymeleaf template engine

  • Spring Boot uses embedded Tomcat and does not support JSP by default
  • Pure static pages will bring great trouble to development. Spring Boot recommends using template engine
  • JSP is a template engine. Spring Boot recommends using Thymeleaf template engine

preparation

  • Turn on the Thymeleaf starter
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
  • All Thymeleaf template engines are written under templates

  • Default encoding and pre suffix

  • Add Thymeleaf constraint to html file
<html xmlns:th="http://www.thymeleaf.org">

Value transmission

  • All html elements can be taken over by Thymeleaf through th: element name replacement
@Controller
public class IndexController {
    @RequestMapping("/test")
    public String index(Model model){
        model.addAttribute("msg","Test successful");
        return "test";
    }
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div th:text="${msg}"></div>
</body>
</html>

Thymeleaf grammar

th:text and th:utext

  • th:text: get the value of the parameter
  • th:utext: when obtaining the value of the parameter, if there is an html tag in the value, the html will be parsed and the corresponding effect will be displayed

th:each traversal

@Controller
public class IndexController {
    @RequestMapping("/test")
    public String index(Model model){
        model.addAttribute("lists", Arrays.asList("147","258"));
        return "test";
    }
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div th:each="list:${lists}" th:text="${list}"></div>
<!--perhaps<div th:each="list:${lists}">[[ ${list} ]]</div>-->
</body>
</html>
data formatexpression
Variable Expressions${...}
Message Expressions#{...}
URL(Link URL Expressions)@{...}
Fragment Expressions~{...}
Text (Text literals)'hello'
Number literals2
Boolean literalstrue,false
Null literalsnull
Flags (literal tokens)one,sometext,main...
Text operation - String concatenation+
Mathematical operation+,-,*,/,%
Boolean expressionand,or,!,not
Comparison operation>,<,>=,<=,==,!=
Ternary conditional operator(if)?(then),(if)?(then):(else)

Extended assembly spring MVC

  • Extend spring MVC:

    Create a new class, add @ Configuration annotation to make this class become a Configuration class, and implement WebMvcConfigurer interface

  • There are many XXXX configurations in springboot to help us with extended configuration

@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
}

Extend / customize view parser

  • Customize a class to implement the ViewResolver interface to become a view parser
public static class MyViewResolver implements ViewResolver{
    @Override
    public View resolveViewName(String viewName, Locale locale) throws Exception {
        return null;
    }
}
  • Method plus @ Bean to generate a view parser object and hand it to Spring's IOC container for management to help us assemble it automatically
@Bean
public ViewResolver myViewResolver(){
    return new MyViewResolver();
}

Topics: Java Front-end Spring Boot