Static pages:

In resources to create a static list and index. The HTM static files, access the address http://localhost:8080/index.html

Spring Boot projects have only SRC directory, no WebApp directory, and will map static access (HTML/images, etc.) to its automatically configured static directory, as shown below

/static

/public

/resources

/META-INF/resources

If you want to jump from the background to static index.html

@Controller
public class HtmlController {
	@GetMapping("/html")
	public String html() {
		return "/index.html";
	}
Copy the code

Dynamic page:

Use Thymeleaf for dynamic pages and add the Thymeleaf component to POM.xml

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
Copy the code

The templates directory is the dynamic page path configured by default for Spring Boot

package hello; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.*; import org.springframework.web.bind.annotation.*; @Controller public class TemplatesController { @GetMapping("/templates") String test(HttpServletRequest request) { // Handle request.setAttribute("key", "hello world"); return "/index"; }}Copy the code

Index.html page:

<! DOCTYPE html> <html> <span th:text="${key}"></span> </html>Copy the code

Access the address: http://localhost:8080/templates

The question

Localhost :8080 = ‘templates’; static =’ index.html ‘;

Answer: The normal way is to use nginx or apach proxy server to do the jump

The second is: The requirement is that there is a hyperlink in the dynamic page index.html in the Templates directory to another dynamic page in templates, the cat.html page, CAR =” a href=”car. HTML “>car =” a href=”car.

Dynamic page directories cannot be jumped statically. Dynamic page directories can only be jumped through the control layer, but there are many hyperlinks on the page to jump to the dynamic page. It is not good to write many hyperlinks to the control layer, so you can use XML configuration:

The tag is view-controller property: path property: view-nameCopy the code

Third is: visit http://localhost:8080/templates page after page after the introduction of the static list of CSS, js static resources and so on, but the page can’t visit the inside of the static static resources

Answer: If accessing a JS, CSS statement resource, start with an absolute path, / slash. The control layer mapping path also begins with/slash