JSF 应用程序范围内的托管 Bean 注入

发布于 2024-12-20 00:08:41 字数 613 浏览 5 评论 0原文

我正在开发一个网络应用程序,使用 jsf2、spring 和 hibernate。

我有一个带有 JSF util 方法的“应用程序范围”托管 bean(称为“utilsJSF”),例如:

  • 读取属性文件以从其密钥获取消息(使用 getBundle)。
  • 添加字符串作为面部消息,稍后显示在视图上。
  • 等等,

我将这个bean注入到我的“基本控制器”中(作为“托管属性”),以便我的所有控制器都扩展这个bean,并且可以访问这些实用程序。我这里没有问题。

但是,如何使用非“托管 bean”的其他类中的这些实用程序呢?

我将解释一下自己:

我有一个异常层次结构,其中每个特定的异常类都必须访问资源包(属性文件),其中键是异常的名称,值是我要向用户显示的消息。异常构造函数从文件中获取值,将其存储在异常的字段中,然后控制器将消息作为 facesmessage 向用户显示。

我可以使用“utilsJSF”托管 bean 显示来自控制器的消息,因为我将其注入到 BaseController 中。

但我无法将“utilsJSF”注入到异常类中以便使用它(因为异常类不是托管bean)。

解决这个问题的最佳方案是什么?

I'm developping a web app, with jsf2,spring and hibernate.

I have an "application scoped" managed bean (called "utilsJSF") with JSF util methods, such as:

  • Reading properties files for getting a message from its key (using getBundle).
  • Adding a string as a facesmessage to be shown later on a view.
  • etc

I inject this bean in my "Base Controller" (as a "managed property"), so that all my controllers extend this one, and can access those utilities. Here I have no problem.

But, how can I use these utilities from other classes that are not "managed beans"?

I will explain myself:

I have an exception hierarchy, where each particular exception class has to access a resource bundle (property file), where the key is the name of the exception and the value is the message I'm gonna show to the user. The exception constructor gets the value from the file, stores it in a field of the exception, and then the controller shows the message to the user as a facesmessage.

I can show the message from the controller using the "utilsJSF" managed bean, because I injected it into the BaseController.

But I cannot inject "utilsJSF" into the exception class in order to use it (because the exception classes are not managed beans).

What's the best solution to solve this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

硬不硬你别怂 2024-12-27 00:08:41

您应该将所有这些实用程序方法(那些(或可以是)public static)重构为真正实用程序类,而不是将其保留在应用程序范围的 bean 中。

public final class Faces {

    private Faces() {
        // Prevent construction.
    }

    public static void addGlobalInfoMessage(String summary) {
        FacesContext.getCurrentInstance().addMessage(null,
            new FacesMessage(FacesMessage.SEVERITY_INFO, summary, null));
    }

    // ...
}

这样您就可以在任何地方使用它。创建一个类似的用于获取捆绑消息。

You should refactor all those utility methods (the ones which are (or can be) public static), into a real utility class, not keep it in an application scoped bean.

public final class Faces {

    private Faces() {
        // Prevent construction.
    }

    public static void addGlobalInfoMessage(String summary) {
        FacesContext.getCurrentInstance().addMessage(null,
            new FacesMessage(FacesMessage.SEVERITY_INFO, summary, null));
    }

    // ...
}

This way you can use it everywhere. Create a similar one for getting bundle messages.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文