如何注释辅助类在里面JSF可见? [英] How to annotate helper class to be visible inside JSF?

查看:124
本文介绍了如何注释辅助类在里面JSF可见?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个辅助类,这既不是一个状态 托管bean ,也不是无国籍 EJB ,也不是实体通过 JPA 或休眠。这只不过是该做简单的事情就像回程日期格式和类似的。

I have a helper class, which is neither a stateful managed bean, nor a stateless EJB, nor an entity mapped to a table via JPA or Hibernate. It is simply a collection of static methods that do simple things like return date formats and similar.

鉴于此,为了使Java类是可见的一个JSF里面,这个类必须的方式进行注释,容器指定为可见JSFs,有没有办法来标注一个辅助类不符合任何标准JSF可视类别,使之成为可见?另一种方法,当然,是在传递从JSF调用的辅助类托管bean管道的方法,但我preFER不弄乱我管理的bean,如果我可以从JSF直接调用它。据我所知,从一个JSF具有无状态EJB这样做,将被视为反模式,但在课堂上,我想使用方法都非常简单和非事务。

Given that, in order for a Java class to be visible inside a JSF, that class must be annotated in a way that the container assigns as visible to JSFs, is there a way to annotate a helper class that does not match any of the standard JSF visible categories so that it becomes visible? An alternative, of course, is to have a conduit method in the managed bean that passes the call from the JSF to the helper class but I prefer not to clutter my managed bean if I can call it directly from the JSF. I understand that doing this with a stateless EJB from a JSF would be considered an anti-pattern but the methods in the class I wish to use are all very simple and non-transactional.

推荐答案

标记您的类 @ApplicationScoped 。请确保它有一个公共无参数的构造函数,这个类没有状态,其方法是线程安全的。

Mark your class as @ApplicationScoped. Make sure it has a public no-arg constructor and that this class doesn't have state and its methods are thread safe.

例如

托管bean(纯JSF)

Managed bean (pure JSF)

//this is important
import javax.faces.bean.ApplicationScoped;

@ManagedBean
@ApplicationScoped
public class Utility {
    public static String foo(String another) {
        return "hello " + another;
    }
}

CDI版本

//this is important
import javax.enterprise.context.ApplicationScoped;

@Named
@ApplicationScoped
public class Utility {
    public static String foo(String another) {
        return "hello " + another;
    }
}

查看

<h:outputText value="#{utility.foo('amphibient')}" />

这篇关于如何注释辅助类在里面JSF可见?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆