Klasa konwertera zmapowana jako sourceCityConverter w faces-config.xml
public class SourceCityConverter implements Converter {
@Override
public SourceCity getAsObject(FacesContext context, UIComponent component, String value) {
try {
SourceCity s = new SourceCity();
String[] staffInfoComponents = value.split(" ");
s.setId(Integer.parseInt(staffInfoComponents[0]));
return s;
} catch (Exception e) {
throw new UnsupportedOperationException("Operacja zakończona niepowodzeniem");
}
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
try {
SourceCity s = (SourceCity) value;
return s.getId()+" "+s.getCityName();
} catch (Exception e) {
throw new UnsupportedOperationException("Niepowodzenie");
}
}
}
Klasa Managed Beana zmapowana jako sourceCity
public class SourceCityController {
@EJB
private SourceCityFacade sourceCityFacade;
private SourceCity sourceCity;
/** Creates a new instance of SourceCityController */
public SourceCityController() {
}
public String showMessage() {
FacesContext context = FacesContext.getCurrentInstance();
HttpServletRequest myRequest = (HttpServletRequest) context.getExternalContext().getRequest();
String message = myRequest.getParameter("message");
return message;
}
public void setSourceCity(SourceCity sourceCity) {
this.sourceCity = sourceCity;
}
public SourceCity getSourceCity() {
return this.sourceCity;
}
public List<SourceCity> getSourceCities() {
return sourceCityFacade.findAll();
}
public SelectItem[] getSourceCitySelectItem() {
SelectItem[] options = null;
List<SourceCity> sourceCities = sourceCityFacade.findAll();
if (sourceCities != null && sourceCities.size() > 0) {
int i = 0;
options = new SelectItem[sourceCities.size()];
for (SourceCity si : sourceCities) {
options[i++] = new SelectItem(si,
si.getCityName());
}
}
return options;
}
public String chooseSourceCity() {
//String r = "success";
String r ="";
// To jest taki kod dla "picu" ;), po prostu debuguje czy zapisuje
// odpowiedni obiekt czy tez nie
if (this.getSourceCity().getCityName().equals("1 Madryt")) {
r="success";
addSuccessMessage("Szukanie Madrytu powiodło sie. ;)");
} else {
r="failed";
addErrorMessage("Nie powiodło się szukanie Madrytu.");
}
return r;
}
private void addErrorMessage(String msg) {
FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg);
FacesContext fc = FacesContext.getCurrentInstance();
fc.addMessage(null, facesMsg);
}
private void addSuccessMessage(String msg) {
FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, msg);
FacesContext fc = FacesContext.getCurrentInstance();
fc.addMessage("successInfo", facesMsg);
}
}
Jeśli chodzi o pobieranie wartości z baze poprzez fasade to daje radę:
http://imgur.com/uCcbu.jpg
Natomiast po naciśnięciu Szukaj Lotu:
http://imgur.com/5Wjis.jpg
Edycja:
Czyli reasumując pobieranie działa doskonale, nie wiem natomiast jak zapisać wybrany obiekt z selectOneMenu do ManagedBeana.
Jakieś pomysły?