spring boot项目只有src目录,没有webapp目录,会将静态访问(html/图片等)映射到其自动配置的静态目录,如下
/static
/public
/resources
/META-INF/resources
所以,一般我们会配置spring.resourcess.static-locations的值:
web.upload-path=E:/
spring.mvc.static-path-pattern=/**
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:${web.upload-path}
动态页面需要先请求服务器,访问后台应用程序,然后再转向到页面,比如访问JSP。spring boot建议不要使用JSP,默认使用Thymeleaf来做动态页面。
在pom.xml 中添加Thymeleaf组件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
TemplatesController.java
package hello;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;
@Controller
public class TemplatesController {
@GetMapping("/templates")
String test(HttpServletRequest request) {
//逻辑处理
request.setAttribute("key", "hello world");
return "/index";
}
}
@RestController:上一篇中用于将返回值转换成json
@Controller:现在要返回的是一个页面,所以不能再用@RestController,而用普通的@Controller/
request.setAttribute("key", "hello world"):这是最基本的语法,向页面转参数 key和value。
return "/index": 跳转到 templates/index.html动态页面,templates目录为spring boot默认配置的动态页面路径。
其中:templates目录为spring boot默认配置的动态页面路径。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有