1,配置文件
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210615165530497.png)
2,获取项目相对路径
public class ApplicationHome {
public static String getApplicationHome() {
String applicationHome = System.getProperty("application_home");
String application_home = applicationHome == null ? System.getenv("application_home") : applicationHome;
application_home = StringUtil.isEmptyOrLength0(application_home) ? System.getProperty("user.dir") : application_home;
return application_home;
}
}
3,读取配置文件内容
@RequestMapping(value = "/")
public String main(@RequestParam Map<String,Object> paramMap, Model model) throws Exception {
String fileName = ApplicationHome.getApplicationHome() + "/config/application.properties";
try {
Properties properties = new Properties();
properties.load(new InputStreamReader(new FileInputStream(fileName), "UTF-8"));
String headerZhu=properties.getProperty("headerZhu");
String headerFu=properties.getProperty("headerFu");
model.addAttribute("headerZhu",headerZhu);
model.addAttribute("headerFu",headerFu);
model.addAttribute("hospitalname","XXXX单位");
} catch (IOException e) {
e.printStackTrace();
}
return "index";
}
4,html页面,前端接受数据(语法)
<div class="headerTitle" th:text="${hospitalname}" ></div>
<div class="headerZhu">主编:[[${headerZhu}]]</div>
<div class="headerFu">副主编:[[${headerFu}]]</div>