11 Replies Latest reply on Oct 24, 2006 5:08 AM by Stefan Willems

    simeple create/edit user question

    dudududu Novice

      I'm tring to develop a user registration module with seam (POC)

      I don't want to use JBPM for now.

      I created a jsf page with 3 inputs
      user.username
      user.name
      user.password

      now I would like to access this page on 2 situation : create user/edit user
      1)can I use the same RegisterAction stateless bean for that?

      2) how does the stateless knows if I would like to edit or to create?
      3) how do I retrive the user information to the page on edit situation?

      I read the tutorial and saw only JBPM examples
      I am very new to seam - please describe me as much as you can

      Thanks a lot!

        • 1. Re: simeple create/edit user question
          Pete Muir Master

          This all very much depends on your desgin choices. Look at http://blog.hibernate.org/cgi-bin/blosxom.cgi/Gavin%20King/stages-of-adoption.html which quickly runs over various possible patterns.

          My preferred technique is to use a conversation. I use a seperate backing bean for create and edit (which may extend a shared functions bean if necessary). I use a third bean for searching (stateless at the monent), and then use a datamodel row selection to populate the edit bean.

          For the 'create' - start the conversation on entering the page, end it on pushing the create button. For 'edit' - start the conversation on entering the page, end it on pushing ok, and have an apply button which will flush current changes to the database but not close the page. For both I provide a cancel button which will end the conversation and navigate away but without flushing any changes. If the user navigates away I let the conversation time out (and not flush anything to the database).

          I hope that didn't make you more confused. If you want me to expand on a particular part, ask.

          • 2. Re: simeple create/edit user question
            hdslkf hklsdg Apprentice

            Stateless beans are only useful if you want to invoke one of their methods without doing anything else with them.

            But if you have a state that should be held (e.g. user filled in fields) you need a STATEFUL bean. To distinguish between new and existing entities you can call two different actions - create and edit. There should be corresponding clickable buttons in your application.

            I suggest it like that (pseudo code - in your case replace Entity by User):

            @Stateful
            @Scope(ScopeType.CONVERSATION)
            @Name(...)
            public class WhatEverBean implements WhatEver {
            
             private boolean newOne;
            
             @PersistenceContext(PersistenceContextType.EXTENDED)
             private EntityManager em;
            
             @In(required=false) @Out(required=false)
             private Entity entity;
            
             @Begin
             public String create() {
             newOne = true;
             entity = new Entity();
             return "editPageOutcome";
             }
            
             @Begin
             public String edit() {
             newOne = false;
            
             entity = em.find(Entity.class, theIdFromRequestParameter);
            OR: entity = getFromDataModelSelection...;
            
             return "editorPageOutcome";
             }
            
             @End
             public String save() {
             if (newOne)
             em.persist(entity);
             else
             em.merge(entity);
             return "toPreviousPageOutcome";
             }
            }


            Ok, why do I suggest conversation scope? Cause the bean needs to live as long as the user edits the entity (including redisplays).

            • 3. Re: simeple create/edit user question
              hdslkf hklsdg Apprentice

              Hm... petemuir was a bit quicker than me. But it seems like our two post complement one another - though we're using slightly different approaches ;).

              • 4. Re: simeple create/edit user question
                Pete Muir Master

                As always with design there are a multitude of ways to do the same thing, some of which are vastly different, some of which are very similar. It will be nice when the EJB3/Seam patterns set is a bit more developed!

                I would suggest using a Seam Managed Persistence Context rather than an extended one to prevent any LazyInitiaisationExceptions.

                • 5. Re: simeple create/edit user question
                  Stefan Willems Newbie

                   

                  "petemuir" wrote:
                  ...
                  I use a third bean for searching (stateless at the monent), and then use a datamodel row selection to populate the edit bean.
                  ...

                  Hi Pete!

                  I'm following the same "pattern" as you, but get a problem doing so.
                  I've also a Stateless SB for searching, including a DataModel for the resultlist.Works fine for the first time displaying, but whenI leave the search page and get back to it, I get a LazyInitialisationException. And yes, I'm already using the Seam Managed Persistence.

                  Any suggestions?

                  Thank you,

                  Stefan

                  • 6. Re: simeple create/edit user question
                    Pete Muir Master

                    How do you 'get back to it'? The back button, or a link?

                    • 7. Re: simeple create/edit user question
                      Stefan Willems Newbie

                      I go back through a <h:commandButton action="..."/>, calling a method in my SLSB, which returns an outcome for faces-config.

                      • 8. Re: simeple create/edit user question
                        Pete Muir Master

                        I'm not sure what your exact problem. But I would guess that you are accessing something that isn't in the stateless scope from your search page, and that there is no persistence context available for it. So if you post some code it will be easier to help ;)

                        • 9. Re: simeple create/edit user question
                          Stefan Willems Newbie

                           

                          The SearchBean:
                          
                          @Stateless
                          @Name("mitarbeiteruebersicht")
                          @LoggedIn
                          @Scope(ScopeType.STATELESS)
                          
                          public class MitarbeiterUebersichtBean
                           implements MitarbeiterUebersicht {
                          ...
                          ...
                           @In (required=false)
                           @Out (required=false)
                           private Mitarbeiter aktMitarbeiter;
                          
                           @DataModel
                           private List<Mitarbeiter> mitarbeiterliste;
                           @DataModelSelection
                           Mitarbeiter selectedMitarbeiter;
                          
                           @Factory("mitarbeiterliste")
                           public void zeigeMitarbeiter() {
                          
                           List result = new LinkedList();
                           mitarbeiterliste = new LinkedList<Mitarbeiter>();
                          
                          
                           result = getQuery().getResultList();
                          
                           Iterator mitarbeiterIter = result.iterator();
                           for (;mitarbeiterIter.hasNext();){
                           Mitarbeiter rs = (Mitarbeiter)mitarbeiterIter.next();
                           mitarbeiterliste.add(rs);
                           }
                           }
                          
                          ...
                          ...
                           public String ende(){
                           return "hauptmenu";
                           }
                          
                           @Begin
                           public String neu(){
                           aktMitarbeiter = new Mitarbeiter();
                           return "editMitarbeiter";
                           }
                          
                           @Begin
                           public String suchen(){
                           return "suchenMitarbeiter";
                           }
                          
                           @Begin
                           public String selectMitarbeiter(){
                           aktMitarbeiter= (Mitarbeiter) weiterbildungDatabase.createQuery("SELECT m FROM Mitarbeiter m WHERE id = :id")
                           .setParameter("id", selectedMitarbeiter.getId())
                           .getSingleResult();
                           return "editMitarbeiter";
                           }
                          ...
                          ...
                          }
                          
                          the SearchJSP
                          ...
                          ...
                           <t:dataTable id="mitarbeiter"
                           var="varMitarbeiter" value="#{mitarbeiterliste}"
                           styleClass="listtable"
                           headerClass="listtablehead"
                           rowClasses="oddrow,evenrow"
                           sortColumn="#{mitarbeiteruebersicht.sortColumn}"
                           sortAscending="#{mitarbeiteruebersicht.sortAscending}">
                          
                           <t:column sortable="true" styleClass="colLeft" style="width:25%;">
                           <f:facet name="header">
                           <t:commandSortHeader columnName="personalnummer">
                           <h:outputText value="Personalnummer"/>
                           </t:commandSortHeader>
                           </f:facet>
                           <h:commandLink id="aendern"value="#{varMitarbeiter.personalnummer}"
                           action="#{mitarbeiteruebersicht.selectMitarbeiter}"/>
                           </t:column>
                          ...
                          ...
                           <div class="cmdBox">
                           <h:commandButton id="cmdNeu"
                           style="width:110px;margin:5px;"
                           action="#{mitarbeiteruebersicht.neu}"
                           value="Neu" />
                           </div>
                           <div class="cmdBox">
                           <h:commandButton id="cmdSuchen"
                           style="width:110px;margin:5px;"
                           action="#{mitarbeiteruebersicht.suchen}"
                           value="Suchen" />
                           </div>
                           <div class="cmdBox">
                           <h:commandButton
                           style="width:110px;margin:5px;"
                           id="cmdEnde"
                           action="#{mitarbeiteruebersicht.ende}"
                           value="Zurück" />
                           </div>
                          ...
                          ...
                          The EditBean:
                          
                          @Name("mitarbeiteredit")
                          @Stateful
                          @Conversational(ifNotBegunOutcome="mitarbeiteruebersicht")
                          @LoggedIn
                          
                          public class MitarbeiterEditBean
                           implements MitarbeiterEdit{
                          
                           @In (create=true)
                           EntityManager weiterbildungDatabase;
                          
                           @In @Out
                           private Mitarbeiter aktMitarbeiter;
                          ...
                          ...
                           @End
                           @Remove @Destroy
                           public String ende() {
                           if (aktMitarbeiter.getId() != 0){
                           weiterbildungDatabase.refresh(aktMitarbeiter);
                           }
                           return "mitarbeiterUebersicht";
                           }
                          ...
                          ...
                          }
                          
                          


                          • 10. Re: simeple create/edit user question
                            Pete Muir Master

                            Please post the exception!

                            • 11. Re: simeple create/edit user question
                              Stefan Willems Newbie

                               

                              2006-10-24 11:06:49,582 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/weiterbildung].[Faces Servlet]] Servlet.service() for servlet Faces Servlet threw exception
                              javax.faces.FacesException: Cannot get value for expression '#{varMitarbeiter.kostenstellen.name}'
                               at org.apache.myfaces.context.servlet.ServletExternalContextImpl.dispatch(ServletExternalContextImpl.java:435)
                               at org.apache.myfaces.application.jsp.JspViewHandlerImpl.renderView(JspViewHandlerImpl.java:234)
                               at org.jboss.seam.jsf.SeamViewHandler.renderView(SeamViewHandler.java:59)
                               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.apache.myfaces.webapp.filter.ExtensionsFilter.doFilter(ExtensionsFilter.java:144)
                               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:30)
                               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.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: Cannot get value for expression '#{varMitarbeiter.kostenstellen.name}'
                               at org.apache.myfaces.el.ValueBindingImpl.getValue(ValueBindingImpl.java:402)
                               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.renderkit.html.HtmlLabelRenderer.encodeBegin(HtmlLabelRenderer.java:78)
                               at javax.faces.component.UIComponentBase.encodeBegin(UIComponentBase.java:512)
                               at org.apache.myfaces.shared_tomahawk.renderkit.RendererUtils.renderChild(RendererUtils.java:433)
                               at org.apache.myfaces.shared_tomahawk.renderkit.RendererUtils.renderChildren(RendererUtils.java:419)
                               at org.apache.myfaces.shared_tomahawk.renderkit.RendererUtils.renderChild(RendererUtils.java:440)
                               at org.apache.myfaces.renderkit.html.ext.HtmlTableRenderer.renderColumnBody(HtmlTableRenderer.java:588)
                               at org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlTableRendererBase.encodeColumnChild(HtmlTableRendererBase.java:282)
                               at org.apache.myfaces.renderkit.html.ext.HtmlTableRenderer.encodeColumnChild(HtmlTableRenderer.java:496)
                               at org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlTableRendererBase.encodeInnerHtml(HtmlTableRendererBase.java:258)
                               at org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlTableRendererBase.encodeChildren(HtmlTableRendererBase.java:123)
                               at org.apache.myfaces.renderkit.html.ext.HtmlTableRenderer.encodeChildren(HtmlTableRenderer.java:171)
                               at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:524)
                               at javax.faces.webapp.UIComponentTag.encodeChildren(UIComponentTag.java:480)
                               at javax.faces.webapp.UIComponentTag.doEndTag(UIComponentTag.java:361)
                               at org.apache.myfaces.shared_tomahawk.taglib.UIComponentBodyTagBase.doEndTag(UIComponentBodyTagBase.java:54)
                               at org.apache.jsp.mitarbeiterUebersicht_jsp._jspx_meth_t_dataTable_0(mitarbeiterUebersicht_jsp.java:461)
                               at org.apache.jsp.mitarbeiterUebersicht_jsp._jspx_meth_h_form_1(mitarbeiterUebersicht_jsp.java:329)
                               at org.apache.jsp.mitarbeiterUebersicht_jsp._jspx_meth_f_view_0(mitarbeiterUebersicht_jsp.java:188)
                               at org.apache.jsp.mitarbeiterUebersicht_jsp._jspService(mitarbeiterUebersicht_jsp.java:112)
                               at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
                               at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
                               at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)
                               at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
                               at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
                               at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
                               at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
                               at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
                               at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:672)
                               at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:463)
                               at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:398)
                               at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:301)
                               at org.apache.myfaces.context.servlet.ServletExternalContextImpl.dispatch(ServletExternalContextImpl.java:416)
                               ... 28 more
                              Caused by: javax.faces.el.EvaluationException: Exception getting value of property name of base of type : de.hydro.gv.weiterbildung.entities.Kostenstellen_$$_javassist_51
                               at org.apache.myfaces.el.PropertyResolverImpl.getValue(PropertyResolverImpl.java:96)
                               at org.apache.myfaces.el.ELParserHelper$MyPropertySuffix.evaluate(ELParserHelper.java:532)
                               at org.apache.commons.el.ComplexValue.evaluate(ComplexValue.java:145)
                               at org.apache.myfaces.el.ValueBindingImpl.getValue(ValueBindingImpl.java:383)
                               ... 62 more
                              Caused by: javax.faces.el.EvaluationException: Bean: de.hydro.gv.weiterbildung.entities.Kostenstellen_$$_javassist_51, property: name
                               at org.apache.myfaces.el.PropertyResolverImpl.getProperty(PropertyResolverImpl.java:442)
                               at org.apache.myfaces.el.PropertyResolverImpl.getValue(PropertyResolverImpl.java:82)
                               ... 65 more
                              Caused by: java.lang.reflect.InvocationTargetException
                               at sun.reflect.GeneratedMethodAccessor204.invoke(Unknown Source)
                               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)
                               ... 66 more
                              Caused by: org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed
                               at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:60)
                               at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111)
                               at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:196)
                               at de.hydro.gv.weiterbildung.entities.Kostenstellen_$$_javassist_51.getName(Kostenstellen_$$_javassist_51.java)
                               ... 70 more