一、静态资源访问
静态资源:js、css、html、音视频等
静态资源路径:指系统可直接访问的路径,且路径下的所有文件均可被用户直接读取。
SpringBoot默认提供静态资源目录位置需置于src/main/resources下,目录名需符合如下规则:
- /static
- /public
- /resources
- /META-INF/resources
在static目录中加入a.jpg,不需重启服务就可访问:http://localhost:8080/a.jpg
二、模板引擎
1. 添加maven依赖
<dependency>
<groupId>com.ibeetl</groupId>
<artifactId>beetl</artifactId>
<version>2.8.5</version>
</dependency>
2.添加beetl配置
在项目运行入口文件xxxapplication.java中加入下面代码:
@Bean(initMethod = "init", name = "beetlConfig")
public BeetlGroupUtilConfiguration getBeetlGroupUtilConfiguration() {
BeetlGroupUtilConfiguration beetlGroupUtilConfiguration = new BeetlGroupUtilConfiguration();
ResourcePatternResolver patternResolver = ResourcePatternUtils.getResourcePatternResolver(new DefaultResourceLoader());
try {
// WebAppResourceLoader 配置root路径是关键
WebAppResourceLoader webAppResourceLoader =
new WebAppResourceLoader(patternResolver.getResource("classpath:/").getFile().getPath());//设置beetl根路径
beetlGroupUtilConfiguration.setResourceLoader(webAppResourceLoader);
} catch (IOException e) {
e.printStackTrace();
}
//读取配置文件信息
return beetlGroupUtilConfiguration;
}
@Bean(name = "beetlViewResolver")
public BeetlSpringViewResolver getBeetlSpringViewResolver(@Qualifier("beetlConfig")
BeetlGroupUtilConfiguration beetlGroupUtilConfiguration) {
BeetlSpringViewResolver beetlSpringViewResolver = new BeetlSpringViewResolver();
beetlSpringViewResolver.setPrefix("templates/");//设置beetl文件的路径为:resources/templates
beetlSpringViewResolver.setSuffix(".btl");//设置beetl的后缀设置为btl
beetlSpringViewResolver.setContentType("text/html;charset=UTF-8");
beetlSpringViewResolver.setOrder(0);
beetlSpringViewResolver.setConfig(beetlGroupUtilConfiguration);
return beetlSpringViewResolver;
}
3.创建controller和返回页面
@RequestMapping("/hellobtl")
public ModelAndView helloBeetl(){
System.out.println("xxx");
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("username",myProperties.getName());
modelAndView.setViewName("hello");
return modelAndView;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>add</title>
</head>
<body>
add Html web info
<hr>
获取项目的context-path-->${ctxPath}
<hr>
获取后台返回的数据--->${username}
</body>
</html>
网友评论