您好,欢迎访问一九零五行业门户网

springboot如何返回html和jsp

本篇文章给大家介绍一下springboot返回html和jsp的方法。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。
一、返回html
(1)添加maven依赖
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-thymeleaf</artifactid></dependency>
(2)thymeleaf模板默认寻找resources下,templates文件夹放html页面,static文件夹放css及js
(3)引入js,需要使用如下格式
<html lang="en" xmlns:th="http://www.thymeleaf.org"><script type="text/javascript" th:src="@{/js/jquery/jquery.min.js}"></script><script type="text/javascript" th:src="@{/js/jquery/jquery.easyui.min.1-7-5.js}"></script><script type="text/javascript" th:src="@{/js/jquery/easyui-lang-zh_cn.js}"></script><script type="text/javascript" th:src="@{/js/index.js}"></script><body><h2>hello world!</h2></body></html>
(4)controller代码如下
package springboot.controller;import org.springframework.stereotype.controller;import org.springframework.web.bind.annotation.requestmapping;@controllerpublic class htmlcontroller { @requestmapping("/show") public string show() { return "aaa"; }}
二、返回jsp
(1)添加jsp的maven依赖
<dependency> <groupid>org.apache.tomcat.embed</groupid> <artifactid>tomcat-embed-jasper</artifactid> <scope>provided</scope></dependency><dependency> <groupid>javax.servlet</groupid> <artifactid>jstl</artifactid></dependency>
注:返回jsp需要把spring-boot-starter-thymeleaf注释掉
(2)在controller里添加寻找jsp页面的视图解析器
@beanpublic internalresourceviewresolver viewresolver() { internalresourceviewresolver viewresolver = new internalresourceviewresolver(); viewresolver.setprefix("/web-inf/"); viewresolver.setsuffix(".jsp"); return viewresolver;}
(3)结构图如下
(4)controller代码如下
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;@controllerpublic 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; }}
注:返回html和jsp时使用@controller注解
三、加载css、js文件
放到static下
推荐学习:html视频教程
以上就是springboot如何返回html和jsp的详细内容。
其它类似信息

推荐信息