带有选项列表的表格:
<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;
}
导致标签为空的原因是什么?
答案 0 :(得分: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是在你的应用程序上下文中定义的。