简体   繁体   English

在WebSphere 9上的JSF 2.2中管理bean

[英]Managing beans in JSF 2.2 on WebSphere 9

Posting this problem / answer as I did not find any other question that was very similar. 发布此问题/答案,因为我没有找到其他任何非常相似的问题。 Lots of related ones but not same scenario. 很多相关的,但情况不一样。

Was upgrading JSF 2.1 to JSF 2.2 (Mojarra) application for h:fileUpload support. 正在将JSF 2.1升级到JSF 2.2(Mojarra)应用程序以支持h:fileUpload。 Application runs on WebSphere 9, and after upgrade was encountering problems with dependency injection using JSF Beans. 应用程序在WebSphere 9上运行,升级后使用JSF Beans进行依赖项注入时遇到问题。

Reading further I realize that JSF @ManagedBean is to be deprecated and should be migrated to CDI @Named but our application had 627 references to the FacesContext and migrating to a CDI bean would mean updating all those places to either produce a FacesContext when needed, or upgrade JSF to 2.3 and inject the FacesContext where it is needed. 进一步阅读,我意识到JSF @ManagedBean已被弃用,应迁移到CDI @Named但是我们的应用程序有627个对FacesContext的引用,并且迁移到CDI bean意味着更新所有这些位置,以便在需要时生成FacesContext或将JSF升级到2.3,并在需要的地方注入FacesContext。 This means staying on JSF 2.2 for now. 这意味着暂时使用JSF 2.2。

Here is the bean : 这是豆子:

package com.company.customersearch;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

@ManagedBean(name = "customerSearchManager")
@ViewScoped
public class customerSearchManager extends PortletManager{

    private static final long serialVersionUID = -9050481751306478727L;
    private Long customerId;
    private SessionData sessionData;

    @ManagedProperty(value = "#{customerSearchService}")
    private CustomerSearchService service;

    public CustomerSearchManager(){
        this.sessionData = new SessionData((HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false));
        if (sessionData.hasSelectedCustomer())
            this.customerId = sessionData.getData().getCustomerId();
    }   

...
}

Running on WebSphere 9 was producing a NullPointerException 在WebSphere 9上运行会产生NullPointerException

Error Rendering View[/pages/customer-search.xhtml]
                                 java.lang.NullPointerException
    at com.sun.faces.application.view.ViewScopedCDIEventFireHelperImpl.fireInitializedEvent(ViewScopedCDIEventFireHelperImpl.java:60)
    at com.sun.faces.application.view.ViewScopeContextManager.fireInitializedEvent(ViewScopeContextManager.java:394)
    at com.sun.faces.application.view.ViewScopeManager.processPostConstructViewMap(ViewScopeManager.java:312)
    at com.sun.faces.application.view.ViewScopeManager.processEvent(ViewScopeManager.java:244)
    at com.sun.faces.application.view.ViewScopeEventListener.processEvent(ViewScopeEventListener.java:68)

The NPE is coming out of a CDI related class! NPE即将脱离CDI相关课程! Why would we get an NPE inside of ViewScopedCDIEventFireHelperImpl.java when intentionally not using CDI?? 当故意不使用CDI时,为什么在ViewScopedCDIEventFireHelperImpl.java获得NPE?

I don't know if other enterprise containers are this way, but for WebSphere, CDI is enabled by default and is apparently overriding or trying to replace the JSF beans at runtime (??). 我不知道其他企业容器是否采用这种方式,但是对于WebSphere,默认情况下启用CDI,并且显然在运行时(??)覆盖或尝试替换JSF bean。 Wish I understood a bit more on what is happening in the container in this scenario. 希望我对这种情况下容器中发生的事情有所了解。

Found the following on the IBM site regarding CDI settings: 在IBM网站上找到有关CDI设置的以下信息:

http://www-01.ibm.com/support/docview.wss?uid=swg21983564 http://www-01.ibm.com/support/docview.wss?uid=swg21983564

There you can see that there are 2 properties to set on your JVM if you are truly intending to use JSF @ManagedBean s and avoid conflicts with CDI all together. 如果确实打算使用JSF @ManagedBean并一起避免与CDI发生冲突,那么可以在JVM上设置两个属性。 Worked for me. 为我工作。

Set : 设置:

com.ibm.ws.cdi.enableCDI = false
com.ibm.ws.cdi.enableImplicitBeanArchives  = false

Hope it helps someone. 希望它可以帮助某人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM