使用setPropertyActionListener设置ENUM



我试图设置一个enum属性与setPropertyActionListener,但我不知道如何做到这一点。下面是实体:

@Entity
public class Invoice {
    public enum InvoiceStatus { ACTIVE, CANCELED }
        ...
        @Enumerated(EnumType.STRING)
    private InvoiceStatus status;
        ...
        public InvoiceStatus getStatus() {
        return status;
    }

    public void setStatus(InvoiceStatus status) {
        this.status = status;
    }

这里是命令按钮它应该通过setPropertyActionListener

将状态设置为ACTIVE
   ...
  <h:form id="invoiceCreatedSuccessfully">
        <p:dialog header="#{msg['title.success']}" widgetVar="invoiceCreatedSuccessfullyDialog" resizable="false" showEffect="fade" hideEffect="fade">  
            <h:panelGrid columns="2" rows="3" style="margin-bottom: 10px">  
                <h:outputText value="#{msg['message.invoiceCreatedSuccessfully']}" />
            </h:panelGrid>  
            <p:commandButton value="#{msg['label.acknowledged']}" actionListener="#{invoiceManager.reload}" action="viewInvoices">
                <f:setPropertyActionListener target="#{invoiceManager.invoice.status}" value="ACTIVE" />
            </p:commandButton>
        </p:dialog>
    </h:form>

没有错误报告,但是数据库中的字段'status'没有被设置。有人能告诉我为什么吗?

string在EL中不直接转换为enum,你需要在faces-config中自定义转换,jsf有一个enum转换器应该为你工作,

<converter>
  <converter-for-class>java.lang.Enum</converter-for-class>
  <converter-class>javax.faces.convert.EnumConverter</converter-class>
</converter>
罢工

现在查看EnumConverter的源代码,似乎只有当targetClass在converter中可用时才能工作。

所以你需要扩展它来使用枚举

public class MyEnumConverter extends EnumConverter {
  public MyEnumConverter () {
    super(MyEnum.class);
  }
}
<converter>
  <converter-id>MyEnum</converter-id>
  <converter-class>com.test.MyEnumConverter</converter-class>
</converter>

在你的组件中添加<f:converter converterId="MyEnum"/>

如果您有许多枚举,并且为了简化操作,您可以查看omnifaces http://showcase.omnifaces.org/converters/GenericEnumConverter

相关内容

  • 没有找到相关文章

最新更新