3 Replies Latest reply on Aug 28, 2009 10:26 PM by Arbi Sookazian

    write a string to variable

    Kmali Malik Newbie

      Hi,
      I'm a seam and java newbe and have a simple question.


      Ho can a write a string given in a (xhtml-)input box into a variable defined in the java code?


      I've made a class login:




      public class login {
              
              public static String username;
              public static String loginname = "test1";
      
              
              
              public boolean check()
              {
              
              if (login.username == login.loginname);
              
              return true;
                      
              }
      }





      This the xhtml code:




      div class="dialog">
                  <div class="prop">
                      <span class="name">Username</span>
                      <span class="value">
                          <h:inputText id="username" value="#{login.username}"/>
                      </span>
                  </div>





      It doesn't work, I get the following error message:
      value could not be converted to the expected type


      What am I doing worng?


      thank you



        • 1. Re: write a string to variable
          Leo van den berg Master

          Hi Kmali,


          If you're a Seam AND a Java newbie I think you're probably overeating a bit.


          Try with some simple java classes and html-pages first and see how that works.


          To give you some hints: Your class lacks getters and setters for the two attrbutes and they're defined as static, which makes it very complex to use in a multi-user environment (web).






          • 2. Re: write a string to variable
            Kmali Malik Newbie


            I just need a login funktion for a application I haven't written myself.
            I tried the to implemnt it via the autheticator class, but it didn't work. (Obviously because of my seam knowledge)


            So I want to try to implement a simple way to authenticate a user.
            I doesn't need to be very secure, because it will only run in a local Network.


            I have a User class from a example Application with getters and setters:



            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.hibernate.validator.Pattern;
            import org.jboss.seam.annotations.Name;
            import org.jboss.seam.annotations.Scope;
            
            @Entity
            @Name("user")
            @Scope(SESSION)
            @Table(name="Customer")
            public class User implements Serializable
            {
                 private String username;
                 private String password;
                 
               
               public User( String username, String password)
               {
                    
                    this.username = username;
                    this.password = password;
               }
               
               public User() {}
               
               @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)
               @Pattern(regex="^\\w*$", message="not a valid username")
               public String getUsername()
               {
                  return username;
               }
               public void setUsername(String username)
               {
                  this.username = username;
               }
               
               @Override
               public String toString() 
               {
                  return "User(" + username + ")";
               }
            }



            I just want to check a hardcoded Username and Password and redirect the user to the home.xhtml
            I would appriciate if somebody could explain to me how to realize this... :(



            • 3. Re: write a string to variable
              Arbi Sookazian Master

              look at the booking example in the distro:


              @Stateless
              @Name("authenticator")
              public class AuthenticatorAction 
                  implements Authenticator
              {
                  @PersistenceContext 
                  private EntityManager em;
              
                  @In(required=false)   
                  @Out(required=false, scope = SESSION)
                  private User user;
                 
                  public boolean authenticate()
                  {
                   List results = em.createQuery("select u from User u where u.username=#{identity.username} and u.password=#{identity.password}")
                                       .getResultList();
                    
                   if (results.size()==0) {
                       return false;
                   } else {
                       user = (User) results.get(0);
                       return true;
                   }
                  }
                  
              }