Thanks for your reply.
what confuses me is, that your picklist maps f.name->f but your converter converts object to id.
Furthermore the p:picklist returns a vector in my case, so casting like u do in
is not possible.
Here is my code, I managed to get my EJB via a JNDI lookup and the getAsObject() part works quite well, but the getAsString() part throws me an
Code: Select all
java.lang.NumberFormatException: For input string: "name"
converter:
Code: Select all
@FacesConverter(value = "NamePlayerConverter")
public class NamePlayerConverter implements Converter {
/**
* converts the String representation of the key back to the Object
*/
@Override
public Object getAsObject(FacesContext context, UIComponent component,
String value) {
// will throw new IllegalArgumentException if it can't parse.
Object obj = new Object();
CRUDService crudService;
try {
crudService = ServiceLocator.getInstance().getService(CRUDService.class);
obj = crudService.findWithNamedQueryAndParam(Player.findByName, "name", value);
} catch (Exception ex) {
Logger.getLogger(NamePlayerConverter.class.getName()).log(Level.SEVERE, null, ex);
}
return obj;
}
/**
* converts the Key object into its String representation.
*/
@Override
public String getAsString(FacesContext context, UIComponent component,
Object value) {
System.out.print(value.getClass().toString());
if (value instanceof Player) {
Player player = (Player) value;
return player.getName();
}
else if(value instanceof Vector){
Vector vector = (Vector) value;
Player player = (Player) vector.get(0);
return player.getName();
}
else {
throw new IllegalArgumentException("Cannot convert non-player object in NamePlayerConverter");
}
}
}
xhtml page
Code: Select all
<p:panel header="Neues Match melden">
<h:panelGrid columns="6">
<h:outputText value="Team 1"/>
<p:pickList value="#{gameController.d1}"
var="player" iconOnly="true" effect="bounce"
itemLabel="#{player.name}"
itemValue="#{player}" converter="NamePlayerConverter"
showSourceControls="false" showTargetControls="false" >
<f:facet name="sourceCaption">Spieler auswählen</f:facet>
<f:facet name="targetCaption">Teamaufstellung</f:facet>
</p:pickList>
ServiceLocator
Code: Select all
public class ServiceLocator {
private static ServiceLocator instance;
InitialContext context;
private ServiceLocator() throws Exception {
try {
Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.enterprise.naming.SerialInitContextFactory");
p.put(Context.URL_PKG_PREFIXES,
"com.sun.enterprise.naming");
p.put(Context.PROVIDER_URL, "iiop://127.0.0.1:3700");
context = new InitialContext(p);
} catch (NamingException e) {
throw new Exception(e);
}
}
public static ServiceLocator getInstance() throws Exception {
if (instance == null)
instance = new ServiceLocator();
return instance;
}
@SuppressWarnings("unchecked")
public <T> T getService(Class<T> clazz) throws Exception {
try {
String lookupName = "java:global/nC-Kicker-Ticker/" + clazz.getSimpleName() + "!" + "de.nobiscum.nckickerticker.EJB" + "." + clazz.getSimpleName();
showJndiContext(context, null, null);
T lookup = (T) context.lookup(lookupName);
return lookup;
} catch (ClassCastException e) {
throw new Exception(e);
} catch (NamingException e) {
throw new Exception(e);
}
}
Would be awesome if you could take a look, cause it is getting really frustrating