13 Replies Latest reply on Oct 9, 2007 2:06 AM by Mohammad Norouzi

    please help me out of this problem

    Mohammad Norouzi Novice

      Hi

      I have a SFSB as follows, it has an entity named "user", in page when I change a list box I want the selected user to be loaded but nothing will be displayed

      @Stateful
      @Name("userRegister")
      @Scope(ScopeType.SESSION)
      public class UserRegisterAction implements UserRegister {
      
       private Log logger = LogFactory.getLog(UserRegisterAction.class);
      
       private User user;
       private String selectedUsername = "0";
      
       //rest of the codes....
      
       public void userListValueChanged(ValueChangeEvent event) {
      
       try {
       selectedUsername = (String) event.getNewValue();
       if(selectedUsername.equals("0")){
       logger.debug("new user");
       User u = new User();
       setUser(u);
       }else{
       logger.debug("edit:"+ selectedUsername);
       User u = userDao.load(selectedUsername);
       setUser(u);
       logger.debug("test:"+ user.getUsername());
       }
       } catch (Exception e) {
       e.printStackTrace();
       }
       }
      
      
      
       //getters and setters....
      


      now in xhtml file:

       <h:panelGroup id="exportUser">
       <h:outputLabel id="userL"
       value="#{bundle['userDefination.group']}" for="groupMenu" />
       <t:selectOneMenu id="userMenu" required="false" value="#{userRegister.selectedUsername}"
       valueChangeListener="#{userRegister.userListValueChanged}"
       onchange="submit(this)" >
       <f:selectItem itemLabel="New User ..." itemValue="0" />
       <f:selectItems id="userList" value="#{userRegister.allUsers}" />
       </t:selectOneMenu>
       </h:panelGroup>
      
      


      after the user change the list this form will submit but any reference to userRegister.user.username or other properties of user don't work .

      at first, user is not null it is a user with null property values. the thing wondering me is that in change value listener I load the user with new values but new values won't be displayed at page!!!

      and #{userRegister.user} returns the object reference of User with the same value that it was already.

      any help would be appreciated

      thanks

        • 1. Re: please help me out of this problem
          Mohammad Norouzi Novice

          #{userRegister.user} always returns value "com.epd.search.entity.User@2f8"
          even after I set the user to "new User()" it won't change !!!

          • 2. Re: please help me out of this problem
            Mohammad Norouzi Novice

            my User's scope is SESSION is this the issue which cause this problem?

            because I just read somewhere in seam reference about @Role that we can define many roles for an entity but I don't know how to use them?

            • 3. Re: please help me out of this problem
              Mohammad Norouzi Novice

              I had this problem in another page and after binding that component it worked but here I have 6 text fields I don't want to bind all of them.

              • 4. Re: please help me out of this problem
                Fernando Montaño Expert

                I would use a @Factory.


                Java:

                
                @Stateful
                @Name("userRegister")
                @Scope(ScopeType.SESSION)
                public class UserRegisterAction implements UserRegister {
                
                 private Log logger = LogFactory.getLog(UserRegisterAction.class);
                
                 private User user;
                 private String selectedUsername = "0";
                
                
                
                 @Factory(value = "user", scope = ScopeType.STATELESS)
                 public User initUser() {
                 if(selectedUsername.equals("0")){
                 logger.debug("new user");
                 user = new User();
                 }else{
                 logger.debug("edit:"+ selectedUsername);
                 user = userDao.load(selectedUsername);
                 logger.debug("test:"+ user.getUsername());
                 }
                 return user;
                 }
                
                 public void userListValueChanged(ValueChangeEvent event) {
                
                 try {
                 selectedUsername = (String) event.getNewValue();
                
                 } catch (Exception e) {
                 e.printStackTrace();
                 }
                 }
                
                }
                
                



                Facelet:

                <h:panelGroup id="exportUser">
                <h:outputLabel id="Username"
                <h:inputText value=#{user.username}/>
                
                <h:outputLabel id="Whatever"
                <h:inputText value=#{user.whateverProperty}/>
                
                <h:outputLabel id="userL" value="#{bundle['userDefination.group']}" for="groupMenu" />
                <t:selectOneMenu id="userMenu" required="false" value="#{userRegister.selectedUsername}"
                 valueChangeListener="#{userRegister.userListValueChanged}" onchange="submit(this)" >
                 <f:selectItem itemLabel="New User ..." itemValue="0" />
                 <f:selectItems id="userList" value="#{userRegister.allUsers}" />
                </t:selectOneMenu>
                </h:panelGroup>
                


                • 5. Re: please help me out of this problem
                  Mohammad Norouzi Novice

                  fernando,
                  really thanks for your nice solution, it worked fine, but another problem

                  when I select a user the information is displayed correctly, now consider I am changing some info say, address or contact number, how to manage this changed data at server and synchronize the changed User with the database? because the outjected with @Factory only set an attribute with stateless scope and now how to send back the new data to the server?

                  first, I tried to add @In for the user as follows:

                   @In("tempUser")
                   private User user;
                  
                   // consider @Factory's value is "tempUser"
                  
                   @Factory(value="tempUser",scope=STATELESS)
                   public User initUser(){
                   //.....
                   }
                  


                  but it didn't work

                  any solution?

                  thanks

                  • 6. Re: please help me out of this problem
                    Fernando Montaño Expert

                    Yes, you're right, "user" is stateless, but take a look what return initUser method. It returns the reference to the instance variable "user".


                    So, this should work:

                    private User user;
                    public void saveCurrentUser() {
                     userDao.update(user);
                    }
                    




                    BTW, I improved a bit my last example (avoiding several loading roundtrip):

                    
                    @Stateful
                    @Name("userRegister")
                    @Scope(ScopeType.SESSION)
                    public class UserRegisterAction implements UserRegister {
                    
                     private Log logger = LogFactory.getLog(UserRegisterAction.class);
                    
                     private User user;
                     private String selectedUsername = "0";
                     private boolean reload;
                    
                    
                    
                     @Factory(value = "user", scope = ScopeType.STATELESS)
                     public User initUser() {
                     if(selectedUsername.equals("0")){
                     logger.debug("new user");
                     user = new User();
                     }else{
                     logger.debug("edit:"+ selectedUsername);
                     if(reload) {
                     user = userDao.load(selectedUsername);
                     reload = false;
                     logger.debug("test:"+ user.getUsername());
                     }
                     }
                     return user;
                     }
                    
                     public void userListValueChanged(ValueChangeEvent event) {
                    
                     try {
                     selectedUsername = (String) event.getNewValue();
                     reload = true;
                    
                     } catch (Exception e) {
                     e.printStackTrace();
                     }
                     }
                    
                     }
                    }
                    


                    • 7. Re: please help me out of this problem
                      Mohammad Norouzi Novice

                      yes, but saveCurrentUser() that you declared only updates the instance variable user and the changes maded by user at client is not visible here. in other words, when I click on submit the changes I've made, not binded to any variable at server-side.

                      I'm not sure I could clarify myself or not

                      • 8. Re: please help me out of this problem
                        Fernando Montaño Expert

                        Did you try it? Because I have the same case (I wrote) working in my application.

                        • 9. Re: please help me out of this problem
                          Fernando Montaño Expert

                          Also take into account the last change I did regarding the reloading of the user, because if a new instance of the same user is reloaded every time, then you don't expect to have the same reference instance variable updated.

                          • 10. Re: please help me out of this problem
                            Mohammad Norouzi Novice

                            thanks Fernando, it works fine :)

                            with relaod, it works, but would you please tell me how this works?
                            as I understood, this @Factory will be called when at client-side an attribute of that name is referred (for example "tempUser")
                            and "relaod" flag compels loading the user only when my combobox has been changed.

                            Am I right?

                            thank you again for your nice solution.

                            • 11. Re: please help me out of this problem
                              Fernando Montaño Expert

                              Yes your'e right.

                              STATELESS @Factory means it will be called every time the name is referenced in the page. In this case returning the already instantiated class.

                              And yes, the value change listener is invoked when the value is changed in the selectbox, enabling the @Factory method to reload with the new username.

                              • 12. Re: please help me out of this problem
                                Sergio R Pantano Newbie

                                Thank you!

                                Work well!

                                @Factory(value = "license", scope=ScopeType.STATELESS)



                                • 13. Re: please help me out of this problem
                                  Mohammad Norouzi Novice

                                  Hi Fernando

                                  sorry to revive this thread, but I have a problem in the above SB

                                  I don't know why changes to fields at client-side is not being seen at server-side. I mean when a user changes an item say, Phone no, and press the Save button at server-side I can see the same value that field already had and new value entered by user has gone!!!

                                  and another strange thing is that, only changes to last item will be applied!!! for example, if I change the position of Phone No to last item, afterwards it will work but the other items don't!!!

                                  do you know what happens here?

                                  first I tried to bind the components to its server items but the problem remains.

                                  thanks so much