6 Replies Latest reply on Nov 30, 2009 6:52 AM by beginner_prithvi

    Need help with using HashMap in facelets

    beginner_prithvi

      Hi,
      I am a newbie and learning facelets and JSF. I am writing a small application which will read files from a specific folder and will display each file in separate tab in page.

      Presently I am saving filenames as a key in a hashmap and corresponding file contents I am saving as value for that key. For saving filecontents in managed bean, I am using StringBuilder. So the scenario is:
      filename is a key in a hash map and filecontent is a StringBuilder object saved as value for that key.

      Below is the code for the same:
      Note: Here aFile is a file object array which holds references to the files in the specific folder. fileContentMap is a HashMap.

       if (aFile.length > 0) {
       StringBuilder contents = null;
       fileContentMap.clear();
       for (int i = 0; i < aFile.length; i++) {
       if (aFile.isFile()) {
       try {
       contents = new StringBuilder();
       BufferedReader input = new BufferedReader(
       new FileReader(aFile));
       try {
       String line = ""; // not declared within while
       // loop
      
       while ((line = input.readLine()) != null) {
       contents.append(line);
       contents.append(System
       .getProperty("line.separator"));
       }
      
      
       fileContentMap.put(aFile.getName(),contents);
       } finally {
       input.close();
       }
      
       } catch (IOException ex) {
       System.out.println("IOExcpetion occured");
       ex.printStackTrace();
       return "error";
       }
       }
      
       }
      
       }
      
      


      I need a way to access that hashmap in my facelet.

      Can someone guide me properly for doing it?


        • 1. Re: Need help with using HashMap in facelets
          beginner_prithvi

          I think I nee to tell you what I am goint to tell with this HashMap.

          I am planning to create a dynamic tabbed pane.
          It will ahve number of tabs as equal to number of files. Each tab lable will be the name of the file. And inside that tab i will display the corresponding file.

          I think it will help you to think in that direction.

          • 2. Re: Need help with using HashMap in facelets
            beginner_prithvi

            Presently I am trying to access HashMap in this way:

             <rich:tabPanel id="myTabs" switchType="client">
             <c:forEach items="#{readFile.getFileContentMap}" var="x">
             <rich:tab label="#{x.name}">
             </rich:tab>
             </c:forEach>
             </rich:tabPanel>
            


            And I have defined HashMap as:
            public class ReadFileClass {
            .................
            ..................
             Map<String, StringBuilder> fileContentMap = new HashMap<String, StringBuilder>();
            .................
            .................
            
             public Map<String, StringBuilder> getFileContentMap() {
             return fileContentMap;
             }
            
            ..................
            ..................
            


            But I am getting exception as:
            Nov 19, 2009 4:37:19 PM com.sun.facelets.FaceletViewHandler handleRenderException
            SEVERE: Error Rendering View[/logDisplay.xhtml]
            javax.el.ELException: /logDisplay.xhtml: Property 'getFileContentMap' not found on type com.cts.ReadFileClass
             at com.sun.facelets.compiler.AttributeInstruction.write(AttributeInstruction.java:53)
             at com.sun.facelets.compiler.UIInstructions.encodeBegin(UIInstructions.java:39)
             at org.ajax4jsf.renderkit.RendererBase.renderChild(RendererBase.java:280)
             at org.ajax4jsf.renderkit.RendererBase.renderChildren(RendererBase.java:262)
             at org.richfaces.renderkit.html.TabPanelRenderer.doEncodeChildren(TabPanelRenderer.java:266)
             at org.richfaces.renderkit.html.TabPanelRenderer.doEncodeChildren(TabPanelRenderer.java:261)
             at org.ajax4jsf.renderkit.RendererBase.encodeChildren(RendererBase.java:121)
             at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:809)
             at javax.faces.component.UIComponent.encodeAll(UIComponent.java:886)
             at javax.faces.render.Renderer.encodeChildren(Renderer.java:137)
             at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:809)
             at javax.faces.component.UIComponent.encodeAll(UIComponent.java:886)
             at javax.faces.component.UIComponent.encodeAll(UIComponent.java:892)
             at com.sun.facelets.FaceletViewHandler.renderView(FaceletViewHandler.java:592)
             at org.ajax4jsf.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:108)
             at org.ajax4jsf.application.AjaxViewHandler.renderView(AjaxViewHandler.java:216)
             at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:106)
             at com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:251)
             at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:144)
             at javax.faces.webapp.FacesServlet.service(FacesServlet.java:245)
             at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
             at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
             at org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:141)
             at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:281)
             at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
             at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
             at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
             at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
             at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
             at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
             at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
             at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
             at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
             at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
             at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
             at java.lang.Thread.run(Unknown Source)
            


            What is wrong here?

            • 3. Re: Need help with using HashMap in facelets
              ilya_shaikovsky
              • 4. Re: Need help with using HashMap in facelets
                nbelaevski

                That's the reason:

                Property 'getFileContentMap' not found on type com.cts.ReadFileClass


                • 5. Re: Need help with using HashMap in facelets

                  This is more of a facelets question.

                  The solution is two fold.

                  You need one more function in your managed bean

                  public List<String> getFileContentNames(){
                  return new ArrayList<String>(fileContentMap.keySet());
                  }
                  


                  And then in the XHTML, do something like this:


                   <rich:tabPanel id="myTabs" switchType="client">
                   <c:forEach items="#{readFile.fileContentNames}" var="x">
                   <rich:tab label="#{readFile.fileContentMap[x]}">
                   </rich:tab>
                   </c:forEach>
                   </rich:tabPanel>
                  



                  This is how, I made it work for UI:INCLUDE.

                  with regards,
                  Natarajan
                  CTS

                  • 6. Re: Need help with using HashMap in facelets
                    beginner_prithvi

                    Hi nbelaevski,
                    I solved the exception. Now I am stuck up with accessing the Map in facelet.

                    Hi ilya_shaikovsky,
                    I tried the approach given in the link. It didnt work for me :(
                    I tried printing map keys using <c:out\>. But i couldnt. Nothing got printed on the page.

                    Hi Natarajan,
                    I tried your approach, but tab labels are simply blank. Even only one tab is getting created instead of equal to number of files.

                    @Ilya and Natarajan: As per Natarajan's logic I tried printing keys on console. And it is printing the names of the file perfectly. But the only thing is that it is not able to print it in facelet. Again one more query, why only one tab is getting created? Actual number of files in the folder are more than one. (Presently two)