0 Replies Latest reply on Nov 24, 2006 3:27 AM by msx

    Page scope of the Seam component

    msx

      Hi all,

      Can anyone, please, explain me the following new-made registration example:

      User.java

      //$Id: User.java,v 1.9 2005/09/10 19:08:01 gavin Exp $
      package org.jboss.seam.example.registration;
      
      import static org.jboss.seam.ScopeType.SESSION;
      
      import java.io.Serializable;
      
      import javax.persistence.Entity;
      import javax.persistence.Id;
      import javax.persistence.Table;
      
      import org.hibernate.validator.Length;
      import org.hibernate.validator.NotNull;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.annotations.Scope;
      
      @Entity
      @Name("user")
      @Scope(SESSION)
      @Table(name = "users")
      public class User implements Serializable {
       private static final long serialVersionUID = 1881413500711441951L;
      
       private String username;
       private String password;
       private String name;
      
       public User(String name, String password, String username) {
       this.name = name;
       this.password = password;
       this.username = username;
       }
      
       public User() {
       }
      
       @NotNull
       public String getName() {
       return name;
       }
      
       public void setName(String name) {
       this.name = name;
       }
      
       @NotNull
       @Length(min = 5, max = 15)
       public String getPassword() {
       return password;
       }
      
       public void setPassword(String password) {
       this.password = password;
       }
      
       @Id
       @NotNull
       @Length(min = 5, max = 15)
       public String getUsername() {
       return username;
       }
      
       public void setUsername(String username) {
       this.username = username;
       }
      
       public String toString() {
       return "User(" + username + ")";
       }
      }
      


      Message.java
      package org.jboss.seam.example.registration;
      
      import static org.jboss.seam.ScopeType.PAGE;
      
      import java.io.Serializable;
      
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.annotations.Scope;
      
      @Name("customMessage")
      @Scope(PAGE)
      public class Message implements Serializable {
      
       private static final long serialVersionUID = -2763236998519280798L;
      
       private static final String DEFAULT_MESSAGE = "Hello World";
      
       private String message = DEFAULT_MESSAGE;
       private String description = DEFAULT_MESSAGE;
      
       public Message() {
       }
      
       public Message(String message, String description) {
       this.message = message;
       this.description = description;
       }
      
       public String getDescription() {
       return description;
       }
      
       public void setDescription(String description) {
       this.description = description;
       }
      
       public String getMessage() {
       return message;
       }
      
       public void setMessage(String message) {
       this.message = message;
       }
      
      }
      


      Register.java
      //$Id: Register.java,v 1.2 2005/09/10 18:29:46 gavin Exp $
      package org.jboss.seam.example.registration;
      
      import javax.ejb.Local;
      
      @Local
      public interface Register {
       String register();
      }


      //$Id: RegisterAction.java,v 1.11 2006/06/11 19:54:33 gavin Exp $
      package org.jboss.seam.example.registration;
      
      import java.util.List;
      
      import javax.ejb.Stateless;
      import javax.persistence.EntityManager;
      import javax.persistence.PersistenceContext;
      
      import org.hibernate.validator.Valid;
      import org.jboss.seam.annotations.In;
      import org.jboss.seam.annotations.Logger;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.annotations.Out;
      import org.jboss.seam.contexts.Contexts;
      import org.jboss.seam.core.FacesMessages;
      import org.jboss.seam.log.Log;
      
      @Stateless
      @Name("register")
      public class RegisterAction implements Register {
      
       @In
       @Valid
       private User user;
       
       @In(value = "customMessage", create = true) @Out
       private Message myMessage;
      
       @PersistenceContext
       private EntityManager em;
      
       @Logger
       private Log log;
      
       public String register() {
       List existing = em.createQuery("select username from User where username=:username")
       .setParameter("username", user.getUsername()).getResultList();
       
       myMessage.setMessage("New message");
      
       log.info(myMessage == Contexts.getPageContext().get("customMessage"));
      
       if (existing.size() == 0) {
       em.persist(user);
       log.info("Registered new user #{user.username}");
       return null; //"/registered.jsp";
       } else {
       FacesMessages.instance().add("User #{user.username} already exists");
       return null;
       }
       }
      
      }
      


      register.jsp
      <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
      <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
      <%@ taglib uri="http://jboss.com/products/seam/taglib" prefix="s" %>
      
      <html>
       <head>
       <title>Register New User</title>
       </head>
       <body>
       <f:view>
       <h:form>
       <table border="0">
       <s:validateAll>
       <tr>
       <td>Username</td>
       <td><h:inputText value="#{user.username}" required="true"/></td>
       </tr>
       <tr>
       <td>Real Name</td>
       <td><h:inputText value="#{user.name}" required="true"/></td>
       </tr>
       <tr>
       <td>Password</td>
       <td><h:inputSecret value="#{user.password}" required="true"/></td>
       </tr>
       </s:validateAll>
       </table>
       <h:messages/>
       <h:commandButton type="submit" value="Register" action="#{register.register}"/>
       </h:form>
       
       Custom message: <b><h:outputText value="#{customMessage.message}"/></b><br/>
       My message: <b><h:outputText value="#{myMessage.message}"/></b><br/>
      
       </f:view>
       </body>
      </html>
      


      The question is about RegisterAction and register.jsp. Why are customMessage and myMessage different on the register.jsp, but in the RegisterAction the are the same?
      If I set any other scope (except ScopeType.PAGE) of the Message component then customMessage and myMessage are the same.