有两种方式进行界面的跳转
一、使用springboot中的thymeleaf方式
1、在pom文件中引入模板引擎jar包
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
2、在templates下建立login.html文件,也可以叫其他名字,主要是与控制器进行匹配
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8" /> <title>Insert title here</title> <script src="http://res.sumahe.cn/js/jquery.min.js"></script> </head> <body> <h1>测试index</h1> </body> </html>
3、书写控制器
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; /** * Created on 2018/7/24. */ @Controller public class TestController { @RequestMapping("/index") public String test() { return "login"; } }
4、启动springboot项目,输入localhost:端口号/index,即可访问到界面,显示 “测试index” 的内容
二、不使用thymeleaf,直接进行界面的跳转
1、注释掉引入的jar包,注意,一定要确保pom文件中没有如下jar包,不然会走默认配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2、在配置文件application.yml或者application.properties进行配置springmvc。
yml方式:
spring:
mvc:
view:
suffix: .html
prefix: /
properties方式:
spring.mvc.view.suffix= .html
spring.mvc.view.prefix= /
3、书写controller
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; /** * Created on 2018/7/24. */ @Controller public class TestController { @RequestMapping("/index") public String test() { //默认跳转到resources底下的static中的界面,静态资源文件,如果直接写login,只需将页面放在static底下即可 //注意:return回的界面名称不能与requestMapping中的跳转路径一直,不然会报错 return "web/resOrg"; } }
4、启动项目,在浏览器中访问即可访问得到resOrg的界面