1、
說(shuō)道跳轉(zhuǎn)镀裤,先說(shuō)一下springBoot web的結(jié)構(gòu)
image.png
其中static 中放的是可一直接訪問(wèn)的數(shù)據(jù) ,比如[http://localhost:8080/helloWorld.html]
可以直接訪問(wèn)html
templates 中放置的是跳轉(zhuǎn)的數(shù)據(jù)
2镜悉、jar包依賴
Springboot有自己默認(rèn)的模板引擎辉词。一定要引用相應(yīng)的依賴包喘沿。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
這里注意:靜態(tài)頁(yè)面的return默認(rèn)是跳轉(zhuǎn)到/static/index.html闸度,當(dāng)在pom.xml中引入了thymeleaf組件,動(dòng)態(tài)跳轉(zhuǎn)會(huì)覆蓋默認(rèn)的靜態(tài)跳轉(zhuǎn)蚜印,默認(rèn)就會(huì)跳轉(zhuǎn)到/templates/index.html莺禁,注意看兩者return代碼也有區(qū)別,動(dòng)態(tài)有或沒(méi)有html后綴都可以窄赋。
3睁宰、注解
注解使用@Controller 而不是@RestController
4、ModelAndView
跳轉(zhuǎn)可以包含視圖和數(shù)據(jù)
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("test1");
modelAndView.addObject("key", 12345);
//System.out.println("test");
return modelAndView;
上例可以跳轉(zhuǎn)到test1視圖中寝凌,并帶上key值