introduction
In traditional web development, jsp pages are usually used. First, you need to introduce springmvc-related packages into the pom file, and then write springmvc configuration files (including path resolution of access resources), and then you need to configure access routes in web.xml. This is undoubtedly too troublesome, and you need to write a large number of configuration files before each development.
Springboot provides an efficient and convenient solution for this. You only need to add web development dependencies to pom.xml to perform web development, saving you cumbersome configuration steps.
The following is the dependencies introduced by web development
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId></dependency>
text
So what should I do if I need to use the page in springboot? JSSP is not recommended for springboot, because JSSP has many restrictions in springboot, so I won’t discuss the specific restrictions here. If you are interested, you can check it online. It is recommended to use the thymeleaf template in springboot, and use html as the page display. So how to access the html page through the Controller?
1. Add thymeleaf dependency in pom.xml file
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> </dependencies>
2. Add access request configuration in application.yml
##thymeleaf page template configuration spring: mvc: view: prefix: / suffix: .html
The static folder in springboot default resources stores static resources, such as js files, css files, pictures, etc. The html page is stored in the templates folder.
3. Create hello.html in the templates folder
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"/> <title>Title</title></head><body>hello world</body></html>
4. Write Controller
/** * Created by Tomthy on 2018/5/10 */@Controllerpublic class ContentController { @GetMapping("/hello") private String helloWorld(){ return "hello"; }}Note: Do not use the @RestController annotation. The @RestController annotation is a collection of @ResponseBody and @Controller. Using the @RestController annotation will return data by default and will not request it to the page.
5. Enter the request address in the browser
Enter the address: http://localhost:8080/hello and you can request it to the hello.html page.
6. Access to static resources
When using static resources in the html page (such as pictures), use <script type="text/javascript" src="/js/wangEditor.js"></script> directly. js is the folder under static.
7. Project Directory
Summarize
The above is what the editor introduced to you. Springboot uses the thymeleaf template to access the html page. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!