Project started spring boot (a) of how to return to the page in the controller class

Inscription : After learning springboot and thymeleaf, would like to complete a project to practice your hand, then use springboot + mybatis and thymeleaf complete a blog system, some problems arise during the completion of these issues are recorded as their own learning experience . In this Thanks in the main group of Tumo TyCoding project, although they too dishes, many places do not understand, but still served me well.

 


In the controller class return to the page There are two ways, ways to use thymeleaf template engine and does not use templates way (ie controller return value ModelAndView or String). String ModelAndView or return value in the controller class, the difference between the two is that some of the attributes can be stored as ModelAndView the same session.

 

1. Do not use a template

1.1 as the return value ModelAndView

@Controller
public class LoginController {

    @RequestMapping(value = "/login")
    public ModelAndView login(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/login.html"); return mv; } }

Resource path is as follows:

 

 

 After starting the project results are as follows:

 

String used as the return value 1.2

@Controller
public class HelloController {

@RequestMapping("/hello")
public String hello() {
return "/hello.html";
}
}

Resource path is as follows:

 

 

 After starting the project results are as follows:

 

 

 

 Both methods can be found by controller class it is returned in the static and login.html hello.html.

Conclusion: springboot static resource is placed under static default route, in other words is the root path is static html pages, etc.

 2. Use template engine thymeleaf

2.1 introduces dependence in pom file

 

 2.2 Adding configuration information application.yml file

# thymeleaf
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.check-template-location=true
spring.thymeleaf.suffix=.html
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.cache=false

2.3 & html write controller class codes

Because here's controller classes and html page path As before, I will not paste the code. Since only demo shows the page, so I did not add thymeleaf URL in html tag

<html lang="en" xmlns:th="http://www.thymeleaf.org">

2.4 Startup Items

 

 

 

 

Both methods can be found by controller class it is returned in the templates and login.html hello.html.

Conclusion: springboot after the integration of static resource templates default templates are placed in the path, that is the root path such as html pages are templates.

Guess you like

Origin www.cnblogs.com/Code-Handling/p/11921354.html