5 Replies Latest reply on Sep 12, 2010 5:39 AM by serkan.s.eskici.online.nl

    DataModelSelection value not injected

    kinghoward

      I cannot get selected value injected into field marked by @DataModelSelection. I copied and modified an example from seam tutorials messages example.


      The @DataModelSelection field is called message, and it is always null, even after i clicked on the commandLink link.


      Can someone help me to spot why no value is ever injected into message?


      here are my classes:


      ---------------------------------------------



      import static org.jboss.seam.ScopeType.EVENT;
      import java.io.Serializable;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.annotations.Scope;
      
      @Name("message")
      @Scope(EVENT)
      public class Message implements Serializable
      {
              
              public Message(){
                      this.title = "";
                      this.text = "";
                      this.read = false;
              }
              
              public Message(String title, String text, boolean read){
                      this.title = title;
                      this.text = text;
                      this.read = read;
              }
              
         private String title;
         private String text;
         private boolean read;
      
         public String getTitle() {
            return title;
         }
         public void setTitle(String title) {
            this.title = title;
         }
         
         public String getText() {
            return text;
         }
         public void setText(String text) {
            this.text = text;
         }
         
         public boolean isRead() {
            return read;
         }
         public void setRead(boolean read) {
            this.read = read;
         }   
      }




      ---------------------------------------------



      import java.util.List;
      import javax.ejb.Local;
      
      @Local
      public interface MessageManager
      {
         public void findMessages();
         public void select();
         public void delete();
         public void destroy();
         public Message getMessage();
         public void setMessage(Message message);
         public List<Message> getMessageList();
         public void setMessageList(List<Message> messageList);
      } 




      ---------------------------------------------



      import static org.jboss.seam.ScopeType.SESSION;
      
      import java.io.Serializable;
      import java.util.ArrayList;
      import java.util.List;
      
      import javax.ejb.Remove;
      import javax.ejb.Stateful;
      
      import org.jboss.seam.ScopeType;
      import org.jboss.seam.annotations.Destroy;
      import org.jboss.seam.annotations.Factory;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.annotations.Out;
      import org.jboss.seam.annotations.Scope;
      import org.jboss.seam.annotations.datamodel.DataModel;
      import org.jboss.seam.annotations.datamodel.DataModelSelection;
      
      @Stateful
      @Scope(SESSION)
      @Name("messageManager")
      public class MessageManagerBean implements Serializable, MessageManager
      {
      
         @DataModel(scope=ScopeType.PAGE)
         private List<Message> messageList;
         
         @DataModelSelection
         @Out(required=false)
         private Message message;
         
         @Factory("messageList")
         public void findMessages()
         {
                 System.out.println("in findMessages(), by factory, making messages");
            messageList = new ArrayList<Message>();
            Message first = new Message("First", "This is a great message", true);
            messageList.add(first);
            Message second = new Message("Second", "This is an average message", false);
            messageList.add(second);
         }
         
         public void select()
         {
            if (message!=null){
                message.setRead(true);
                System.out.println("in select(), for message: " + message.getTitle());
            }
            else{
                System.out.println("in select(), NULL message");
            }
         }
         
         public void delete()
         {
            if (message!=null)
            {
                System.out.println("deleting message: " + message.getTitle());
                for(int i=0;i<messageList.size(); i++){
                        if(((Message)messageList.get(i)).getTitle().equalsIgnoreCase(message.getTitle())){
                                messageList.remove(i);
                        }
                }
      
               message=null;
            }
            System.out.println("in delete(), but message is NULL");
         }
         
         
         
         public List<Message> getMessageList() {
              return messageList;
      }
      
      public void setMessageList(List<Message> messageList) {
              this.messageList = messageList;
      }
      
      public Message getMessage() {
              return message;
      }
      
      public void setMessage(Message message) {
              this.message = message;
              System.out.println("set message to: " + message.getTitle());
      }
      
      @Remove @Destroy
         public void destroy() {}
      
      }




      ---------------------------------------------------
      here is my view class, it is called messages.xhtml:





      <!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <ui:composition xmlns="http://www.w3.org/1999/xhtml"
              xmlns:s="http://jboss.com/products/seam/taglib"
              xmlns:ui="http://java.sun.com/jsf/facelets"
              xmlns:f="http://java.sun.com/jsf/core"
              xmlns:h="http://java.sun.com/jsf/html"
              xmlns:rich="http://richfaces.org/rich"
              xmlns:p="http://jboss.com/products/seam/pdf"
              xmlns:a="http://richfaces.org/a4j" template="layout/template.xhtml">
      
              <ui:define name="body">
      
                      <h:form id="messageForm">
      
                              <rich:panel>
                                      <h:outputText
                                              value="No datastore to display: #{messageManager.messageList.size}"
                                              rendered="#{messageManager.messageList.size == 0}" />
                                      <h:outputText
                                              value="Total number of messages: #{messageManager.messageList.size}"
                                              rendered="#{messageManager.messageList.size != 0}" />
                                      <h:dataTable id="messages" var="msg"
                                              value="#{messageManager.messageList}"
                                              rendered="#{messageManager.messageList.size>0}">
                                              <h:column>
                                                      <f:facet name="header">
                                                              <h:outputText value="Read" />
                                                      </f:facet>
                                                      <h:selectBooleanCheckbox id="read" value="#{msg.read}"
                                                              disabled="true" />
                                              </h:column>
                                              <h:column>
                                                      <f:facet name="header">
                                                              <h:outputText value="Title" />
                                                      </f:facet>
                                                      <h:commandLink id="link" value="#{msg.title}"
                                                              action="#{messageManager.select}" />
                                              </h:column>
                                              <h:column>
                                                      <s:button id="delete" value="Delete"
                                                              action="#{messageManager.delete}" />
                                              </h:column>
                                      </h:dataTable>
                                           <h3><h:outputText id="title" value="#{message.title}"/></h3>
                                           <div><h:outputText id="text" value="#{message.text}"/></div>
                              </rich:panel>
                      </h:form>
              </ui:define>
      
      </ui:composition>
      





      ---------------------------------------------

        • 1. Re: DataModelSelection value not injected
          lvdberg

          Hi,


          The values of the list and the selected value must be stored somewhere and normally that's a conversation context. The s:links propoagate the conversation id, so it should work if you change the meessagemanager to a conversation scope AND also start a conversation (the easies way is to add a tag in the page-descriptor.


          Leo

          • 2. Re: DataModelSelection value not injected
            kinghoward

            thanks for the reply, Leo!


            after changing messagemanager's scope to CONVERSATION, I still cannot get value injected to message, the field marked with @DataModelSelection.


            Maybe I still don't know how to start a conversation, what do you mean by add a tag in the page-descriptor? can you please give me an example?


            Also, i'm observing the method marked with  @Factory (i.e. findMessages() in MessageManagerBean or messageMangager) is called over and over, rather than just once when messageList is null. Is that because the system is starting a new conversation and messageList is set to null everytime an event or page reload happens?


            • 3. Re: DataModelSelection value not injected
              lvdberg

              Hi Howard,


              you can start a conversation automatically when you enter the page. You need to add something like this in your pages.xml




              <page view-id="messages.xhtml"  >
                   <begin-conversation join="true" />
              </page>
              
              



              Another way is adding an Annotation to an Action which you call when you enter. Something like




              @Begin(join=true)
              public void doSomething(){
              .....
              }



              This is basic Seamology!



              Leo


              • 4. Re: DataModelSelection value not injected
                kinghoward

                thanks again for the reply! I'm a beginner to seam...


                i looked into it, but cannot make the conversation to last, see details in this new post:


                My Link


                any ideas?

                • 5. Re: DataModelSelection value not injected
                  serkan.s.eskici.online.nl

                  Howard,


                  Change this:


                  <h:dataTable id="messages" var="msg" value="#{messageManager.messageList}" ...>
                  



                  into this:


                  <h:dataTable id="messages" var="msg" value="#{messageList}" ...>