Formularz z listą wyboru:
<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" />
Gdy nie wpisuję liczby, kliknij przycisk polecenia - wyświetlony zostanie komunikat sprawdzania poprawności, ale etykiety obiektu w liście wyboru zostaną zaktualizowane i są to tylko puste nawiasy.
itemLabel="#{integration.name} (#{integration.backend.name})"
Kiedy wybieram je i wpisuję nazwę - wszystko jest w porządku (działa tak, jakbym wypełnił imię w pierwszej kolejności), więc jedynym problemem są etykiety. To jest mój konwerter:
@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();
}
}
I metoda na komponencie bean w celu uzyskania integracji (komponent bean ma zasięg widzenia):
public DualListModel<Integration> getIntegrations() {
integrations.setSource(customerService.getIntegrations(customer));
return integrations;
}
Co powoduje, że etykiety są puste?
Odpowiedzi:
0 dla odpowiedzi № 1W konwerterze przekazujesz tylko wartość id twojego pojo, więc kiedy zostanie przekonwertowany, jest to po prostu obiekt zerowy o ustalonym id.
Zmiana konwertera na poniższy spowoduje najprawdopodobniej poprawne wyświetlenie pierwszej etykiety:
/**
* {@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();
}
Jeśli jednak twoja klasa jest trwałym bytem, możesz chcieć pobrać obiekt z bazy danych w konwerterze, sprawdź następujący kod:
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;
}
Zakładam, że używasz hibernacji i wiosny, a bean sessionFactory jest zdefiniowany w kontekście aplikacji.