1参考链接
SpringBoot如何返回页面 - 编程小大白 - 博客园
在springboot整合thymeleaf模板引擎中@Controller和@RestController不同注解的跳转页面方法 - 海龟先生吖 - 博客园
controller层和spring的启动类最好放在同一个目录下
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
@ResponseBody注解
1、@ResponseBody 注解可被应用于方法上,标志该方法的返回值应该被直接写回到 HTTP 响应体中去(而不会被被放置到 Model中 或 被解释为一个视图名)
2、@ResponseBody主要需要spring-webmvc、jackson-mapper-asl两个包,其余依赖包Maven会帮你完成。<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.1.2.RELEASE</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.8</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
case1(无法访问templates)
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<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.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<!--<version>3.1.2.RELEASE</version>-->
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<!--<version>1.9.8</version>-->
<type>jar</type>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
TestController.java
package com.example.demo.Contr;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author:
* @date:2022/2/11
* @description:
*/
@Controller
public class TestController {
//可以访问
@RequestMapping("/page1")
public String page1() {
return "redirect:index1.html";
}
//不可以访问,没有访问templates目录的权限
@RequestMapping("/page2")
public String page2() {
return "redirect:index2.html";
}
//可以访问
@RequestMapping("/page3")
public String page3() {
return "index1.html";
}
//不可以访问,没有访问templates目录的权限
@RequestMapping("/page4")
public String page4() {
return "index2.html";
}
}
这说明如果想要访问index2.html,需要将index2.html放到resources/static 目录下才可以访问
如果已经将html文件放到了static目录下了,在control层return "index"; 后还是
请务必加上:
# control层返回的视图没有跳转可能是没有设置 spring.mvc.view.suffix=.html spring.mvc.view.suffix=.html
当然还是有终极解决方案来解决这个存放路径问题的,那就是使用springmvc的配置:
spring:
mvc:
view:
suffix: .html
static-path-pattern: /**
resources:
static-locations: classpath:/templates/,classpath:/static/
或
server.port=8080
spring.mvc.view.suffix=.html
spring.mvc.static-path-pattern=/**
#spring.resources.static-locations=classpath:/templates/,classpath:/static/
spring.web.resources.static-locations=classpath:/templates/,classpath:/static/
然后就可以访问templates目录下的html文件了
case2(无法跳转到指定的页面)
注意:@RequestMapping中的路径一定不要和返回的页面名称完全相同,这样会报500的错误!!!!
1 使用@Controller注解
不进行跳转是因为。。。。。这对于我来说是一个很深入的问题,日后补充。。。。
TestController1.java
package com.example.demo.Contr;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* @author:
* @date:2022/2/11
* @description: 使用@Controller注解实现页面跳转
*
* 注意:类名上方和方法的上方不能出现@ResponseBody否则页面无法进行跳转,只会返回一个字符串
*/
@Controller
public class TestController1 {
@RequestMapping("/TestController1-page1")
public String page1() {
return "redirect:index1.html";
}
@RequestMapping("/TestController1-page2")
public String page2() {
return "index1";
}
/**
*@param: [model]
*@return: java.lang.String
*@description: 使用@Controller注解跳转时,如果需要携带数据,可以 public String page3(Model model) 利用model携带数据
*/
@RequestMapping("/TestController1-page3")
public String page3(Model model) {
model.addAttribute("msg", "hello world!");
return "index1";
}
}
2.使用@RestController注解
TestController2.java
package com.example.demo.Contr;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import java.util.HashMap;
import java.util.Map;
/**
* @author:
* @date:2022/2/11
* @description: 使用@RestController注解实现页面跳转
*/
@RestController
public class TestController2 {
@GetMapping("/TestController2-page1")
public ModelAndView page1(){
ModelAndView modelAndView = new ModelAndView();
Map<String, String> map = new HashMap<>();
map.put("1", "张三");
map.put("2", "李四");
map.put("3", "王麻子");
modelAndView.addObject("list", map);
//modelAndView.setView();
modelAndView.setViewName("index1");
return modelAndView;
}
@GetMapping("/TestController2-page2")
public ModelAndView page2(){
return new ModelAndView("index1");
}
}
2.Thymeleaf模板引擎处理页面的交互
添加Thymeleaf的pom依赖
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>
常规的application.properties中Thymeleaf的配置
与Thymeleaf有关的页面必须放在templates文件夹下
spring.thymeleaf.mode=HTML
spring.thymeleaf.cache=false
spring.thymeleaf.encoding=utf-8
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
但是有些情况 classpath:/templates/ ----> /static/
当然也可以修改,更改配置文件:
ymal格式的配置文件中这样改写常规配置
spring:
thymeleaf:
prefix: classpath:/static/
suffix: .html
cache: false #关闭缓存
或
application.properties格式的配置文件中这样改写常规配置
spring.thymeleaf.prefix=classpath:/static/
spring.thymeleaf.suffix=.html
spring.thymeleaf.cache=false