Version 2

    &12525;&12464;&12452;&12531;&26178;&12398;&20869;&37096;&21205;&20316; (What happens when you login?)

     

    The login page is formated as a regular XHTML form. The form uses the The specified item was not found. &12471;&12531;&12508;&12523;User&12456;&12531;&12486;&12451;&12486;&12451;Bean&12434;&34920;&29694;&12377;&12427;&12418;&12398;&12392;&12375;&12390;&12289;&12471;&12531;&12508;&12523;&12434;LoginAction&12456;&12531;&12486;&12451;&12486;&12451;&12434;&34920;&29694;&12377;&12427;&12418;&12398;&12392;&12375;&12390;&20351;&12356;&12414;&12377;&12290;&20001;&26041;&12398;Bean&12399;Seam&12395;&12424;&12387;&12390;&31649;&29702;&12373;&12428;&12427;EJB 3.0 POJO&12391;&12377;&12290;

     <div>
        <h:outputLabel for="username">Login Name</h:outputLabel>
        <h:inputText id="username" value="#{user.username}" ></h:inputText>
     </div>
     <div>
        <h:outputLabel for="password">Password</h:outputLabel>
        <h:inputSecret id="password" value="#{user.password}" ></h:inputSecret>
     </div>
     
     ... ...
     
     <div class="buttonBox">
        <h:commandButton action="#{login.login}" 
                         value="Account Login" class="button" ></h:commandButton>
     </div>
    
    The User bean is mapped to the The specified item was not found. Bean&12395;&12510;&12483;&12503;&12373;&12428;&12414;&12377;&12290;username&12392;password Bean&12503;&12525;&12497;&12486;&12451;&12399;XHTM&12501;&12457;&12540;&12512;&20869;&12398;&12518;&12540;&12470;&20837;&21147;&12501;&12451;&12540;&12523;&12489;&12395;&12510;&12483;&12503;&12373;&12428;&12414;&12377;&12290;
     @Entity
     @Name("user")
     @Scope(SESSION)
     public class User implements Serializable {
       private String username;
       private String password;
       private String name;
     
       @NotNull
       @Length(min=5, max=15)
       public String getPassword() {
          return password;
       }
       public void setPassword(String password) {
          this.password = password;
       }
       
       @Id
       @Length(min=5, max=15)
       public String getUsername () {
          return username;
       }
       public void setUsername (String username) {
          this.username = username;
       }
     
        // ... ...
     }
    
    The LoginAction is an EJB 3.0 session bean and it is mapped to the symbol in the JSF page via the @Name annotation by Seam. So, the LoginAction.login() method is invoked when the "login" button is clicked. This bean is "stateless" since it only has one-time transactional methods.

     

    LoginAction&12399;EJB 3.0&12475;&12483;&12471;&12519;&12531;Bean&12391;&12289;&12381;&12428;&12399;Seam&12395;&12424;&12387;&12390;@Name&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12434;&20351;&12387;&12390;JSF&12506;&12540;&12472;&20869;&12398;&12471;&12531;&12508;&12523;&12395;&12510;&12483;&12503;&12373;&12428;&12414;&12377;&12290;&12375;&12383;&12364;&12387;&12390;&12289;LoginAction.login()&12513;&12477;&12483;&12489;&12399;&12300;&12525;&12464;&12452;&12531;(login)&12301;&12508;&12479;&12531;&12364;&12463;&12522;&12483;&12463;&12373;&12428;&12427;&12392;&21628;&12403;&20986;&12373;&12428;&12414;&12377;&12290;&12371;&12398;Bean&12399;&21628;&12403;&20986;&12377;&12383;&12403;&12395;&12488;&12521;&12531;&12470;&12463;&12471;&12519;&12531;&12364;&32066;&20102;&12377;&12427;&12513;&12477;&12483;&12489;&12434;&25345;&12388;&12384;&12369;&12394;&12398;&12391;&12300;&12473;&12486;&12540;&12488;&12524;&12473;&12301;&12391;&12377;&12290;

     @Stateless
     @Name("login")
     @Interceptor(SeamInterceptor.class)
     public class LoginAction implements Login {
       
       @In @Out
       private User user;
       
       @PersistenceContext
       private EntityManager em;
       
       @In
       private Context sessionContext;
       @In
       private FacesContext facesContext;
     
       public String login() {
          List <User> results = em.createQuery(
            "from User where username=:username and password=:password")
                .setParameter("username", user.getUsername())
                .setParameter("password", user.getPassword())
                .getResultList();
          
          if ( results.size()==0 ) {
             facesContext.addMessage(null, new FacesMessage("Invalid login"));
             return "login";
          } else {
             user = results.get(0);
             sessionContext.set("loggedIn", true);         
             return "main";
          }
       }
     }
    

    The @In annotation in the LoginAction session bean injects the User entity bean and the HTTP session Context / FacesContext system objects. The @Out annotation indicates the LoginAction bean can also modify the User object managed by Seam and make the altered instance available to other session beans / JSF pages.

     

    LoginAction&12475;&12483;&12471;&12519;&12531;Bean&12398;@In&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12399;&12289;User&12456;&12531;&12486;&12451;&12486;&12451;Bean&12392;HTTP&12475;&12483;&12471;&12519;&12531;&12467;&12531;&12486;&12461;&12473;&12488;&12420;FacesContext&12471;&12473;&12486;&12512;&12458;&12502;&12472;&12455;&12463;&12488;&12434;&27880;&20837;&12375;&12414;&12377;&12290;@Out&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12399;LoginAction Bean&12364;Seam&12395;&12424;&12387;&12390;&31649;&29702;&12373;&12428;&12427;User&12458;&12502;&12472;&12455;&12463;&12488;&12434;&20462;&27491;&12391;&12365;&12427;&12371;&12392;&12392;&12289;&20182;&12398;&12475;&12483;&12471;&12519;&12531;Bean&12420;JSF&12506;&12540;&12472;&12395;&23550;&12375;&12390;&21029;&12398;&12452;&12531;&12473;&12479;&12531;&12473;&12434;&21033;&29992;&21487;&33021;&12395;&12391;&12365;&12427;&12371;&12392;&12434;&24847;&21619;&12375;&12414;&12377;&12290;