1 2 Previous Next 17 Replies Latest reply on Jul 9, 2006 11:58 PM by gavin.king

    'edit' object through datatable page

    trouby

      Hey,

      I have a simple question regarding object editing,

      I have a page containing a 'datatable' tag which fetches several objects via an EJB3,

      I want two simple actions: delete per row/ edit per row,

      by adding tag per row:

      '<h:commandButton value="delete" action="#{myEjbBean.deleteObject}" />'
      

      as it seems, the object gets injected into the bean fine (this is only working with h:commandButton tag, injection does not work when I use 's:link' tag.

      so this process seems to be straight forward, but what about editing an object?

      editing required two steps:
      1) showing a form with the current object data (edit)
      2) submitting the new edited data and persist it (update)

      if the selected row object is already injected by seam, can I bypass the 'edit' action and re-direct to another page and have the selected object already accessible there? WITHOUT having an actionListener on the way? if so? how do I do that?

      I tried the following:
      <s:link view="/some_new_page.xhtml" value="edit">
      <f:param name="id" value="#{myvar.id}"/>
      </s:link>
      


      but appearntly 'some_new_page.xhtml' does not have 'myvar' accessible, so I assume 's:link' tag does not inject the object...


      Note (as I mentioned), when I Tried to use 's:link' tag with delete, the object was not injected as well,


      Am I missing something? or I must use an actionListener method to inject the object for the 'edit' process and then use another method for the 'update' as well?

      Thanks a lot,
      Asaf

        • 1. Re: 'edit' object through datatable page
          gavin.king

          Way too vague:

          What does "injected" mean? @DataModelSelection? Something else?

          What scope is myEjbBean? What scope is the object that backs the edit page? etc...

          Please show your actual code.

          • 2. Re: 'edit' object through datatable page
            trouby

            Eh, I'm sorry, here are some more details:

            "injected" means 'DataModelSelection'

            my 'DataModelSelection' object code (the intersting part)

            @Scope(ScopeType.EVENT)
            @Name("role")
            
            @Table(name="ROLE")
            @Entity
            public class Role implements Serializable {
             private long roleId;
             private String name;
             private String description;
            
             public void setRoleId(long roleId) {
             this.roleId = roleId;
             }
            
             @Id
             @GeneratedValue(strategy = GenerationType.IDENTITY)
             @Column(name="ROLE_ID")
             public long getRoleId() {
             return roleId;
             }
            
             public void setName(String name) {
             this.name = name;
             }
            
             @Length(min=3, max=40) @NotNull //seam
             @Column(name="NAME", nullable=false)
             public String getName() {
             return name;
             }
            
             ....
            
            }
            
            



            My ejb3 used for actions:

            @Stateless()
            @Name("roleManager")
            @Interceptors( {org.jboss.seam.ejb.SeamInterceptor.class} )
            public class RoleBean implements RoleManagerLocal,RoleManagerRemote {
            
             //For insertions
             @In(create=true) @Valid
             Role role;
            
             @DataModel
             private List<Role> roleList;
             @DataModelSelection
             @Out(required=false)
             private Role selectedRole;
            
            /**
             * Injected entity manager
             */
             //@PersistenceContext
             @PersistenceContext
             public EntityManager em;
            
            ....
            
            


            here's the 'listRoles.xhtml' with the datatable of the DataModel (roleList) with the actions edit/delete I explained in the first post,


            <t:dataTable id="rolesDataTableList"
             styleClass="scrollerTable"
             headerClass="standardTable_Header"
             footerClass="standardTable_Header"
             rowClasses="standardTable_Row1,standardTable_Row2"
             columnClasses="standardTable_Column,standardTable_ColumnCentered,standardTable_Column"
             var="role"
             value="#{roleList}"
             preserveDataModel="false"
             rows="10"
             >
             <h:column>
             <f:facet name="header">
             <h:outputText value="#{msgs.id}" />
             </f:facet>
             <h:outputText value="#{role.roleId}" />
             </h:column>
            
             <h:column>
             <f:facet name="header">
             <h:outputText value="#{msgs.name}" />
             </f:facet>
             <h:outputText value="#{role.name}" />
             </h:column>
            
             <h:column>
             <f:facet name="header">
             <h:outputText value="#{msgs.description}" />
             </f:facet>
             <h:outputText value="#{role.description}" />
             </h:column>
             <h:column>
             <f:facet name="header">
             <h:outputText value="#{msgs.actions}" />
             </f:facet>
             <f:facet name="header"><h:outputText value="#{msgs.actions}" /></f:facet>
             <h:outputLink value="roleEdit.seam">Edit
             <f:param name="roleId" value="#{role.roleId}"/>
             </h:outputLink>
             </h:column>
             </t:dataTable>
            




            clicking on 'edit' link redirects to the page but 'role' seems to be empty.

            Thanks a lot.

            • 3. Re: 'edit' object through datatable page
              gavin.king

              You still didn't show the code for roleEdit.

              • 4. Re: 'edit' object through datatable page
                trouby

                Hey again,

                It is actually almost empty, I just added one input tag to see if the selected 'role' from last page is accessible...

                <div class="entry">
                 <div class="label"><h:outputLabel for="description">#{msgs.description}</h:outputLabel></div>
                 <div class="input">
                 <h:inputText id="description" value="#{role.description}" required="true"/><br/>
                 <span class="errors"><h:message for="description" /></span>
                 </div>
                </div>
                


                in order to see if it's injected into role entity...


                the plan was add an action for the whole form which merge the updated entity...

                Thanks.

                • 5. Re: 'edit' object through datatable page
                  gavin.king

                  Actually, your roleManager class looks broken, but its hard to say for sure.

                  It's a stateless component, hence the @DataModel scope defaults to EVENT, which means that the @DataModelSelection will always be null.

                  I have no idea what "@In(create=true) @Valid Role role" is for, but since you decided not to post all the code for that class, I can't tell if its right or wrong.

                  As a first step, you need to use @DataModel(scope=SESSION) to ensure that the @DataModelSelection will work.

                  In future, if I ask you to post code, please post _all_ of it, TIA.

                  • 6. Re: 'edit' object through datatable page
                    gavin.king

                     

                    "trouby" wrote:
                    It is actually almost empty, I just added one input tag to see if the selected 'role' from last page is accessible...


                    The Java code.

                    • 7. Re: 'edit' object through datatable page
                      trouby

                      Hey again,

                      '@In(create=true) @Valid Role role' is irrelevant for this,
                      I have another page named 'roleAdd.xhtml' which its data is submitted to this entity, I hope it is valid...

                      I did post the code for that class... see above, it is the class with the @entity / @name("role"); annotations.

                      regarding the stateless context of 'roleManager', it must be stateful in order to keep the state of the 'DataModelSelection' entity?

                      I do not need the entity to be accessible for the whole session, just for the next page for editing...


                      Thanks a lot :)

                      • 8. Re: 'edit' object through datatable page
                        gavin.king

                        Oh OK, I see what you've tried to do.

                        No, that will definitely not work. You never outject the value of the @DataModelSelection into the context variable named "role".

                        Seriously, you would be much better off taking a look at how the booking demo and the blog demo handle navigation from a list. They each use a different approach (booking demo uses @DataModelSelection, blog demo uses @RequestParameter). There are several variations upon both approaches.

                        • 9. Re: 'edit' object through datatable page
                          trouby

                          yeah,
                          I see the problem...

                          Well, I looked at both examples,
                          blog example has two actions, delete/select, both use an action at the backend and make a usage of the 'DataModelSelection' object,
                          I couldnt find any example that links from a datatable selected row to another page without passing through an action... :(

                          Thanks a lot.

                          • 10. Re: 'edit' object through datatable page
                            trouby

                            yeah,
                            I see the problem...

                            Well, I looked at both examples,
                            I couldnt find any example that links from a datatable selected row to another page without passing through an action... :-/

                            Thanks a lot.

                            • 11. Re: 'edit' object through datatable page
                              gavin.king

                              It is *totally* possible to do this in pull-MVC style, without using an action.

                              Perhaps the easiest way for you us to use an @Factory method.

                              • 12. Re: 'edit' object through datatable page
                                gavin.king

                                ie. an @Factory method that outjects the Role, after either getting its id from a request parameter, or calling back to the other component to get the @DataModelSelection.

                                • 13. Re: 'edit' object through datatable page
                                  trouby

                                  Cool,

                                  that what I'm looking for, it's really a waste of an action / navigation-rule for raising an 'edit' form for the selected @DataObjectSelection entity,

                                  Are there any examples for this? it'll be nice to see a simple CRUD app,


                                  I appriciate your help.

                                  • 14. Re: 'edit' object through datatable page
                                    trouby

                                    Hey,

                                    back to this, if someone is interested, I found two options (as directed by Gavin)

                                    1) using factory method for outjecting the selected entity, but since I already have too much factory methods I passed away
                                    2) outjecting the selected object by handling the ID of the selected object as a parameter:

                                    1) in the datatable list, add an '<f:param> tag, specifying the ID of the entity"
                                    2) add under META-INF/pages.xml file the view-id of the page related for editing/handling the selected entity with an action on a bean to outject the selected entity by using '@RequestParameter("paramName')

                                    The 'blog' sample works with an MVC style and makes a usage of pages.xml and parameters, so you can take a look at it.


                                    Cheers.

                                    1 2 Previous Next