3 Replies Latest reply on Dec 14, 2009 12:17 AM by Tim Evers

    inputText and convertEntity

    Malcolm Green Newbie

      Is it possible to have a <s:convertEntity/> in association with an <h:inputText> like this


      <h:inputText id="personID" value="#{person}" required="true">
        <s:convertEntity/>
      </h:inputText>



      So instead of the primary key id being selected via a dropdown box, it is entered directly?


      Thanks for your help.


      Malcolm

        • 2. Re: inputText and convertEntity
          Ignacio Larrañaga Newbie

          Click HELP for text formatting instructions. Then edit this text and check the preview.


          I try something similar but gets me a format exception because it tries to convert the toString representation I think.


          java.lang.NumberFormatException: For input string: "Nokia E50"
                  at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
                  at java.lang.Integer.parseInt(Integer.java:449)
                  at java.lang.Integer.<init>(Integer.java:660)
                  at org.jboss.seam.ui.EntityIdentifierStore.get(EntityIdentifierStore.java:46)
                  at sun.reflect.GeneratedMethodAccessor723.invoke(Unknown Source)
                  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                  at java.lang.reflect.Method.invoke(Method.java:597)
                  at org.jboss.seam.util.Reflections.invoke(Reflections.java:22)
                  at org.jboss.seam.intercept.RootInvocationContext.proceed(RootInvocationContext.java:32)
                  at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:56)
                  at org.jboss.seam.transaction.RollbackInterceptor.aroundInvoke(RollbackInterceptor.java:28)
                  at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
                  at org.jboss.seam.core.MethodContextInterceptor.aroundInvoke(MethodContextInterceptor.java:44)
                  at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
                  at org.jboss.seam.core.SynchronizationInterceptor.aroundInvoke(SynchronizationInterceptor.java:32)
                  at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
                  at org.jboss.seam.intercept.RootInterceptor.invoke(RootInterceptor.java:107)
                  at org.jboss.seam.intercept.JavaBeanInterceptor.interceptInvocation(JavaBeanInterceptor.java:185)
                  at org.jboss.seam.intercept.JavaBeanInterceptor.invoke(JavaBeanInterceptor.java:103)
                  at org.jboss.seam.ui.EntityIdentifierStore_$$_javassist_seam_6.get(EntityIdentifierStore_$$_javassist_seam_6.java)
                  at org.jboss.seam.ui.AbstractEntityLoader.get(AbstractEntityLoader.java:27)
                  at sun.reflect.GeneratedMethodAccessor722.invoke(Unknown Source)
                  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                  at java.lang.reflect.Method.invoke(Method.java:597)
                  at org.jboss.seam.util.Reflections.invoke(Reflections.java:22)
                  at org.jboss.seam.intercept.RootInvocationContext.proceed(RootInvocationContext.java:32)
                  at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:56)
                  at org.jboss.seam.transaction.RollbackInterceptor.aroundInvoke(RollbackInterceptor.java:28)
                  at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
                  at org.jboss.seam.transaction.TransactionInterceptor$1.work(TransactionInterceptor.java:97)
                  at org.jboss.seam.util.Work.workInTransaction(Work.java:47)
                  at org.jboss.seam.transaction.TransactionInterceptor.aroundInvoke(TransactionInterceptor.java:91)
                  at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
                  at org.jboss.seam.core.MethodContextInterceptor.aroundInvoke(MethodContextInterceptor.java:44)
                  at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
                  at org.jboss.seam.intercept.RootInterceptor.invoke(RootInterceptor.java:107)
                  at org.jboss.seam.intercept.JavaBeanInterceptor.interceptInvocation(JavaBeanInterceptor.java:185)
                  at org.jboss.seam.intercept.JavaBeanInterceptor.invoke(JavaBeanInterceptor.java:103)
                  at org.jboss.seam.ui.JpaEntityLoader_$$_javassist_seam_5.get(JpaEntityLoader_$$_javassist_seam_5.java)
                  at org.jboss.seam.ui.EntityConverter.getAsObject(EntityConverter.java:76)
                  at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getConvertedValue(HtmlBasicInputRenderer.java:167)
                  at javax.faces.component.UIInput.getConvertedValue(UIInput.java:942)
                  at javax.faces.component.UIInput.validate(UIInput.java:868)
                  at javax.faces.component.UIInput.executeValidate(UIInput.java:1072)
                  at javax.faces.component.UIInput.processValidators(UIInput.java:672)
                  at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1058)
                  at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1058)
                  at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1058)
                  at org.richfaces.component.UISwitchablePanel.processValidators(UISwitchablePanel.java:225)
                  at javax.faces.component.UIForm.processValidators(UIForm.java:235)
                  at org.ajax4jsf.component.AjaxViewRoot$3.invokeContextCallback(AjaxViewRoot.java:439)
                  at org.ajax4jsf.component.AjaxViewRoot.processPhase(AjaxViewRoot.java:238)
                  at org.ajax4jsf.component.AjaxViewRoot.processValidators(AjaxViewRoot.java:455)
                  at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:76)
                  at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:100)
                  at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
                  at javax.faces.webapp.FacesServlet.service(FacesServlet.java:265)
                  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
                  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                  at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:83)
                  at org.jboss.seam.web.IdentityFilter.doFilter(IdentityFilter.java:40)
                  at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                  at org.jboss.seam.web.MultipartFilter.doFilter(MultipartFilter.java:90)
                  at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                  at org.jboss.seam.web.ExceptionFilter.doFilter(ExceptionFilter.java:64)
                  at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                  at org.jboss.seam.web.RedirectFilter.doFilter(RedirectFilter.java:45)
                  at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                  at org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:178)
                  at org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:290)
                  at org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:368)
                  at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:495)
                  at org.jboss.seam.web.Ajax4jsfFilter.doFilter(Ajax4jsfFilter.java:56)
                  at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                  at org.jboss.seam.web.LoggingFilter.doFilter(LoggingFilter.java:60)
                  at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                  at org.jboss.seam.web.HotDeployFilter.doFilter(HotDeployFilter.java:53)
                  at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                  at org.jboss.seam.servlet.SeamFilter.doFilter(SeamFilter.java:158)
                  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
                  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                  at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
                  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
                  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235)
                  at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
                  at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190)
                  at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:433)
                  at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)
                  at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
                  at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
                  at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
                  at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
                  at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
                  at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
                  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
                  at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829)
                  at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:598)
                  at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
                  at java.lang.Thread.run(Thread.java:619)



          • 3. Re: inputText and convertEntity
            Tim Evers Master

            Writing your own converter is very simple and will probably be the only way to achieve what you want here.


            I'm pretty sure the Seam doco has a reasonable example. If not just look at the source code for any other converter and copy it.


            Here's a really simple converter (just incase you don't find an example)


            @Name(StringConverter.SEAM_ID)
            @org.jboss.seam.annotations.faces.Converter(id = "GekkoStringConverter", forClass=String.class)
            public class StringConverter implements Converter {
                
                public static final String SEAM_ID = "StringConverter";
                
                public static final String STRING_ID =
                     StringConverter.class.getName() + ".STRING";
            
                @Override
                public Object getAsObject(FacesContext context,
                    UIComponent component,
                    String value)
                {
                    if (StringUtils.isBlank(value)) {
                        return null;
                    }
                    
                    return value;
                }
            
                @Override
                public String getAsString(FacesContext context,
                    UIComponent component,
                    Object value)
                {
                    if (!(value instanceof String)) {
                        throw new ConverterException(MessageFactory.getMessage(
                            context, STRING_ID,
                                value, MessageFactory.getLabel(context, component)));
                    }
                    
                    if (StringUtils.isBlank((String)value)) {
                        return "";
                    }
                    
                    return (String)value;
                }
            
            }
            



            All you need to do is implement the getAsString and getAsObject to work with whatever input/output you want.


            So my guess is you'll have something like this


            public String getAsString(FacesContext context,
                    UIComponent component,
                    Object value)
            {
                return value.getName();
            }
            
            public Object getAsObject(FacesContext context,
                    UIComponent component,
                    String value)
            {
                return queryThingByName(value)
            }
            



            You just need to write the queryThingByName method.