【问题标题】:Application Scoped bean for converter for p:selectOneMenu用于 p:selectOneMenu 转换器的应用范围 bean
【发布时间】:2019-12-23 14:42:34
【问题描述】:

我已经通过 github 和 PrimeFaces 实现了 PrimeFaces selectOneMenu,但不清楚为什么将 themeservice-bean 定义为应用程序范围,例如

@Named
@ApplicationScoped
public class ThemeService {

    private List<Theme> themes;

    @PostConstruct
    public void init() {
        themes = new ArrayList<>();
        themes.add(new Theme(0, "Nova-Light", "nova-light"));
        themes.add(new Theme(1, "Nova-Dark", "nova-dark"));
        themes.add(new Theme(2, "Nova-Colored", "nova-colored"));
        themes.add(new Theme(3, "Luna-Blue", "luna-blue"));
        themes.add(new Theme(4, "Luna-Amber", "luna-amber"));
        themes.add(new Theme(5, "Luna-Green", "luna-green"));
        themes.add(new Theme(6, "Luna-Pink", "luna-pink"));
        themes.add(new Theme(7, "Omega", "omega"));
    }

    public List<Theme> getThemes() {
        return themes;
    } 
}

在我看来,同一个 bean 应该定义为视图/请求范围,因为列表可能需要填充不同的记录,但是当我更改范围时,它会给出错误

java.lang.NullPointerException
    at org.primefaces.showcase.convert.ThemeConverter.getAsObject(ThemeConverter.java:27)

转换器中的返回语句

                ThemeService service = (ThemeService) fc.getExternalContext().getApplicationMap().get("themeService");
                return service.getThemes().get(Integer.parseInt(value));

也许有人对此有所了解。

【问题讨论】:

标签: jsf primefaces


【解决方案1】:

您尝试从仅存储应用程序范围 bean 的 applicationMap 获取请求或查看范围 bean。

尝试用getRequestMap或getViewMap替换getApplicationMap。

或者更好的是@Inject the themeService 到你的转换器中:

How to inject @EJB, @PersistenceContext, @Inject, @Autowired, etc in @FacesConverter?

【讨论】:

  • 太快了……谢谢它解决了。我没有完全理解转换器。
  • @NasirAbbas “它解决了” 它是如何解决的? “太快”是什么意思?
  • 还有来自 applicationMap 的 themeService(applicationScoped) IS 请求.. 很奇怪
  • @kukeltje OP 在问题中提出,只要将 themeService 的范围更改为查看范围或请求范围,从 applicationMap 获取 themeService 就会停止工作。 (这就是我得到问题的方式)。
  • 好的,现在我又读了一遍,你的评论我同意你的解释,但是标题和发布的代码完全无关。 OP 应该发布了一个引发错误的视图或 requestscoped bean,而不是工作代码(因为 发布的代码似乎很好......
猜你喜欢
  • 2012-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-13
  • 2017-04-28
  • 1970-01-01
  • 2013-04-04
相关资源
最近更新 更多