3 Replies Latest reply on Feb 23, 2006 9:27 AM by jameswoodward

    RequestParameter question

      Right, here's what I want to be able to do...

      I want to use the following URL as the starting point for my JSF application.

      http://localhost:8080/usermaint/listUsers.jsf?CompanyID=533&UserID=159

      This URL is part of a legacy application, which I cannot change. The CompanyID parameter identifies the company I want to display the list of users for. The UserID parameter identifies the current user.

      I want to store these values in a scope (SESSION or CONVERSATION?) so they can be referenced at any point whilst inside the application.

      I have tried using the following code, which works fine as long as you close the browser in between changing the selected CompanyID. Otherwise, on subsequent calls you always see the first CompanyID details.

      @Stateful
      @Name("currentUser")
      @Scope(ScopeType.SESSION)
      @Interceptors(SeamInterceptor.class)
      public class CurrentUserBean implements CurrentUser, Serializable {
      
       @In
       Context sessionContext;
      
       @RequestParameter("CompanyID")
       private String paramCompanyId;
      
       private Integer companyId;
      
       @RequestParameter("UserID")
       private String paramUserId;
      
       private Integer userId;
      
       @Create
       public void init() {
       companyId = new Integer(paramCompanyId);
       userId = new Integer(paramUserId);
       }
      ...
      }
      


      How should I be doing this please?

        • 1. Re: RequestParameter question
          gavin.king

          The problem is that you are using an @Create method of a SESSION scope component - which is called only the first time the component is needed.

          Instead, use getCompany() and getUser() methods, or use an @Create method of an EVENT scoped component.

          • 2. Re: RequestParameter question
            liudan2005

            Use @Factory for your method

            • 3. Re: RequestParameter question

              Thanks for that, here is my working version!

              @Stateful
              @Name("mySession")
              @Scope(ScopeType.SESSION)
              @Interceptors(SeamInterceptor.class)
              @Cache(NoPassivationCache.class)
              public class MySessionBean implements Serializable, MySession {
              
               @PersistenceContext
               private EntityManager entityManager;
              
               @RequestParameter("CompanyID")
               private String paramCompanyId;
              
               @RequestParameter("UserID")
               private String paramUserId;
              
               private Company company;
              
               private User user;
              
               @Out
               @Factory("company")
               public Company getCompany() {
               if (paramCompanyId != null) {
               company = entityManager.find(Company.class, new Integer(paramCompanyId));
               }
              
               return company;
               }
              
               @Out
               @Factory("user")
               public User getUser() {
               if (paramUserId != null) {
               user = entityManager.find(User.class, new Integer(paramUserId));
               }
              
               return user;
               }
              
               @Create
               public void init() {
               System.out.println("MySessionBean.init()");
               }
              
               @Destroy
               @Remove
               public void destroy() {
               System.out.println("MySessionBean.destroy()");
               }
              }
              


              Which I can access from the view using:

              <af:outputText value="#{company.name}"/>