Discussion:
Need help with using HashMap in facelets
beginner_prithvi
2009-11-19 11:21:21 UTC
Permalink
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.

************************************

public class ReadFileClass {
.................
..................
Map<String, StringBuilder> fileContentMap = new HashMap<String,
StringBuilder>();
.................
.................

public Map<String, StringBuilder> getFileContentMap() {
return fileContentMap;
}

..................
..................

//Logic to store data in 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 think I need to tell you what I am going to do with this HashMap.

I am planning to create a dynamic tabbed pane.
It will have 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.

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

Can someone guide me properly for doing it?

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>

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)


Whats wrong here?
--
View this message in context: http://old.nabble.com/Need-help-with-using-HashMap-in-facelets-tp26421286p26421286.html
Sent from the java.net - facelets users mailing list archive at Nabble.com.
Paulo Pinto
2009-11-19 12:33:58 UTC
Permalink
Hi ,

your EL expression is wrong.

You should have written:

"#{readFile.fileContentMap}"

The EL uses the same notation as JavaBeans for the getter/setters.

-
Paulo
Post by 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
filename is a key in a hash map and filecontent is a StringBuilder object
saved as value for that key.
Note: Here aFile is a file object array which holds references to the files
in the specific folder. fileContentMap is a HashMap.
************************************
public class ReadFileClass {
.................
..................
Map<String, StringBuilder> fileContentMap = new HashMap<String,
StringBuilder>();
.................
.................
public Map<String, StringBuilder> getFileContentMap() {
return fileContentMap;
}
..................
..................
//Logic to store data in 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 think I need to tell you what I am going to do with this HashMap.
I am planning to create a dynamic tabbed pane.
It will have 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.
I need a way to access that hashmap in my facelet.
Can someone guide me properly for doing it?
<rich:tabPanel id="myTabs" switchType="client">
<c:forEach items="#{readFile.getFileContentMap}" var="x">
<rich:tab label="#{x.name}">
</rich:tab>
</c:forEach>
</rich:tabPanel>
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)
Whats wrong here?
--
http://old.nabble.com/Need-help-with-using-HashMap-in-facelets-tp26421286p26421286.html
Sent from the java.net - facelets users mailing list archive at Nabble.com.
---------------------------------------------------------------------
Mark Collette
2009-12-10 21:52:35 UTC
Permalink
If you have an accessor method called getFileContentMap(), then your EL
should be #{readFile.fileContentMap} not
#{readFile.getFileContentMap}. Also, c:forEach needs a datastructure
that has an ordering to it, which Map doesn't provide. As well, how
would the name property resolve to the Map key? Instead, maybe make
your own class that holds the file name and the StringBuilder, and then
just have an ArrayList of that class.

- Mark Collette
Post by 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
filename is a key in a hash map and filecontent is a StringBuilder object
saved as value for that key.
Note: Here aFile is a file object array which holds references to the files
in the specific folder. fileContentMap is a HashMap.
************************************
public class ReadFileClass {
.................
..................
Map<String, StringBuilder> fileContentMap = new HashMap<String,
StringBuilder>();
.................
.................
public Map<String, StringBuilder> getFileContentMap() {
return fileContentMap;
}
..................
..................
//Logic to store data in 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 think I need to tell you what I am going to do with this HashMap.
I am planning to create a dynamic tabbed pane.
It will have 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.
I need a way to access that hashmap in my facelet.
Can someone guide me properly for doing it?
<rich:tabPanel id="myTabs" switchType="client">
<c:forEach items="#{readFile.getFileContentMap}" var="x">
<rich:tab label="#{x.name}">
</rich:tab>
</c:forEach>
</rich:tabPanel>
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)
Whats wrong here?
Loading...