在Beans,Facelets,Java 8中将接口与默认方法一起使用

时间:2018-10-26 14:53:00

标签: interface java-8 xhtml javabeans

我有一个家长班

public class ParentBean {

    @ManagedProperty(value = "#{bundle}")
    private ResourceBundleBean bundle;

    public ResourceBundleBean getBundle() {
        return bundle;
    }

    public void setBundle(ResourceBundleBean bundle) {
        this.bundle = bundle;
    }

}

现在我有一个豆子

@ManagedBean(name = "servicio")
@ViewScoped
public class ServicioBean extends ParentBean  {
    private Boolean cumple;

    public Boolean getCumple() {
        return cumple;
    }

    public void setCumple(Boolean cumple) {
        this.cumple = cumple;
    }
}

要启用新功能,一种选择是在Parent类中添加属性及其方法。

public class ParentBean {

    @ManagedProperty(value = "#{bundle}")
    private ResourceBundleBean bundle;

    public ResourceBundleBean getBundle() {
        return bundle;
    }

    public void setBundle(ResourceBundleBean bundle) {
        this.bundle = bundle;
    }

    private final Map<String,String> listaSiNo = new HashMap<String, String>() {{
        put("S", "Sí");
        put("N", "No");
    }};

    public Map<String,String> getListaSiNo() {
        return listaSiNo;
    }

}

它在我的facelet(带有继承项)中完美运行。

<p:outputLabel value="Cumple?:" for="itCumple" />
<p:selectOneMenu id="itCumple" value="#{servicio.cumple}" >
    <f:selectItems value="#{servicio.listaSiNo.entrySet()}" var="entry"
                   itemValue="#{entry.key}" itemLabel="#{entry.value}" />
</p:selectOneMenu>

阅读本文: https://www.baeldung.com/java-static-default-methods

但是,我想使用接口方法 为什么?。因为我有多个Bean,而且我不想影响所有Bean。

public interface ListasForBeans {

    default Map<String,String> getListaSiNo() {
        Map<String,String> listaSiNo = new HashMap<String, String>() {{
            put("S", "Sí");
            put("N", "No");
        }};
        return listaSiNo;
    }

}

我只希望将语句implements ListasForBeans放入我选择的Bean中。 (关于接口的内部属性以及受影响的bean中的更多内容,请参见更多方法)。

@ManagedBean(name = "servicio")
@ViewScoped
public class ServicioBean extends ParentBean implements ListasForBeans {
    private Boolean cumple;

    public Boolean getCumple() {
        return cumple;
    }

    public void setCumple(Boolean cumple) {
        this.cumple = cumple;
    }
}

我遇到此错误:

javax.servlet.ServletException: The class 'ServicioBean' 
does not have the property 'listaSiNo'.

有可能吗?

如何做到?

0 个答案:

没有答案