4 Replies Latest reply on Jan 11, 2011 7:10 PM by femibyte

    Refreshing context variable originally initialized via commandButton submission

    femibyte

      Hi, I am a fairly new user to Seam and I love using it so far.
      However, I need some help with an application I am currently working on. It's a cache administration interface,


      Basically I have an initial front page that allows the user to specify which environment to connect to - Dev,QA,Prod etc and based on the selection, a connection is initialized and the cache is queried on subsequent pages. The context variable env is set and propagated on subsequent pages. However the problem I am having is that when I return to the initialization page and select a different env value, this value does not override the previously set value, which seems 'sticky', Any suggestions on how to fix this problem? I have set scope as CONVERSATION scope, and I specify an @In annotation on the subsequent pages. I tried using PAGE scope, but that doesn't seem to work because I get errors that @In requires a null value.


      Any ideas about how to solve this issue would be greatly appreciated.


      Sincerely,
      Femi

        • 1. Re: Refreshing context variable originally initialized via commandButton submission
          monkeyden

          Unless you're using a long running conversation, which you would have to do explicitly, you are in the default/implied conversation which (I believe) is EVENT scope.  Suffice to say, it's too short for your purposes.  In the initial backing bean, you probably want to do:


          @Out(ScopeType.SESSION)
          private String env;
          



          and in your subsequent pages, use:


          @In
          private String env;
          



          Without seeing code, I can't venture a guess as to why it seems to be working as desired on the first pass.

          • 2. Re: Refreshing context variable originally initialized via commandButton submission
            femibyte

            My code looks like this:


            Backing bean for welcome page:


            @Name("welcomeBean")
            @Scope(CONVERSATION)
                public class WelcomeBean implements Welcome,Serializable {
                private final static Log logger=LogFactory.getLog(WelcomeBean.class);
                @Out(required=false)
                private String env;
             
               @NotNull @Lob
               public String getEnv() {
                 
                  return env;
               }
               @Begin
                public void setEnv(String env) {
                    logger.info("Setting ENV=" + env);
                    this.env=env;
               }

                public String submit() {
                    logger.info("Obtained ENV=" + env);
                    return "/namespaces.seam";
                }


            Backing bean for subsequent page:


            @Name("namespaceManager")
            @Stateful
            public class NamespaceManagerBean implements Serializable, NamespaceManager
            {
            private final static Log logger = LogFactory.getLog(NamespaceManagerBean.class);
               
                @In private String env;

               @DataModel
               private List<Namespace> namespaceList;


                @Out(required=false)
                    private List<String> commandList;

                
                @Factory("commandList")
                public void initCommandList()
                {
                    commandList=new ArrayList<String>();
                    commandList.add("getAllKeys");
                    commandList.add("getMatchingKeys");
                    commandList.add("removeNamespace");
                }
              
               @DataModelSelection
               @Out(required=false, scope = CONVERSATION)
               public Namespace namespace;

            @Out(value="cacheManager", required=true)
                private CoherenceCacheManager cacheManager;

                @Factory("cacheManager")
                public void getCacheConnection() throws Exception
                {
                    cacheManager=new CoherenceCacheManager();
                    logger.info("ENV=" + env);  
                    cacheManager.connectToCache(env);
              
                }

               @Factory("namespaceList")
               public void findNamespaces()
               {      
                    if (cacheManager==null)
                       {
                   logger.info("cacheManager is null");  
                   try {
                        getCacheConnection();
                   }
                   catch(Exception e)
                       {
                           e.printStackTrace();
                           }
                       }
                   String[] nsList=cacheManager.getAllNamespaces();
                   namespaceList=new ArrayList<Namespace>();
                   for(int i=0;i<nsList.length;i++)
                       {
                           Namespace ns=new Namespace();
                           ns.setTitle(nsList[i]);
                           namespaceList.add(ns);
                       }
                  
                 }
              
               public String select()
               {
                         if (namespace!=null) namespace.setRead(true);
                         return "/cacheEntries.seam";
               }
             


            The env variable is submitted via this jsp:



            <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
            <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
            <%@ taglib uri="http://jboss.com/products/seam/taglib" prefix="s" %>
            <html>
            <head>
              <title>Coherence Cache Management</title>
            </head>
            <body>
              <f:view>
              <h:form>
                 <h2>Coherence Cache Management</h2>

                 <h4>Select Environment</h4>
                 <h:selectOneMenu value="#{welcomeBean.env}">
                             <f:selectItem itemLabel="LocalDev" itemValue="LocalDev"/>
                             <f:selectItem itemLabel="Dev" itemValue="Dev"/>
                             <f:selectItem itemLabel="QA" itemValue="QA"/>
                 </h:selectOneMenu>
            <br><br><br>
                <h:commandButton value="Submit" action="#{welcomeBean.submit}">

            </h:commandButton>

            </h:form>
            </f:view>
            </body>
            </html>


            I need the env variable to be refreshed on the following page upon the 2nd submission via the jsp with a differnt value.


            Thanks for your help in advance.

            • 3. Re: Refreshing context variable originally initialized via commandButton submission
              femibyte

              Can someone let me know how to edit the post above? I mistakenly didn't do a preview before submitting it and would like to re-format it.

              • 4. Re: Refreshing context variable originally initialized via commandButton submission
                femibyte

                Better formatting of the code posted above.


                Backing bean for welcome page:


                @Name("welcomeBean")
                @Scope(CONVERSATION)
                
                public class WelcomeBean implements Welcome,Serializable {
                 private final static Log logger = LogFactory.getLog(WelcomeBean.class);
                
                
                    @Out(required=false)
                    private String env;
                  @NotNull @Lob
                   public String getEnv() {
                      
                      return env;
                   }
                   @Begin
                    public void setEnv(String env) {
                        logger.info("Setting ENV=" + env);
                        this.env=env;
                   }
                
                    public String submit() {
                        logger.info("Obtained ENV=" + env);
                        return "/namespaces.seam";// + "?env=" + env;
                
                    }
                }
                




                Backing bean for subsequent page:




                @Name("namespaceManager")
                @Stateful
                public class NamespaceManagerBean implements Serializable, NamespaceManager
                {
                private final static Log logger = LogFactory.getLog(NamespaceManagerBean.class);
                
                    
                    @In 
                     private String env;
                
                
                
                   @DataModel
                   private List<Namespace> namespaceList;
                
                
                    @Out(required=false)
                        private List<String> commandList;
                 @Factory("commandList")
                    public void initCommandList()
                    {
                        commandList=new ArrayList<String>();
                        commandList.add("getAllKeys");
                        commandList.add("getMatchingKeys");
                        commandList.add("removeNamespace");
                    }
                   
                   @DataModelSelection
                   @Out(required=false, scope = CONVERSATION)
                   public Namespace namespace;
                   
                    @Out(value="cacheManager", required=true)
                    private CacheManager cacheManager;
                
                    //    @Begin(join=true)
                    @Factory("cacheManager")
                    public void getCacheConnection() throws Exception
                    {
                        cacheManager=new CacheManager();
                        logger.info("ENV=" + env);   
                        cacheManager.connectToCache(env);
                
                    }
                
                   @Factory("namespaceList")
                
                public void findNamespaces() 
                   {       
                        if (cacheManager==null)
                           {
                       logger.info("cacheManager is null");   
                       try {
                            getCacheConnection();
                       }
                       catch(Exception e)
                           {
                               e.printStackTrace();
                               } 
                           }
                       String[] nsList=cacheManager.getAllNamespaces();
                       namespaceList=new ArrayList<Namespace>();
                       for(int i=0;i<nsList.length;i++)
                           {
                               Namespace ns=new Namespace();
                               ns.setTitle(nsList[i]);
                               namespaceList.add(ns);
                             
                           }
                     
                           }
                public String select()
                   {
                             if (namespace!=null) namespace.setRead(true);
                             return "/cacheEntries.seam";
                   }
                
                   
                   @Remove @Destroy
                       public void destroy() { 
                       env=null;
                }
                
                }
                


                The env variable is submitted via this jsp:




                <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
                <%@ taglib uri="http://jboss.com/products/seam/taglib" prefix="s" %>
                <html>
                 <head>
                  <title>Coherence Cache Management</title>
                 </head>
                 <body>
                  <f:view>
                  <h:form>
                     <h2>Cache Management</h2>
                
                     <h4>Select Environment</h4>
                     <h:selectOneMenu value="#{welcomeBean.env}">
                                 <f:selectItem itemLabel="LocalDev" itemValue="LocalDev"/>
                                 <f:selectItem itemLabel="Dev" itemValue="Dev"/>
                                 <f:selectItem itemLabel="QA" itemValue="QA"/>
                     </h:selectOneMenu>
                <br><br><br>
                    <h:commandButton value="Submit" action="#{welcomeBean.submit}">
                 
                 </h:commandButton>
                
                </h:form>
                </f:view>
                 </body>
                </html>




                I need the env variable to be refreshed on the following page upon the 2nd submission via the jsp with a differnt value.


                Thanks for your help in advance.