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

    Page scope of the Seam component

    S Z Newbie

      Hi all,

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


      //$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;
      @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() {
       public String getName() {
       return name;
       public void setName(String name) {
       this.name = name;
       @Length(min = 5, max = 15)
       public String getPassword() {
       return password;
       public void setPassword(String password) {
       this.password = password;
       @Length(min = 5, max = 15)
       public String getUsername() {
       return username;
       public void setUsername(String username) {
       this.username = username;
       public String toString() {
       return "User(" + username + ")";

      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;
      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;

      //$Id: Register.java,v 1.2 2005/09/10 18:29:46 gavin Exp $
      package org.jboss.seam.example.registration;
      import javax.ejb.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;
      public class RegisterAction implements Register {
       private User user;
       @In(value = "customMessage", create = true) @Out
       private Message myMessage;
       private EntityManager em;
       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) {
       log.info("Registered new user #{user.username}");
       return null; //"/registered.jsp";
       } else {
       FacesMessages.instance().add("User #{user.username} already exists");
       return null;

      <%@ 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" %>
       <title>Register New User</title>
       <table border="0">
       <td><h:inputText value="#{user.username}" required="true"/></td>
       <td>Real Name</td>
       <td><h:inputText value="#{user.name}" required="true"/></td>
       <td><h:inputSecret value="#{user.password}" required="true"/></td>
       <h:commandButton type="submit" value="Register" action="#{register.register}"/>
       Custom message: <b><h:outputText value="#{customMessage.message}"/></b><br/>
       My message: <b><h:outputText value="#{myMessage.message}"/></b><br/>

      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.