来自selectOneMenu的Setter例程未调用

时间:2017-07-08 15:25:24

标签: java jsf primefaces jsf-2

我使用的是Primefaces 6.1并且与selectOneMenu之一存在问题。我已将页面缩小为selectOneMenu-Item,但无法确定问题所在的位置。

的XHTML页:

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui" template="/WEB-INF/template.xhtml">
    <ui:define name="content">
        <h:form id="formCenter">
            <div class="ContainerIndent">
                <div class="EmptyBox20"/>
                <p:selectOneMenu value="#{bankAccountEntryClearing.selectedInterfaceFile}"
                                 var="interfaceFileItem"
                                 converter="#{interfaceFileConverter}">  
                    <f:selectItem itemLabel="Bank-Daten auswählen" itemValue="#{null}" noSelectionOption="true"/>
                    <f:selectItems value="#{bankAccountEntryClearing.items}" 
                                   var="interfaceFileItem" 
                                   itemLabel="#{interfaceFileItem.fileName}" 
                                   itemValue="#{interfaceFileItem}"/>              
                    <p:column style="text-align: left">  
                        #{interfaceFileItem.fileName}
                    </p:column>  
                    <p:ajax event="change" process="@this" update=":formCenter:testSetterId"/>
                </p:selectOneMenu>
            </div>
            <p:inputText id="testSetterId" value="#{bankAccountEntryClearing.fileName}"/>
        </h:form>
    </ui:define>
</ui:composition>

转换器:

@ManagedBean
@SessionScoped
@Named("interfaceFileConverter")
public class InterfaceFileConverter implements Converter {

    private final Logger logger = Logger.getLogger(this.getClass().getName());

    @Inject
    private InterfaceFileFacade ejbFacade;

    private Integer getKey(String value) {
        Integer key = Integer.valueOf(value);
        return key;
    }

    public String getAsString(FacesContext fc, UIComponent uic, Object object) {
        if (object == null) return null;
        if (object instanceof InterfaceFile) 
            return String.valueOf(((InterfaceFile) object).getInterfaceFileId());
        return null;
    } 


    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        if (value == null || value.length() == 0) {
            logger.info("Interface Converter : null");
            return null;
        }
        InterfaceFile interfaceFile = this.ejbFacade.findInterfaceFile(getKey(value));
        logger.info("Interface Converter : " + interfaceFile.getInterfaceFileId());
        return interfaceFile;
    }
}

豆:

@SessionScoped
@ManagedBean
@Named(value = "bankAccountEntryClearing")
public class BankAccountEntryClearing extends AbstractHouseController<InterfaceFile> implements Serializable {

    private static final long serialVersionUID = 1L;
    private final Logger logger = Logger.getLogger(this.getClass().getName());

    @Inject
    private InterfaceFileFacade         ejbFacade;

    private InterfaceFile               interfaceFile = null;
    private Integer                     houseId;
    private List<InterfaceFile>         interfaceFiles = null;

    public BankAccountEntryClearing() {
        super(InterfaceFile.class);
    }

    @PostConstruct
    public void init() {
        super.setFacade(ejbFacade);
    }

    public List<InterfaceFile> getItems() {
        if (this.getHouseId() == null)
            return null;

        if (this.getHouseId() != this.houseId) {
            interfaceFiles = ejbFacade.getInterfaceFiles(this.getHouseId(),1);
        }

        return interfaceFiles;
    }

    public InterfaceFile getSelectedInterfaceFile() {
        logger.info("Interface Bean getter");
        return interfaceFile;
    }

    public void setSelectedInterfaceFile(InterfaceFile interfaceFile) {
        logger.info("Interface Bean setter: " + interfaceFile.getFileName());
        this.interfaceFile      = interfaceFile;
    }

    public String getFileName() {
        if (this.interfaceFile != null)
            return this.interfaceFile.getFileName();
        return "";
    }
}

物品是普通加载的,可以选择。选择一个项后,将调用转换器中的getAsObject,但不会调用setter-routine。

16:55:23,411 INFO  [com.np.propmgmt.controller.BankAccountEntryClearing] (default task-30) Interface Bean getter
16:55:23,412 INFO  [com.np.propmgmt.controller.BankAccountEntryClearing] (default task-30) Interface Bean getter
16:55:27,772 INFO  [com.np.propmgmt.converter.InterfaceFileConverter] (default task-48) Interface Converter : 69

1 个答案:

答案 0 :(得分:0)

我找到了一个后来并解决了这个问题。原因是实体类对象InterfaceFile本身。 这个JPA实体类是唯一一个由Eclipse JPA-Tool生成的实体类。在这种情况下,没有“等于”和“hashCode”功能 生成了主键。在选择项目时,返回的InterfaceFile(来自getAsObject)的“selectOneMenu”比较 列表(来自getItems)和failes。添加“”后,显示验证错误。

将这两个函数添加到InterfaceFile-entity-class后,一切正常。

Validation Error: Value is not valid

相同