1 Reply Latest reply on Nov 10, 2008 9:54 PM by Thai Dang Vu

    Possible to use a session bean seam component to host a valueChangeListener method?

    Thai Dang Vu Apprentice

      I modify a little bit the Message example of Seam reference. This is the messages.xhtml:


      <h:form>
          <h:outputText value="No messages to display" rendered="#{messages.rowCount == 0}"/>
          <h:dataTable value="#{messages}" rendered="#{messages.rowCount > 0}" var="message">
              <h:column>
                  <f:facet name="header"><h:outputText value="Read" /></f:facet>
                  <h:selectBooleanCheckbox value="#{message.read}" valueChangeListener="#{messageManager.markMessage}"/>
              </h:column>
              <h:column>
                  <f:facet name="header"><h:outputText value="Title" /></f:facet>
                  <h:commandLink value="#{message.title}" action="#{messageManager.select}"/>
              </h:column>
              <h:column>
                  <f:facet name="header"><h:outputText value="Date/Time" /></f:facet>
                  <h:outputText value="#{message.datetime}">
                      <f:convertDateTime type="both" dateStyle="medium" timeStyle="short"/>
                  </h:outputText>
              </h:column>
              <h:column>
                  <h:commandButton value="Delete" action="#{messageManager.delete}"/>
              </h:column>
          </h:dataTable>
          <h3><h:outputText value="#{message.title}" /></h3>
          <h:outputText value="#{message.text}"/>
      </h:form>



      I added a valueChangeListener method to the check boxes. In the MessagesManageBean, I added a method to catch the valueChangeListener event.


      @Stateful
      @Scope(ScopeType.SESSION)
      @Name("messageManager")
      public class MessageManagerBean implements Serializable, MessageManager {
          private final Log log = LogFactory.getLog(getClass());
      
          @DataModel
          private List<Message> messages;
      
          @DataModelSelection
          @Out(required = false)
          private Message message;
      
          @PersistenceContext(type = PersistenceContextType.EXTENDED)
          private EntityManager entityManager;
      
          @Override
          @SuppressWarnings("unchecked")
          @Factory("messages")
          public void findMessage() {
              messages = entityManager.createQuery("select m from Message m order by m.datetime desc")
                      .getResultList();
          }
      
          @Override
          public void select() {
              message.setRead(true);
          }
      
          @Override
          public void delete() {
              messages.remove(message);
              entityManager.remove(message);
              message = null;
          }
      
          @Override
          public void markMessage(ValueChangeEvent event) {
              log.debug(new StringBuilder("Check if Seam intercepts the valueChangeListener:\n")
                      .append("Old value: ").append(defaultIfNull(event.getOldValue(), "null")).append("--> new value")
                      .append(defaultIfNull(event.getNewValue(), "null")).append('\n').append("Message: ")
                      .append(defaultIfNull(message, "no message title because the message is null")));
          }
      
          @Override
          @Remove
          public void destroy() {
          }
      }



      I noticed that the markMessage wasn't called although there's no exception!