簡體   English   中英

如何在JSF視圖中將抽象類用作CDI托管bean

[英]How to use abstract class as CDI managed bean in JSF view

我可以在JSF視圖中使用抽象類作為CDI托管bean嗎? 我想在派生類中使用set或override屬性,並在父抽象類的JSF頁面中使用它。 派生視圖以設置上下文,模板父視圖必須顯示為所有子項的公共部分。 我聽說我可以在JSF視圖中使用帶有@Named注釋的抽象類,但是我得到了錯誤

目標無法訪問,標識符'viewModel'已解析為null

如果我將抽象類更改為典型類,那么它的工作。 可能在JSF視圖中使用抽象類是不可能的?

ViewModel.java

@Named
@ConversationScoped
@Inherited
@Documented
@Stereotype
@Target({ TYPE })
@Retention(RUNTIME)
public abstract class ViewModel {
    private String foo;

    public void setFoo(String foo) {
        this.foo = foo;
    }

    public String getFoo() {
        return foo;
    }

    abstract void bar();
}

DerivedViewModel.java

@Named
@ConversationScoped
@Inherited
@Documented
@Stereotype
@Target({ TYPE })
@Retention(RUNTIME)
public class DerivedViewModel extends ViewModel {
    public void init() {
        this.setFoo("Foo");
    }

    @Override
    void bar() {;}
}

View.xhtml

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://xmlns.jcp.org/jsf/html" xmlns:f="http://xmlns.jcp.org/jsf/core"
    xmlns:ui="http://xmlns.jcp.org/jsf/facelets" xmlns:c="http://xmlns.jcp.org/jsp/jstl/core"
    xmlns:fn="http://xmlns.jcp.org/jsp/jstl/functions" xmlns:pt="http://xmlns.jcp.org/jsf/passthrough"
    xmlns:p="http://primefaces.org/ui" xmlns:pe="http://primefaces.org/ui/extensions"
    xmlns:sys="http://argustelecom.ru/system" xmlns:o="http://omnifaces.org/ui">

    <ui:param name="viewModel" value="#{viewModel}" />

    <ui:define name="body">
        <h:outputText value="#{viewModel.foo}" />

        <ui:insert name="derived" />
    </ui:define>

</ui:composition>

DerivedView.xhtml

<ui:composition template="View.xhtml" xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://xmlns.jcp.org/jsf/html" xmlns:f="http://xmlns.jcp.org/jsf/core"
    xmlns:ui="http://xmlns.jcp.org/jsf/facelets" xmlns:c="http://xmlns.jcp.org/jsp/jstl/core"
    xmlns:fn="http://xmlns.jcp.org/jsp/jstl/functions" xmlns:pt="http://xmlns.jcp.org/jsf/passthrough"
    xmlns:p="http://primefaces.org/ui" xmlns:pe="http://primefaces.org/ui/extensions"
    xmlns:sys="http://argustelecom.ru/system" xmlns:o="http://omnifaces.org/ui">

    <ui:param name="viewModel" value="#{derivedViewModel}" />
    <ui:define name="metadata">
        <f:metadata>
            <f:viewAction action="#{derivedViewModel.init()}"/>
        </f:metadata>
    </ui:define>

    <ui:define name="derived">
        blablabla
    </ui:define>

</ui:composition>

忘記EL或JSF。 考慮OO。 你能打這個電話嗎?

viewModel.foo

答案是否定的,因為它是abstract並且viewModel不能作為abstractViewModel的實例存在。 出於同樣的原因,你不能在EL中使用它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM