0 Replies Latest reply on Nov 10, 2006 10:03 AM by Richard Richter

    Simple list/detail Seam application

    Richard Richter Newbie

      I started to experiment with Seam (1.1) and while I modify some examples, it's all fine. Later I started to create simple application with list page and detail page, where list is based on Booking demo. Now I tried to handle list/detail/save/delete situations with one action bean. So I have three classes - entity, interface and action itself.

      action class:

      @Stateful(name = "ProjectManagerActionEJB")
      @Name("projectManager")
      @Scope(ScopeType.SESSION)
      public class ProjectManagerAction implements ProjectManager {
       @PersistenceContext
       private EntityManager em;
      ...
       @DataModel
       private List<Project> projects;
      
       @DataModelSelection
       @In(required = false)
       @Out(required = false)
       private Project project;
      
       public String find() {
       queryProjects();
       return "/main";
       }
      
       private void queryProjects() {
       String searchPattern = searchString == null ? "%" : '%' + searchString.toLowerCase().replace('*', '%') + '%';
       projects = em.createQuery("select p from Project p where lower(p.name) like :search or lower(p.code) like :search")
       .setParameter("search", searchPattern)
       .getResultList();
       }
      
       public String getSearchString() {
       return searchString;
       }
      
       public void setSearchString(String searchString) {
       this.searchString = searchString;
       }
      
       public String detail() {
       return "/detail";
       }
      
       public String save() {
       System.out.println("save project = " + project);
       Project updatedProject = em.find(Project.class, project.getId());
       em.merge(project);
       System.out.println("updated project = " + updatedProject);
       em.merge(updatedProject);
       queryProjects();
       return "/main";
       }
      
       public String cancel() {
       return "/main";
       }
      
       public String delete() {
       Project deletedProject = em.find(Project.class, project.getId());
       em.remove(deletedProject);
       queryProjects();
       return "/main";
       }
      
       @Destroy @Remove
       public void destroy() {
       }
      }
      


      Detail is fine - proper project is selected. Delete also works, because project with proper id somehow gets into the action. Save is not working, because values from form are not put into that project field (original project without changes is still there).

      detail.xhtml snippet:
       <ui:define name="content">
       <!--<a:outputPanel id="searchResults">-->
       <div class="section">
       <h:outputText value="No Project Found" rendered="#{project != null}"/>
       <h:form>
       <div class="entry">
       <div class="label">
       <h:outputLabel for="code">Code:</h:outputLabel>
       </div>
       <div class="input">
       <h:inputText id="code" value="#{project.code}" required="true"/>
       </div>
       </div>
       <div class="entry">
       <div class="label">
       <h:outputLabel for="name">Name:</h:outputLabel>
       </div>
       <div class="input">
       <h:inputText id="name" value="#{project.name}" required="true"/>
       </div>
       </div>
       <div class="entry">
       <div class="label">
       <h:outputLabel for="description">Description:</h:outputLabel>
       </div>
       <div class="input">
       <h:inputTextarea id="description" value="#{project.description}" required="false"/>
       </div>
       </div>
       <div class="entry">
       <div class="label">
       <s:link value="Delete" action="#{projectManager.delete}" buttonClass="button" linkStyle="button"/>
       </div>
       <div class="input">
       <s:link value="Ok" action="#{projectManager.save}" buttonClass="button" linkStyle="button"/>&#160;
       <s:link value="Cancel" action="#{projectManager.cancel}" buttonClass="button" linkStyle="button"/>
       </div>
       </div>
       </h:form>
       </div>
       </ui:define>
      


      Now... in original example there were used commandButton for "Ok", but I had a problem to navigate back to "/main" with this (s:link worked fine from the start).

      I'm pretty sure that I made some naive errors because I'm still in the process of studying JPA/JFC/Seam tricombination. Anyway, here are my questions:

      - Is it viable to handle list/detail with single action bean?
      - What should I do if I want to obtain project still bound to persistence session in save/delete? (Minor problem, I can still select/change the project.)

      Thanks for hints ;-)

      Richard "Virgo" Richter