前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >spring boot 默认静态文件位置及动态文件位置

spring boot 默认静态文件位置及动态文件位置

作者头像
凯哥Java
发布2019-06-28 15:04:16
2.2K0
发布2019-06-28 15:04:16
举报
文章被收录于专栏:凯哥Java凯哥Java

spring boot项目只有src目录,没有webapp目录,会将静态访问(html/图片等)映射到其自动配置的静态目录,如下

/static

/public

/resources

/META-INF/resources

所以,一般我们会配置spring.resourcess.static-locations的值:

代码语言:javascript
复制
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组件

代码语言:javascript
复制
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>

TemplatesController.java

代码语言:javascript
复制
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默认配置的动态页面路径。

ffc06d52b7434ec60736308978a2cc40.png
ffc06d52b7434ec60736308978a2cc40.png

其中:templates目录为spring boot默认配置的动态页面路径。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-05-12 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 动态页面
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档