带选择列表的表单:
<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;
}
是什么原因导致标签为空?
在转换器中,您只传递pojo的id值,因此当它被转换时,它基本上是一个带有设置id的null对象。
将您的转换器更改为以下最有可能使第一个标签正确显示:
/**
* {@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是在您的应用程序上下文中定义的。