Version 2

    &30331;&37682;&20966;&29702;&12398;&20869;&37096;&21205;&20316; (What happens in the registration process?)

     

    The registration page is formated as a regular XHTML form.

     

    &30331;&37682;&12506;&12540;&12472;&12399;&36890;&24120;&12398;XHTML&12501;&12457;&12540;&12512;&12398;&26360;&24335;&12391;&35352;&36848;&12373;&12428;&12414;&12377;&12290;

     <div class="entry">
        <div class="label">
            <h:outputLabel for="username">Username:</h:outputLabel>
        </div>
        <div class="input">
            <h:inputText id="username" value="#{user.username}"></h:inputText><br/>
            <span class="errors"><h:message for="username" ></h:message></span>
        </div>
     </div>
     <div class="entry">
        <div class="label">
            <h:outputLabel for="name">Real Name:</h:outputLabel>
        </div>
        <div class="input">
            <h:inputText id="name" value="#{user.name}" ></h:inputText><br/>
            <span class="errors"><h:message for="name" ></h:message></span>
        </div>
     </div>
     
     ... ...
     
        <div class="input">
            <h:commandButton value="Register" 
                             action="#{register.register}" 
                             class="button"></h:commandButton>
            <h:commandButton value="Cancel" action="login" class="button"></h:commandButton>
        </div>
    

    As you can see the fields in the form are mapped to the properties in the user bean (e.g., the {user.username} property is mapped to the Username field in the form). The form action is mapped to the register() method in the register bean (i.e., the {register.register} action.

     

    &12372;&35239;&12398;&12424;&12358;&12395;&12501;&12457;&12540;&12512;&20869;&12398;&12501;&12451;&12540;&12523;&12489;&12399;&12518;&12540;&12470;Bean&12395;&12510;&12483;&12503;&12373;&12428;&12414;&12377;&65288;&12388;&12414;&12426;&12289;{user.username}&12503;&12525;&12497;&12486;&12451;&12399;&12501;&12457;&12540;&12512;&12391;&12398;Username&12501;&12451;&12540;&12523;&12489;&12395;&12510;&12483;&12503;&12373;&12428;&12414;&12377;&65289;&12290;form&12450;&12463;&12471;&12519;&12531;&12399;&30331;&37682;Bean&12398;register()&12513;&12477;&12483;&12489;&65288;&12388;&12414;&12426;&12289;{register.register}&12450;&12463;&12471;&12519;&12531;)&12395;&12510;&12483;&12503;&12373;&12428;&12414;&12377;&12290;

     

    So, let's check out the User entity bean. EJB 3.0 maps the entity bean to the User SQL table by default, with each bean property corresponding to one data column. And Seam maps the User bean to the The specified item was not found. Bean&12395;&12510;&12483;&12503;&12375;&12414;&12377;&12290;@NotNull&12420;@Length&12392;&12356;&12387;&12383;&22949;&24403;&24615;&21046;&32004;&12450;&12494;&12486;&12540;&12471;&12519;&12531;(validation constraint annotation)&12395;&27880;&24847;&12375;&12390;&12362;&12367;&12371;&12392;&12399;&22823;&20107;&12391;&12377;&12290;&12381;&12428;&12425;&12399;Seam&12395;&12424;&12387;&12390;&33258;&21205;&30340;&12395;&26908;&35388;&12364;&34892;&12431;&12428;&12414;&12377;&12290;JSF&12501;&12457;&12540;&12512;&12398;&19981;&27491;&12487;&12540;&12479;&12434;&20837;&21147;&12377;&12427;&12392;&12289;Seam&12399;&30331;&37682;&12506;&12540;&12472;&12395;&12522;&12480;&12452;&12524;&12463;&12488;&12375;&12390;&12289;&12456;&12521;&12540;&12513;&12483;&12475;&12540;&12472;&12434;&34920;&31034;&12375;&12414;&12377;&12290;

     @Entity
     @Name("user")
     @Scope(SESSION)
     public class User implements Serializable {
       private String username;
       private String password;
       private String name;
     
       @NotNull
       @Length(max=100)
       public String getName() {
          return name;
       }
     
        // ... ...
        
       @NotNull
       @Length(min=5, max=15)
       public String getPassword() {
          return password;
       }
       
       // ... ...
       
       @Id
       @Length(min=5, max=15)
       public String getUsername() {
          return username;
       }
       
       // ... ...
     }
    

    The RegisterAction is an EJB 3.0 session bean and it is mapped to the bean in the JSF / Facelets page via the @Name annotation by SEAM. So, the RegisterAction.register() method is invoked when the "register" button is clicked. RegisterAction&12399;EJB 3.0&12475;&12483;&12471;&12519;&12531;Bean&12391;&12289;&12381;&12428;&12399;JSF&12420;Facelets&12398;Bean&12395;&12510;&12483;&12503;&12373;&12428;&12414;&12377;&12290;&12381;&12428;&12399;Seam&12364;@Name&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12434;&20351;&12358;&12371;&12392;&12391;&12395;&12424;&12387;&12390;&34892;&12431;&12428;&12414;&12377;&12290;&12371;&12358;&12375;&12390;&12289;RegisterAction.register()&12513;&12477;&12483;&12489;&12399;"register"&12508;&12479;&12531;&12364;&25276;&12373;&12428;&12383;&12392;&12365;&12395;&21628;&12403;&20986;&12373;&12428;&12414;&12377;&12290;

     @Stateful
     @Scope(EVENT)
     @Name("register")
     @Interceptor(SeamInterceptor.class)
     public class RegisterAction implements Register {
     
       @In @Valid
       private User user;
       
       @PersistenceContext
       private EntityManager em;
       
       @In
       private FacesContext facesContext;
       
       @IfInvalid(outcome=REDISPLAY)
       public String register() {
          if ( user.getPassword().equals(verify) ) {
             List existing = em.createQuery(
                "select username from User where username=:username")
                .setParameter("username", user.getUsername())
                .getResultList();
             if (existing.size()==0) {
                em.persist(user);
                return "login";
             } else {
                facesContext.addMessage(null, 
                    new FacesMessage("username already exists"));
                return null;
             }
          } else {
             facesContext.addMessage(null, 
                new FacesMessage("re-enter your password"));
             verify=null;
             return null;
          }
       }
     
       // ... ...
     }
    

    The @In annotation in the RegisterAction() session bean injects the User entity bean and the FacesContext system object. The form input is validated by the server to match the data schema, if it is not valid the @IfInValid annotation tells the JSF to re-display the form with error messages.