This is what I am using now. Every Entity in my project has an id named id
that is a Long. So all I have to do is add the interface Idable to the entity.
There is no doubt a better way. With discussions like this we can find an
optimal technique that everyone can easily use with minimal setup.
Looking it up from the original list seems like a good idea.
Comments and or criticism are welcome.
| public interface Idable {
| public Long getId();
| }
|
I modifed some code from other posts and came up with the following. Generic
converter.
| public abstract class LookupConverter<T extends Idable> implements Converter
| {
| private Class<T> entityClass;
| private EntityManager em;
|
| public LookupConverter(Class<T> entityClass, EntityManager em) {
| this.entityClass = entityClass;
| this.em = em;
| }
|
| @SuppressWarnings("unchecked")
| public String getAsString(FacesContext facesContext, UIComponent
component, Object obj)
| {
| T t =(T) obj;
| if(t == null || t.getId()==null)
| {
| return "";
| }
| else
| {
| return t.getId().toString();
| }
| }
|
| public Object getAsObject(FacesContext facesContext, UIComponent
component, String str)
| throws ConverterException
| {
| return getObject(str);
| }
|
| private Object getObject(String str)
| {
| try
| {
| return em.find(entityClass, Long.valueOf(str));
| }
| catch (NumberFormatException e)
| {
| throw new ConverterException(new FacesMessage("You must select
something."));
| }
| }
| }
|
I am using inner classes to deliver my converters form a SLSB. I am passing
the EntityManager to the constructor.
| @Stateless
| @Name("converterFactory")
| public class ConverterFactoryBean implements ConverterSizeFactory
| {
| @Logger private Log log;
| @PersistenceContext EntityManager em;
|
| public Converter getSizeConverter()
| {
| return new SizeConverter(em);
| }
|
| public static class SizeConverter extends LookupConverter<Size>
| {
| public SizeConverter(EntityManager em)
| {
| super(Size.class, em);
| }
| }
| }
|
I have a facelets taglib that creates the select field and renders the message
in a mouseover icon with another custom facelets tag.
| <ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
| <tr class="tr0">
| <td>#{selectFieldLabel}</td>
| <td>
| <h:selectOneMenu id="#{selectFieldId}"
| value="#{selectFieldValue}"
| class="select_std w200"
| converter="#{selectFieldConverter}">
| <f:selectItems value="#{selectFieldItems}" />
| </h:selectOneMenu>
| </td>
| <td><pt:errorsField fieldId="#{selectFieldId}" /></td>
| </tr>
| </ui:composition>
|
In the page I set it up as follows:
| <pt:selectField2 selectFieldId="size"
| selectFieldLabel="Size"
| selectFieldValue="#{poCoupling.size}"
| selectFieldItems="#{sizeSelectItems}"
|
selectFieldConverter="#{converterFactory.sizeConverter}"/>
|
View the original post :
http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3955338#3955338
Reply to the post :
http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3955338
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
JBoss-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jboss-user