gpt4 book ai didi

java - 如何在 jsp 文件中获取当前的 Spring 配置文件。

转载 作者:行者123 更新时间:2023-11-30 05:50:07 25 4
gpt4 key购买 nike

我正在开发 Spring MVC 应用程序。在我的应用程序中,我有两个配置文件,测试和生产。当我部署我的应用程序以测试 envirionemnt,生产环境中的生产时,测试启用。

我需要在 jsp 文件中获取当前配置文件。是否可以 ?我不想发送额外的变量来获取此信息,因为文件包含在许多其他文件中。谢谢

最佳答案

由于您使用的是 MVC,因此更直接的方法是在 Controller 中 Autowiring 环境并在模型中传递配置文件。

@Autowired
private Environment environment;

@RequestMapping("/needsProfile")
public String needsProfile(Model model) {
model.addAttribute("profiles", environment.getActiveProfiles());
return "needsProfile";
}

在你的 needsProfile.jsp 中:

<jsp:useBean id="profiles" type="java.lang.String[]" scope="request"/>

<%-- ... --%>

<div>First Profile: ${profiles[0]}</div>

关于java - 如何在 jsp 文件中获取当前的 Spring 配置文件。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14582661/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com