How springboot returns html and jsp

醉折花枝作酒筹
Release: 2021-04-30 09:21:47
forward
3070 people have browsed it

This article will introduce to you how springboot returns html and jsp. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

How springboot returns html and jsp

1. Return to html

(1) Add maven dependency

 org.springframework.boot spring-boot-starter-thymeleaf 
Copy after login

(2) The thymeleaf template looks for the templates file under resources by default Put the html page in the folder, and put the css and js in the static folder

(3) To introduce js, you need to use the following format

      

Hello World!

Copy after login

(4) The controller code is as follows

package springboot.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class HtmlController { @RequestMapping("/show") public String show() { return "aaa"; } }
Copy after login

2. Return jsp

(1) Add the maven dependency of jsp

 org.apache.tomcat.embed tomcat-embed-jasper provided   javax.servlet jstl 
Copy after login

Note:Return jsp needs to comment out spring-boot-starter-thymeleaf

(2) Add a view parser to find the jsp page in the controller

@Bean public InternalResourceViewResolver viewResolver() { InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); viewResolver.setPrefix("/WEB-INF/"); viewResolver.setSuffix(".jsp"); return viewResolver; }
Copy after login

(3) The structure diagram is as follows

(4) The controller code is as follows

package springboot.controller; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.view.InternalResourceViewResolver; @Controller public class JspController { @RequestMapping("/test") public String index() { return "home"; } @Bean public InternalResourceViewResolver viewResolver() { InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); viewResolver.setPrefix("/WEB-INF/"); viewResolver.setSuffix(".jsp"); return viewResolver; } }
Copy after login

Note:Use the @Controller annotation when returning html and jsp

3. Load css , js file

Put it under static

Recommended learning:html video tutorial

The above is the detailed content of How springboot returns html and jsp. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!