【问题标题】:Read context-param in CDI bean returns null在 CDI bean 中读取上下文参数返回 null
【发布时间】:2015-01-04 00:51:23
【问题描述】:

我正在尝试从 CDI Bean 应用程序范围内读取 web.xml 中包含的 context-param 属性。不幸的是,返回了一个空值。

@ApplicationScoped
@Named
public class Utils  {
   public String getUpdateFile() {
    FacesContext ctx = FacesContext.getCurrentInstance();    
         return ctx.getExternalContext().getInitParameter("update.file");
   }
}

还有 web.xml:

<context-param>
        <param-name>update.file</param-name>
        <param-value>update.zip</param-value>
</context-param>

是我的错吗?任何解决方法?我的环境是 WildFly 8.1.0

【问题讨论】:

    标签: jakarta-ee cdi wildfly


    【解决方案1】:

    只需注入 Servlet 上下文。 ServletContext 作为应用程序范围的 bean 提供。它可以安全地注入到任何 CDI bean 中,如下所示:

    @Inject   
    private ServletContext context;
    
    public String getUpdateFile() {
    
             return context.getInitParameter("update.file");
    }
    

    在这里编码了一个迷你tutorial。

    【讨论】:

      猜你喜欢
      • 2012-04-12
      • 2016-04-11
      • 2021-02-12
      • 2020-02-10
      • 2013-05-07
      • 1970-01-01
      • 1970-01-01
      • 2020-03-16
      • 2023-03-27
      相关资源
      最近更新 更多