Cannot use a custom Converter...
jbosskiki Mar 1, 2007 1:07 PMHi,
I'm new in seam framework and I'd like to use a Converter. I've seen the sample for the dvd store and I've tried to do mine.
The Converter is created correctly (seen in debug mode) and after I've an exception....
Here is the code:
Converter.java:
@Name("titleconverrter") public class TitleViewConverter implements Converter { public TitleViewConverter() { System.out.println("constructed..."); } public String getAsString(FacesContext context, UIComponent component, Object value) { System.out.println("Converting to a String..."); if (value == null) return null; Title title = (Title) value; return title.getType().toString(); } public Object getAsObject(FacesContext context, UIComponent component, String value) { System.out.println("Converting to an Object..."); if (value == null || value.length()==0) return null; return null; } }
The EJB with the value to set:
User.java:
@Entity @Name("user") @Table(name = "USER_TABLE") @NamedQueries( { @NamedQuery(name = MappedQueries.USER_QUERY_FIND_BY_CREDENTIAL, query = "select u from User u where u.password = :password and u.login=:login and u.login is not null and u.password is not null"), @NamedQuery(name = MappedQueries.USER_QUERY_FIND_BY_LOGIN, query = "select u from User u where u.login=:login") }) public class User implements java.io.Serializable { private static final long serialVersionUID = 1; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(precision = 4, scale = 0) private short iduser; @ManyToOne @JoinColumn(name = "IDTITLE") private Title civility; @Column(length = 50) private String firstname; @Column(length = 50) private String middlename; @NotNull @Column(nullable = false, length = 50) private String lastname; @NotNull @Column(length = 25) private String password; @NotNull @Column(length = 10, nullable = false) private String login; @Column(length = 200) private String note; @ManyToMany(fetch = FetchType.LAZY) @JoinTable(name = "CREDENTIAL", joinColumns = { @JoinColumn(name = "IDUSER", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "IDROLE", nullable = false, updatable = false) }) private List<Role> roles; public User() { } public User(String lastname) { this.lastname = lastname; } public User(String firstname, String middlename, String lastname, String password, String login) { this(lastname); this.firstname = firstname; this.middlename = middlename; this.password = password; this.login = login; } public Title getCivility() { return civility; } public void setCivility(Title civility) { this.civility = civility; } /** * @return the firstname */ public String getFirstname() { return firstname; } /** * @param firstname * the firstname to set */ public void setFirstname(String firstname) { this.firstname = firstname; } /** * @return the iduser */ public short getIduser() { return iduser; } /** * @param iduser * the iduser to set */ public void setIduser(short iduser) { this.iduser = iduser; } /** * @return the lastname */ public String getLastname() { return lastname; } /** * @param lastname * the lastname to set */ public void setLastname(String lastname) { this.lastname = lastname; } /** * @return the login */ public String getLogin() { return login; } /** * @param login * the login to set */ public void setLogin(String login) { this.login = login; } /** * @return the middlename */ public String getMiddlename() { return middlename; } /** * @param middlename * the middlename to set */ public void setMiddlename(String middlename) { this.middlename = middlename; } /** * @return the password */ public String getPassword() { return password; } /** * @param password * the password to set */ public void setPassword(String password) { this.password = password; } public void setNote(String note) { this.note = note; } public String getNote() { return note; } /** * @return the roles */ public List<Role> getRoles() { if (roles == null) roles = new Vector<Role>(0); return roles; } /** * @param roles * the roles to set */ public void setRoles(List<Role> roles) { this.roles = roles; } /* * (non-Javadoc) * * @see java.lang.Object#hashCode() */ @Override public int hashCode() { final int PRIME = 31; int result = 1; result = PRIME * result + iduser; return result; } /* * (non-Javadoc) * * @see java.lang.Object#equals(java.lang.Object) */ @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; final User other = (User) obj; if (iduser != other.iduser) return false; return true; } @PrePersist @PreUpdate public void format() { firstname = Formatter.captializeFirstWordsLetter(firstname); middlename = Formatter.captializeFirstWordsLetter(middlename); lastname = Formatter.toUpperCase(lastname); } }
The EJB session to query the db and display the Title (I use a View to get the label with the correct locale
TitleManager.java:
@Local public interface TitleManager { public void loadData(); public List<SelectItem> getTitles(); public Title getNullTitle(); public void destroy(); }
and the EJB class:
TitleManagerBean.java
@Stateful @Scope(ScopeType.EVENT) @Name("titlemanager") public class TitleManagerBean implements TitleManager { @PersistenceContext (unitName = "pu.agaetis-jta") private EntityManager em; @EJB private CommonFacade common; @In private Locale locale; protected List<TitleView> titles; private List<SelectItem> items; @Create public void loadData() { titles = common.findAllTitleViews(em, common.findLocaleByLocale(em, locale)); items = new ArrayList<SelectItem>(titles.size()); for (TitleView title : titles) items.add(new SelectItem(title.getTitle(), title.getText())); } public List<SelectItem> getTitles() { return items; } public Title getNullTitle() { return new Title(); } @Destroy @Remove public void destroy() { } }
finally my html page:
register.xhtml:
<h:form id="register"> <fieldset><s:validateAll> <f:facet name="aroundInvalidField"> <s:span styleClass="errors" /> </f:facet> <div class="entry"> <div class="label"><h:outputLabel for="civility">Title:</h:outputLabel></div> <div class="input"><s:decorate> <h:selectOneMenu value="#{user.civility}" converter="#{titleconverrter} "> <f:selectItem itemLabel="None" itemValue="#{titlemanager.nullTitle}" /> <f:selectItems value="#{titlemanager.titles}"/> </h:selectOneMenu> <a:outputPanel id="titleErrors"> <s:message /> </a:outputPanel> </s:decorate></div> </div> <div class="entry"> <div class="label"><h:outputLabel for="firstname">First Name:</h:outputLabel></div> <div class="input"><s:decorate> <h:inputText id="firstname" value="#{user.firstname}" required="false"> <a:support event="onblur" reRender="firtnameErrors" /> </h:inputText> <br /> <a:outputPanel id="firtnameErrors"> <s:message /> </a:outputPanel> </s:decorate></div> </div> <div class="entry"> <div class="label"><h:outputLabel for="middlename">Middle Name:</h:outputLabel></div> <div class="input"><s:decorate> <h:inputText id="middlename" value="#{user.middlename}" required="false"> <a:support event="onblur" reRender="middlenameErrors" /> </h:inputText> <br /> <a:outputPanel id="middlenameErrors"> <s:message /> </a:outputPanel> </s:decorate></div> </div> <div class="entry"> <div class="label"><h:outputLabel for="lastname">Last Name:</h:outputLabel></div> <div class="input"><s:decorate> <h:inputText id="lastname" value="#{user.lastname}" required="true"> <a:support event="onblur" reRender="lastnameErrors" /> </h:inputText> <br /> <a:outputPanel id="lastnameErrors"> <s:message /> </a:outputPanel> </s:decorate></div> </div> <div class="entry"> <div class="label"><h:outputLabel for="login">Login:</h:outputLabel></div> <div class="input"><s:decorate> <h:inputSecret id="login" value="#{user.login}" required="true" /> <br /> <h:message for="login" /> </s:decorate></div> </div> <div class="entry"> <div class="label"><h:outputLabel for="password">Password:</h:outputLabel></div> <div class="input"><s:decorate> <h:inputSecret id="password" value="#{user.password}" required="true" /> <br /> <h:message for="password" /> </s:decorate></div> </div> <div class="entry"> <div class="label"><h:outputLabel for="verify">Verify Password:</h:outputLabel></div> <div class="input"><s:decorate> <h:inputSecret id="verify" value="#{register.verify}" required="true" /> <br /> <h:message for="verify" /> </s:decorate></div> </div> </s:validateAll> <div class="entry errors"><h:messages globalOnly="true" /></div> <div class="entry"> <div class="label"> </div> <div class="input"><h:commandButton id="register" value="Register" action="#{register.register}" /> <s:button id="cancel" value="Cancel" view="/home.xhtml" /></div> </div> </fieldset> </h:form>
Everything should be k but when I access the page I've this error:
java.lang.IllegalArgumentException: Cannot convert com.agaetis.common.data.business.model.converter.TitleViewConverter@160e129 of type class java.lang.String to interface javax.faces.convert.Converter at com.sun.el.lang.ELSupport.coerceToType(ELSupport.java:367) at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:194) at com.sun.facelets.el.TagValueExpression.getValue(TagValueExpression.java:71) at com.sun.facelets.el.LegacyValueBinding.getValue(LegacyValueBinding.java:56) at javax.faces.component.UIOutput.getConverter(UIOutput.java:65) at org.apache.myfaces.shared_impl.renderkit._SharedRendererUtils.findUIOutputConverter(_SharedRendererUtils.java:48) at org.apache.myfaces.shared_impl.renderkit.RendererUtils.findUIOutputConverter(RendererUtils.java:316) at org.apache.myfaces.shared_impl.renderkit.html.HtmlRendererUtils.findUIOutputConverterFailSafe(HtmlRendererUtils.java:352) at org.apache.myfaces.shared_impl.renderkit.html.HtmlRendererUtils.internalRenderSelect(HtmlRendererUtils.java:280) at org.apache.myfaces.shared_impl.renderkit.html.HtmlRendererUtils.renderMenu(HtmlRendererUtils.java:252) at org.apache.myfaces.shared_impl.renderkit.html.HtmlMenuRendererBase.encodeEnd(HtmlMenuRendererBase.java:54) at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:536) at org.jboss.seam.ui.JSF.renderChild(JSF.java:179) at org.jboss.seam.ui.JSF.renderChildren(JSF.java:162) at org.jboss.seam.ui.UIDecorate.encodeChildren(UIDecorate.java:162) at com.sun.facelets.tag.jsf.ComponentSupport.encodeRecursive(ComponentSupport.java:244) at com.sun.facelets.tag.jsf.ComponentSupport.encodeRecursive(ComponentSupport.java:249) at com.sun.facelets.tag.jsf.ComponentSupport.encodeRecursive(ComponentSupport.java:249) at com.sun.facelets.tag.jsf.ComponentSupport.encodeRecursive(ComponentSupport.java:249) at com.sun.facelets.FaceletViewHandler.renderView(FaceletViewHandler.java:573) at org.ajax4jsf.framework.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:101) at org.ajax4jsf.framework.ajax.AjaxViewHandler.renderView(AjaxViewHandler.java:222) at org.apache.myfaces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:384) at javax.faces.webapp.FacesServlet.service(FacesServlet.java:138) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173) at org.jboss.seam.servlet.SeamExceptionFilter.doFilter(SeamExceptionFilter.java:46) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173) at org.jboss.seam.servlet.SeamRedirectFilter.doFilter(SeamRedirectFilter.java:32) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173) at org.ajax4jsf.framework.ajax.xmlfilter.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:75) at org.ajax4jsf.framework.ajax.xmlfilter.BaseFilter.doFilter(BaseFilter.java:213) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173) at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178) at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:175) at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:74) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105) at org.jboss.web.tomcat.tc5.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:156) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869) at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664) at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527) at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112) at java.lang.Thread.run(Unknown Source)
Thanks for your help
Mickael