【问题标题】:How to get current Spring profile in jsp file.如何在 jsp 文件中获取当前的 Spring 配置文件。
【发布时间】:2013-01-13 00:12:59
【问题描述】:

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

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

【问题讨论】:

    标签: java spring jsp profile


    【解决方案1】:

    我知道这是一个旧线程,但也许有人仍在寻找它。

    在jsp中有一个简单的访问环境的方法,无需实现自定义类或taglibs:

    <spring:eval expression="@environment.getActiveProfiles()"></spring:eval>
    

    当然jsp必须声明:

    <%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
    

    我使用的是 spring 4.2,但它是从 3.0.1 开始实现的

    【讨论】:

      【解决方案2】:

      为了更优雅的方式,您可以创建自定义 JSP 标记。这是一个可以用来实现此目的的教程:http://blog.florianlopes.io/access-spring-profiles-with-custom-jsp-tag/

      使用此自定义标签,您将能够根据活动的 Spring 配置文件限制区域。

      这个想法是扩展 Spring RequestContextAwareTag。它将使您的自定义标签可以访问 Spring 配置文件。扩展这个类,实现很简单:

      public class ProfileConditionTag extends RequestContextAwareTag {
      
          private String expectedProfile;
      
          @Override
          protected int doStartTagInternal() throws Exception {
              final Environment environment = this.getRequestContext().getWebApplicationContext().getEnvironment();
              if (environment != null) {
                  final String[] activeProfiles = environment.getActiveProfiles();
                  if (ArrayUtils.contains(activeProfiles, this.expectedProfile)) {
                      return EVAL_BODY_INCLUDE;
                  }
              }
      
              return SKIP_BODY;
          }
      
          // Getters, setters
      }
      

      SKIP_BODY 返回值告诉 JSP 处理器在活动配置文件不是预期的情况下跳过标签正文。

      1. 创建标签库描述并导入标签:

        <%@ taglib prefix="tagprefix" uri="/WEB-INF/taglib/profile.tld" %>
        
      2. 使用它:

        <tagprefix:profile value="dev"> Only displayed if the active Spring profile is "dev". </tagprefix:profile>

      【讨论】:

        【解决方案3】:

        由于您使用的是 MVC,因此更直接的方法是在控制器中自动装配环境并在模型中传递配置文件。

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

        在你需要的Profile.jsp:

        <jsp:useBean id="profiles" type="java.lang.String[]" scope="request"/>
        
        <%-- ... --%>
        
        <div>First Profile:  ${profiles[0]}</div>
        

        【讨论】:

          【解决方案4】:

          你可以使用类似下面的东西:

          ServletContext sc = request.getSession().getServletContext();
          WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(sc);
          String[] profiles = applicationContext.getEnvironment().getActiveProfiles();
          

          【讨论】:

          • 感谢您的快速回复,但抱歉,我不明白。我问我是否可以从 JSP 代码中获取哪些配置文件处于活动状态的信息。为什么我应该在文件或数据库中存储额外的属性来解决我的问题?
          猜你喜欢
          • 2010-10-26
          • 2011-08-18
          • 1970-01-01
          • 1970-01-01
          • 2022-07-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多