-
1. Re: Custom JSF Converter & CDI
kenfinni Feb 25, 2013 9:42 AM (in response to mbasovni)Martin,
I'm confused about what the problem is.
Are you getting an exception of some kind?
Or is the code you have not doing anything?
Ken
-
2. Re: Custom JSF Converter & CDI
mbasovni Feb 26, 2013 1:09 PM (in response to kenfinni)When I have for my CalendarConverter only one annotation:
@FacesConverter(forClass=Calendar.class)
and I inject EntityManager just like this:
@Inject private EntityManager em;
I get NullPointerException. Look at file "log1".
When I tried to add:
@Named
=> the same exception
Gatein: 3.5.2.Final
Portletbridge: 3.2.0.Beta1
JBoss AS: 7.1.3.Final
cdi-portlet-integration: 1.0.0.Alpha2
-
log1.zip 2.8 KB
-
-
3. Re: Custom JSF Converter & CDI
kenfinni Feb 26, 2013 1:13 PM (in response to mbasovni)Martin,
After looking at http://stackoverflow.com/questions/13182660/conversion-error-setting-value-52-for-null-converter did you add @ManagedBean onto your converter?
As BalusC points out in his response, without @ManagedBean the EntityManager will not be injected. Also, the EntityManager would need to use @PersistenceContext instead of @Inject, unless you had a CDI producer creating the EntityManager that could then be injected.
Ken
-
4. Re: Custom JSF Converter & CDI
mbasovni Feb 26, 2013 1:23 PM (in response to kenfinni)Ken,
look at it. It does not work!
package dip.xbasov00.calendar.util; import javax.faces.bean.ManagedBean; import javax.faces.component.UIComponent; import javax.faces.context.FacesContext; import javax.faces.convert.Converter; import javax.faces.convert.FacesConverter; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import dip.xbasov00.calendar.domain.Calendar; @ManagedBean @FacesConverter(forClass=Calendar.class) public class CalendarConverter implements Converter { @PersistenceContext private EntityManager em; @Override public Object getAsObject(FacesContext context, UIComponent component, String value) { System.out.println("CalendarConverter.getAsObject: " + value); if (em == null) System.out.println("EntityManager is NULL !!!!"); return em.find(Calendar.class, Integer.parseInt(value)); } @Override public String getAsString(FacesContext context, UIComponent component, Object value) { System.out.println("CalendarConverter.getAsString: " + value.toString()); return String.valueOf(((Calendar) value).getId()); } }
NullPointerException again... :-(
Martin
-
5. Re: Custom JSF Converter & CDI
mbasovni Mar 11, 2013 7:14 AM (in response to mbasovni)Problem is resolved:
@FacesConverter(value="calendarConverter", forClass=Calendar.class) public class CalendarConverter implements Converter { private CalendarDAO calendarDao; public CalendarConverter() { super(); try { InitialContext ic = new InitialContext(); calendarDao = (CalendarDAO) ic.lookup("java:module/EJBCalendarDAO"); } catch (NamingException e) { e.printStackTrace(); } } @Override public Object getAsObject(FacesContext context, UIComponent component, String value) { Calendar c = calendarDao.getCalendar(Integer.parseInt(value)); return c; } @Override public String getAsString(FacesContext context, UIComponent component, Object value) { return String.valueOf(((Calendar) value).getId()); } }
Calendar class must override metod equals and hashCode!
-
6. Re: Custom JSF Converter & CDI
kenfinni Mar 11, 2013 9:18 AM (in response to mbasovni)Martin,
Glad you were able to figure out the problem.
Ken