0

带有选项列表的表格:

<p:inputText id="number" value="#{accountNumberBean.account.accountNumber}" required="true" label="#{msg['newAccountNumberForm.number']}">
    <f:validateLength maximum="50" for="name" />
</p:inputText>

<p:message for="number" display="text" />

<p:pickList id="integrations" value="#{accountNumberBean.integrations}" var="integration" itemLabel="#{integration.name} (#{integration.backend.name})" itemValue="#{integration}" converter="integrationConverter">
    <f:facet name="sourceCaption">#{msg['newAccountNumberForm.integrations.available']}/>
    <f:facet name="targetCaption">#{msg['newAccountNumberForm.integrations.used']}/>
</p:pickList>
<p:commandButton value="#{msg['button.createAndNext']}" action="#{accountNumberBean.addNewAndStay}" update="@form" />

当我不填写数字输入时,单击命令按钮 - 显示验证消息,但选项列表中的对象标签会更新,它们只是空括号。

itemLabel="#{integration.name} (#{integration.backend.name})"

当我选择它们并填写名称时 - 一切正常(就像我首先填写名称一样),所以唯一的问题是标签。这是我的转换器:

@FacesConverter(forClass = Integration.class, value = "integrationConverter")
public class IntegrationConverter implements Converter {

    /**
     * {@inheritDoc} 
     */
    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        Integration integration = new Integration();
        integration.setId(Long.parseLong(value));
        return integration;
    }

    /**
     * {@inheritDoc} 
     */
    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        return ((Integration) value).getId().toString();
    }

}

以及 bean 上获取集成的方法(bean 是视图范围的):

public DualListModel<Integration> getIntegrations() {
        integrations.setSource(customerService.getIntegrations(customer));
        return integrations;
    }

是什么导致标签为空?

4

1 回答 1

0

在你的转换器中,你只传递你的 pojo 的 id 值,所以当它被转换时,它基本上是一个带有设置 id 的空对象。

将转换器更改为以下很可能会使第一个标签正确显示:

 /**
 * {@inheritDoc} 
 */
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
    Integration integration = new Integration();
    String[] split = value.split("_");
    integration.setId(split[0]);
    integration.setName(split[1]);
    return integration;
}

/**
 * {@inheritDoc} 
 */
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
    Integration i = (Integration) value;
    return i.getId() + "_" + i.getName();
}

但是,如果您的类是一个持久实体,您可能希望从转换器的数据库中检索对象,请检查以下代码:

private SessionFactory sessionFactory;

@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
    Object object = null;

    if (StringUtils.isNotBlank(value)) {
        try {
            int underscoreIndex = value.indexOf("_");
            String className = value.substring(0, underscoreIndex);
            String id = value.substring(underscoreIndex + 1, value.length());

            Class<?> clazz = Class.forName(className);
            object = getSessionFactory().getCurrentSession().get(clazz, Long.parseLong(id));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    return object;
}

@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
    String result = null;

    if (value != null && !value.equals("")) {
            Integration obj = (Integration) value;
            result = Hibernate.getClass(obj).getName() + "_" + obj.getId();
        }
    return result;
}

private SessionFactory getSessionFactory() {
    if(sessionFactory == null) {
        sessionFactory = ((SessionFactory) FacesContextUtils                    .getWebApplicationContext(FacesContext.getCurrentInstance())
                .getBean("sessionFactory"));
    }

    return sessionFactory;
}

我假设您使用的是 hibernate 和 spring,并且 sessionFactory bean 是在您的应用程序上下文中定义的。

于 2013-02-25T15:55:37.670 回答