4 Replies Latest reply on Dec 18, 2006 4:34 PM by Patrick Madden

    Can I mix Session, Entity and regular Java Beans in Seam app

    Patrick Madden Newbie

      Hello,

      I'm very new to Seam. I Have successfully created sites using JSP and Tomcat however.

      I have seam CR2 deployed with 4.0.5GA using eclipse. I would like to be able to have a standard java beans created that is used as temporary object in my application to initialize some entity beans and then commit to the database. Take for example a very simple UserName and Password combination. My database password takes a byte[] for the password (sqlserver 2000). I can't change that. When I generate my entities the User Entity bean has a

      public void setPassword(byte[] password)
      method. Therefore I want a very simple POJO similar to the Greeter example that is not backed by the DB. I want to create this object as follows:
      package com.foo.web.bean;
      
      import java.io.Serializable;
      
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.annotations.In;
      import org.jboss.seam.annotations.Out;
      
      @Name("Greeter")
      public class GreeterBean implements Serializable
      {
       /**
       *
       */
       private static final long serialVersionUID = 1L;
       private String name = "";
       private String password = "";
      
       @Out
       public String getName()
       {
       return name;
       }
      
       @Out
       public String getPassword()
       {
       return this.password;
       }
      
       @In
       public void setName(String name)
       {
       this.name = name;
       }
      
       @In
       public void setPassword(String password)
       {
       this.password = password;
       }
      }
      

      And then use it in a form as follows:
      <div id="sidebar">
       <fieldset>
       <div>
       <h:outputLabel for="name">Login Name</h:outputLabel>
       <h:inputText id="name" value="#{Greeter.name}" style="width: 175px;" />
       </div>
       <div>
       <h:outputLabel for="password">Password</h:outputLabel>
       <h:inputSecret id="password" value="#{Greeter.password}" style="width: 175px;" />
       </div>
       <div class="errors"><h:messages globalOnly="true"/></div>
       <div class="buttonBox"><h:commandButton action="#{Login.login}" value="Account Login" class="button" /></div>
       <div class="notes"><h:commandLink action="register">Register New User</h:commandLink></div>
       </fieldset>
       </div>


      Inside my stateless LoginAction class, I have an @In Greeter variable and I will use it to extract the bytes from the password to login my user. However, when I do this I get the following error:
      javax.faces.el.EvaluationException: /home.xhtml @25,77 value="#{Greeter.name}": Exception getting value of property name of base of type : com.clooster.web.ejb.session.GreeterBean$$EnhancerByCGLIB$$8988b3bf
       at com.sun.facelets.el.LegacyValueBinding.getValue(LegacyValueBinding.java:60)
       at javax.faces.component.UIOutput.getValue(UIOutput.java:77)
       at org.apache.myfaces.shared_impl.renderkit.RendererUtils.getStringValue(RendererUtils.java:217)
       at org.apache.myfaces.shared_impl.renderkit.html.HtmlTextRendererBase.renderInput(HtmlTextRendererBase.java:135)
       at org.apache.myfaces.shared_impl.renderkit.html.HtmlTextRendererBase.encodeEnd(HtmlTextRendererBase.java:53)
       at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:536)
       at com.sun.facelets.tag.jsf.ComponentSupport.encodeRecursive(ComponentSupport.java:242)
       at com.sun.facelets.tag.jsf.ComponentSupport.encodeRecursive(ComponentSupport.java:239)
       at com.sun.facelets.tag.jsf.ComponentSupport.encodeRecursive(ComponentSupport.java:239)
       at com.sun.facelets.FaceletViewHandler.renderView(FaceletViewHandler.java:580)
       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.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.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.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(Thread.java:595)
      Caused by: javax.faces.el.EvaluationException: Bean: com.clooster.web.ejb.session.GreeterBean$$EnhancerByCGLIB$$8988b3bf, property: name
       at org.apache.myfaces.el.PropertyResolverImpl.getProperty(PropertyResolverImpl.java:442)
       at org.apache.myfaces.el.PropertyResolverImpl.getValue(PropertyResolverImpl.java:82)
       at com.sun.facelets.el.LegacyELContext$LegacyELResolver.getValue(LegacyELContext.java:141)
       at com.sun.el.parser.AstValue.getValue(AstValue.java:117)
       at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:192)
       at com.sun.facelets.el.TagValueExpression.getValue(TagValueExpression.java:71)
       at com.sun.facelets.el.LegacyValueBinding.getValue(LegacyValueBinding.java:56)
       ... 36 more
      Caused by: java.lang.reflect.InvocationTargetException
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
       at java.lang.reflect.Method.invoke(Method.java:585)
       at org.apache.myfaces.el.PropertyResolverImpl.getProperty(PropertyResolverImpl.java:438)
       ... 42 more
      Caused by: org.jboss.seam.RequiredException: In attribute requires value for component: Greeter.password
       at org.jboss.seam.Component.getInstanceToInject(Component.java:1878)
       at org.jboss.seam.Component.injectMethods(Component.java:1322)
       at org.jboss.seam.Component.inject(Component.java:1113)
       at org.jboss.seam.interceptors.BijectionInterceptor.bijectTargetComponent(BijectionInterceptor.java:48)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
       at java.lang.reflect.Method.invoke(Method.java:585)
       at org.jboss.seam.util.Reflections.invoke(Reflections.java:18)
       at org.jboss.seam.intercept.Interceptor.aroundInvoke(Interceptor.java:169)
       at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:64)
       at org.jboss.seam.interceptors.ConversationInterceptor.endOrBeginLongRunningConversation(ConversationInterceptor.java:51)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
       at java.lang.reflect.Method.invoke(Method.java:585)
       at org.jboss.seam.util.Reflections.invoke(Reflections.java:18)
       at org.jboss.seam.intercept.Interceptor.aroundInvoke(Interceptor.java:169)
       at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:64)
       at org.jboss.seam.interceptors.ExceptionInterceptor.handleExceptions(ExceptionInterceptor.java:38)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
       at java.lang.reflect.Method.invoke(Method.java:585)
       at org.jboss.seam.util.Reflections.invoke(Reflections.java:18)
       at org.jboss.seam.intercept.Interceptor.aroundInvoke(Interceptor.java:169)
       at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:64)
       at org.jboss.seam.intercept.RootInterceptor.createSeamInvocationContext(RootInterceptor.java:144)
       at org.jboss.seam.intercept.RootInterceptor.invokeInContexts(RootInterceptor.java:129)
       at org.jboss.seam.intercept.RootInterceptor.invoke(RootInterceptor.java:102)
       at org.jboss.seam.intercept.JavaBeanInterceptor.interceptInvocation(JavaBeanInterceptor.java:140)
       at org.jboss.seam.intercept.JavaBeanInterceptor.intercept(JavaBeanInterceptor.java:75)
       at com.clooster.web.ejb.session.GreeterBean$$EnhancerByCGLIB$$8988b3bf.getName(<generated>)
       ... 47 more


      So my question is is this possible - am I doing something wrong? Any help is greatly appreciated.

        • 1. Re: Can I mix Session, Entity and regular Java Beans in Seam
          Gavin King Master

          You don't need any of those @In and @Out annotations.

          • 2. Re: Can I mix Session, Entity and regular Java Beans in Seam
            Patrick Madden Newbie

            Thank you very much - indeed that fixed my POJO problem. Now, I'm down to one last hurdle. Hopefully after this I'll be off and running. I did buy the Rough Cuts book by the way but I haven't seen this in there yet.

            Question is - What is the proper way to inject the GreeterBean into my Stateless LoginAction class. I'll list the code here:

            package com.clooster.web.ejb.session;
            
            import java.util.List;
            
            import javax.ejb.Stateless;
            import javax.faces.application.FacesMessage;
            import javax.faces.context.FacesContext;
            import javax.persistence.EntityManager;
            import javax.persistence.PersistenceContext;
            
            import org.jboss.seam.annotations.In;
            import org.jboss.seam.annotations.Logger;
            import org.jboss.seam.annotations.Name;
            import org.jboss.seam.contexts.Context;
            import org.jboss.seam.core.FacesMessages;
            import org.jboss.seam.log.Log;
            
            import com.clooster.web.ejb.entity.ClUsers;
            
            @Stateless
            @Name("login")
            public class LoginAction implements Login
            {
            
             @Logger
             private Log log;
            
             @In
             FacesMessages facesMessages;
            
             @In
             GreeterBean greeter;
            
             @PersistenceContext
             private EntityManager em;
            
             @In
             private transient Context sessionContext;
            
             @In
             private transient FacesContext facesContext;
            
             // seam-gen method
             public String login()
             {
             log.info("Entered LoginAction login");
             log.info("Greeter is " + greeter);
            
             List<ClUsers> results = em.createQuery(
             "from ClUsers where cloosterid=:cloosterid and password=:password")
             .setParameter("cloosterid", greeter.getName())
             .setParameter("password",
             greeter.getPassword().getBytes())
             .getResultList();
            
             if (results.size() == 0)
             {
             facesContext.addMessage(null, new FacesMessage(
             "Invalid login"));
            
             return "login";
             }
             else
             {
             ClUsers user = results.get(0);
             sessionContext.set("loggedIn", true);
             sessionContext.set("User", user);
            
             log.info("Logged In");
             return "main";
             }
             }
            }
            


            When I remove the @In annotation on the GreeterBean, it is null after submitting the form. When I include the @In annotation I get the following error.
            Exception during INVOKE_APPLICATION(5): org.jboss.seam.RequiredException: In attribute requires value for component: login.greeter
            with the stack trace as follows:
            org.jboss.ejb3.tx.Ejb3TxPolicy.handleInCallerTx(Ejb3TxPolicy.java:93)
            org.jboss.aspects.tx.TxPolicy.invokeInCallerTx(TxPolicy.java:130)
            org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:195)
            org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
            org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:76)
            org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
            org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:62)
            org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
            org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:77)
            org.jboss.ejb3.security.Ejb3AuthenticationInterceptor.invoke(Ejb3AuthenticationInterceptor.java:102)
            org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
            org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:47)
            org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
            org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:106)
            org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
            org.jboss.ejb3.stateless.StatelessContainer.localInvoke(StatelessContainer.java:211)
            org.jboss.ejb3.stateless.StatelessLocalProxy.invoke(StatelessLocalProxy.java:79)
            $Proxy313.login(Unknown Source)
            com.clooster.web.ejb.session.Login$$FastClassByCGLIB$$7b8ae7bc.invoke(<generated>)
            net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)
            org.jboss.seam.intercept.RootInvocationContext.proceed(RootInvocationContext.java:45)
            org.jboss.seam.intercept.ClientSideInterceptor$1.proceed(ClientSideInterceptor.java:69)
            org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:55)
            org.jboss.seam.interceptors.ExceptionInterceptor.handleExceptions(ExceptionInterceptor.java:38)
            sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
            sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
            java.lang.reflect.Method.invoke(Method.java:585)
            org.jboss.seam.util.Reflections.invoke(Reflections.java:18)
            org.jboss.seam.intercept.Interceptor.aroundInvoke(Interceptor.java:169)
            org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:64)
            org.jboss.seam.intercept.RootInterceptor.createSeamInvocationContext(RootInterceptor.java:144)
            org.jboss.seam.intercept.RootInterceptor.invokeInContexts(RootInterceptor.java:129)
            org.jboss.seam.intercept.RootInterceptor.invoke(RootInterceptor.java:102)
            org.jboss.seam.intercept.ClientSideInterceptor.interceptInvocation(ClientSideInterceptor.java:78)
            org.jboss.seam.intercept.ClientSideInterceptor.intercept(ClientSideInterceptor.java:47)
            com.clooster.web.ejb.session.Login$$EnhancerByCGLIB$$ad46ced0.login(<generated>)
            sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
            sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
            java.lang.reflect.Method.invoke(Method.java:585)
            com.sun.el.parser.AstValue.invoke(AstValue.java:151)
            com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:283)
            com.sun.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:68)
            com.sun.facelets.el.LegacyMethodBinding.invoke(LegacyMethodBinding.java:69)
            org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:63)
            javax.faces.component.UICommand.broadcast(UICommand.java:106)
            javax.faces.component.UIViewRoot._broadcastForPhase(UIViewRoot.java:94)
            javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:168)
            org.apache.myfaces.lifecycle.LifecycleImpl.invokeApplication(LifecycleImpl.java:343)
            org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:86)
            javax.faces.webapp.FacesServlet.service(FacesServlet.java:137)
            org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
            org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
            org.jboss.seam.servlet.SeamRedirectFilter.doFilter(SeamRedirectFilter.java:32)
            org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
            org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
            org.jboss.seam.servlet.SeamExceptionFilter.doFilter(SeamExceptionFilter.java:46)
            org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
            org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
            org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
            org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
            org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
            org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
            org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
            org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:175)
            org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:74)
            org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
            org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
            org.jboss.web.tomcat.tc5.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:156)
            org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
            org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
            org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
            org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
            org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
            org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
            java.lang.Thread.run(Thread.java:595)
            


            Once again, the form snippet is as follows:


            Thank you in advance!!! Greatly appreciated.
            <fieldset>
             <div>
             <h:outputLabel for="name">Login Name</h:outputLabel>
             <h:inputText id="name" value="#{Greeter.name}" style="width: 175px;" />
             </div>
             <div>
             <h:outputLabel for="password">Password</h:outputLabel>
             <h:inputSecret id="password" value="#{Greeter.password}" style="width: 175px;" />
             </div>
             <div class="errors"><h:messages globalOnly="true"/></div>
             <div class="buttonBox"><h:commandButton action="#{login.login}" value="Account Login" class="button" /></div>
             <div class="notes"><h:commandLink action="register">Register New User</h:commandLink></div>
             </fieldset>
            


            • 3. Re: Can I mix Session, Entity and regular Java Beans in Seam
              Pete Muir Master

               

              "PatrickMadden" wrote:
              @Name("Greeter")
              public class GreeterBean implements Serializable


              @In GreeterBean greeter;


              Spot the problem? - Seam bijects based on variable name not class, so you've named your variable Greeter but are trying to inject greeter. N.B. by convention geeter is correct, and Greeter is wrong. This also assumes that an instance of greeter already exists in a Seam context. If it doesn't use @In(create=true)

              • 4. Re: Can I mix Session, Entity and regular Java Beans in Seam
                Patrick Madden Newbie

                Thank you both very much for the very fast replies. It works and I can now log into my application correctly.

                Seam is a new paradigm for me and I'm making rookie mistakes. However, thanks to your explanations and the Seam Book, I believe I have what I need to handle the rest. (hopefully anyway). I'll never forget the variable vs class thing again. I GET IT now!!!

                PVM