You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Paul Spencer <pa...@apache.org> on 2006/06/29 05:13:55 UTC
How to displayi an images generated by a managed bean?
I need to include a graphic image generated by JFreeCharts. Currently
the generation of the image is in an action of a managed bean and is
displayed in a separate windows via <h:commandButton>. How should I
convert this to a <h:graphicImage> so it can be displayed in an existing
page?
Paul Spencer
Re: How to displayi an images generated by a managed bean?
Posted by Cagatay Civici <ca...@gmail.com>.
Hi,
As Lance already mentioned, there is a JSF Chart Component based on
JFreeChart.
Here is the online example to try the component;
http://www.cagataycivici.com/chartcreator/
Also the RC1 for the new version will be released in JSF-COMP tomorrow.
Cheers,
Cagatay
On 6/29/06, L Frohman <lf...@gmail.com> wrote:
>
> Look at:
>
>
> http://www.jroller.com/page/cagataycivici?entry=phaselistener_renders_an_ima
> ge_no
>
> there is also a chart creator:
>
> http://www.jroller.com/page/cagataycivici?entry=jsf_chart_creator
>
> Lance
>
> -----Message d'origine-----
> De : Paul Spencer [mailto:paulsp@apache.org]
> Envoyé : jeudi 29 juin 2006 05:14
> À : MyFaces Discussion
> Objet : How to displayi an images generated by a managed bean?
>
> I need to include a graphic image generated by JFreeCharts. Currently the
> generation of the image is in an action of a managed bean and is displayed
> in a separate windows via <h:commandButton>. How should I convert this to
> a
> <h:graphicImage> so it can be displayed in an existing page?
>
> Paul Spencer
>
>
Re: How to displayi an images generated by a managed bean?
Posted by Paul Spencer <pa...@mindspring.com>.
Julian,
Can you explain the code and configuration behind
#{myBackingBean.chartURL} and the servlet you have created?
I need to understand where the graphic is created, i.e. in the managed
bean or servlet, how the servlet gets what is needs, and any
housekeeping that is performed.
Paul Spencer
Julian Ray wrote:
> Hi Paul,
>
> We use JFreeCharts by creating a servlet which serves up the charts and then
> creating a URL in the backingbean which is set as the value for the
> graphicImage
>
> Eg <h:graphicImage styleClass="..." value="#{myBackingBean.chartURL}" />
>
> -----Original Message-----
> From: Paul Spencer [mailto:paulsp@apache.org]
> Sent: Wednesday, June 28, 2006 11:14 PM
> To: MyFaces Discussion
> Subject: How to displayi an images generated by a managed bean?
>
> I need to include a graphic image generated by JFreeCharts. Currently the
> generation of the image is in an action of a managed bean and is displayed
> in a separate windows via <h:commandButton>. How should I convert this to a
> <h:graphicImage> so it can be displayed in an existing page?
>
> Paul Spencer
>
>
Re: How to displayi an images generated by a managed bean?
Posted by Paul Spencer <pa...@apache.org>.
Cagatay,
I have not ruled anything out. I am still looking at the options. In
general I like what I see in JSF-COMP.
JSF_COMP appears to be a very simple way to embed charts!
Paul Spencer
Cagatay Civici wrote:
> Hi,
>
> Why not use the JSF Chart Creator of JSF-COMP?
>
> It does not use a servlet, instead a phaselistener to render the charts so
> there is no need to to do any configuration in the application.
>
> Cagatay
>
> On 6/29/06, Julian Ray <ju...@yahoo.com> wrote:
>>
>> Sure.
>>
>> First you need to be able to access the faces context from a non-faces
>> servlet. I posted some info on the WIKI about how to get the faces
>> context
>> (and therefor all session values and access to managed beans) within a
>> servlet. Take a look at
>> http://wiki.apache.org/myfaces/AccessFacesContextFromServlet.
>>
>> Our charting servlet uses simple get params to control the chart -- as
>> this
>> is "visible" to the outside world we only pass params which allow us to
>> get
>> the appropriate managed bean and session properties from within faces
>> context. So in the doGet() we call appropriate managed beans and
>> JFreeChart
>> methods to creat and return the chart. This is standard servlet stuff
>> (with
>> the myfaces context added). Included code below
>>
>> protected void processRequest(HttpServletRequest request,
>> HttpServletResponse response) throws ServletException, IOException {
>> FacesContext facesContext = getFacesContext(request, response);
>> // Here we get whatever we need from the faces context.
>> SessionBean sessionBean = (SessionBean)
>> getSessionBean(facesContext);
>>
>> if (null == sessionBean) {
>> log.debug("Session bean is null.");
>> } else {
>> log.debug("Got session bean. Userid=" +
>> sessionBean.getUserId());
>> }
>>
>> // Get the chart type to process from the URL
>> AbstractChart chart = null;
>> Integer chartType =
>> NumberUtils.createInteger(request.getParameter("type").trim());
>>
>> log.debug("Chart Type = '" + chartType + "'");
>>
>> // Here we do whatever needed to create the chart...
>> String className = ChartType.getChartClassName(chartType);
>> log.debug("Creating chart object for class '" + className + "'");
>>
>> String reportTitle = request.getParameter("til");
>> log.debug("Chart Title = '" + reportTitle + "'");
>>
>> String reportSubTitle = request.getParameter("sub");
>> log.debug("Chart Sub Title = '" + reportSubTitle + "'");
>>
>> // See if the chart is in the cache. The servlet is
>> single-threaded
>> and reuasable which means
>> /// that a new chart map is created for each instance of a chart.
>> Charts should be serializable
>> // and not store state in any way
>> if (map.containsKey(className)) {
>> chart = (AbstractChart) map.get(className);
>> } else {
>> // Use reflection to get the chart. Note that is must have a
>> no-args constructor
>> try {
>> chart = (AbstractChart)
>> Class.forName(className).newInstance();
>> // Push the chart to the map.
>> map.put(className, chart);
>> } catch (ClassNotFoundException e) {
>> log.debug(e.getMessage());
>> throw new IOException(e.getMessage());
>> } catch (InstantiationException e) {
>> log.debug(e.getMessage());
>> throw new IOException(e.getMessage());
>> } catch (IllegalAccessException e) {
>> log.debug(e.getMessage());
>> throw new IOException(e.getMessage());
>> } catch (IllegalArgumentException e) {
>> log.debug(e.getMessage());
>> throw new IOException(e.getMessage());
>> }
>> }
>>
>> // Chart setup here
>> if (null == chart) {
>> log.error("Chart # " + chartType + " not found on server.");
>> response.setContentType("text/html");
>>
>> ServletOutputStream outputStream =
>> response.getOutputStream();
>> outputStream.println("Chart not found on server.");
>> outputStream.flush();
>> outputStream.close();
>> } else {
>> // Populate the chart so it can access the params
>> chart.setRequest(request);
>> chart.setSessionBean(sessionBean);
>>
>> // Process the chart
>> JFreeChart jfreeChart = chart.getChart();
>> // get the image
>> response.setContentType("image/png");
>> ChartUtilities.writeChartAsPNG(response.getOutputStream(),
>> jfreeChart, chart.getChartWidth(), chart.getChartHeight());
>> }
>> }
>>
>> Our JSF backing bean constructs a url based on options selected by the
>> user.
>> This url is passed to the <h:graphicImage> tag. The form of the URL is
>> /myContextRoot/myChartServlet?param1=value1¶m2=value2 etc.
>>
>> In the backing bean we have
>>
>> Public String getChartURL() {
>> StringBuffer sb = new StringBuffer();
>> sb.append("/myContextRoot/myChartServlet");
>> //sb.append(.....) etc...
>> return sb.toString();
>> }
>> and in the JSF we have
>>
>> <h:graphicImage styleClass="whatever..."
>> value="#{myBackingBean.chartURL}"
>> />
>>
>> Our servlet implementation loads chart classes as the servlet is
>> initialized
>> which speeds up the servlet once it is "warmed up".
>>
>> One more note, While this approach works well found that it is more
>> convienient to create a custom component to provide the JSF interface as
>> we
>> use charts on a large number of our pages and it fits the MVC model
>> better
>> to let the JSF fuly control the format and display of the greaphic.
>>
>> I hope this helps.
>>
>>
>>
>> Example
>>
>> -----Original Message-----
>> From: Paul Spencer [mailto:paulsp@apache.org]
>> Sent: Thursday, June 29, 2006 7:55 AM
>> To: MyFaces Discussion
>> Subject: Re: How to displayi an images generated by a managed bean?
>>
>> Julian,
>> Can you explain the code and configuration behind
>> #{myBackingBean.chartURL
>> }
>> and the servlet you have created?
>>
>> I need to understand where the graphic is created, i.e. in the managed
>> bean
>> or servlet, how the servlet gets what is needs, and any housekeeping that
>> is
>> performed.
>>
>>
>> Paul Spencer
>>
>> Julian Ray wrote:
>> > Hi Paul,
>> >
>> > We use JFreeCharts by creating a servlet which serves up the charts
>> > and then creating a URL in the backingbean which is set as the value
>> > for the graphicImage
>> >
>> > Eg <h:graphicImage styleClass="..." value="#{myBackingBean.chartURL}"
>> > />
>> >
>> > -----Original Message-----
>> > From: Paul Spencer [mailto:paulsp@apache.org]
>> > Sent: Wednesday, June 28, 2006 11:14 PM
>> > To: MyFaces Discussion
>> > Subject: How to displayi an images generated by a managed bean?
>> >
>> > I need to include a graphic image generated by JFreeCharts. Currently
>> > the generation of the image is in an action of a managed bean and is
>> > displayed in a separate windows via <h:commandButton>. How should I
>> > convert this to a <h:graphicImage> so it can be displayed in an
>> existing
>> page?
>> >
>> > Paul Spencer
>> >
>> >
>>
>>
>>
>
Re: How to displayi an images generated by a managed bean?
Posted by Cagatay Civici <ca...@gmail.com>.
Hi,
FYI The 1.2.0 RC1 is released at jsf-comp.
Here is the changelog;
http://www.jroller.com/page/cagataycivici?entry=jsf_charting_monster_is_back
Cheers,
Cagatay
On 6/29/06, Paul Spencer <pa...@mindspring.com> wrote:
>
> Cagatay,
> I have not ruled anything out. I am still looking at the options. In
> general I like what I see in JSF-COMP.
>
> JSF_COMP appears to be a very simple way to embed charts!
>
> Paul Spencer
>
>
> Cagatay Civici wrote:
> > Hi,
> >
> > Why not use the JSF Chart Creator of JSF-COMP?
> >
> > It does not use a servlet, instead a phaselistener to render the charts
> so
> > there is no need to to do any configuration in the application.
> >
> > Cagatay
> >
> > On 6/29/06, Julian Ray <ju...@yahoo.com> wrote:
> >>
> >> Sure.
> >>
> >> First you need to be able to access the faces context from a non-faces
> >> servlet. I posted some info on the WIKI about how to get the faces
> >> context
> >> (and therefor all session values and access to managed beans) within a
> >> servlet. Take a look at
> >> http://wiki.apache.org/myfaces/AccessFacesContextFromServlet.
> >>
> >> Our charting servlet uses simple get params to control the chart -- as
> >> this
> >> is "visible" to the outside world we only pass params which allow us to
> >> get
> >> the appropriate managed bean and session properties from within faces
> >> context. So in the doGet() we call appropriate managed beans and
> >> JFreeChart
> >> methods to creat and return the chart. This is standard servlet stuff
> >> (with
> >> the myfaces context added). Included code below
> >>
> >> protected void processRequest(HttpServletRequest request,
> >> HttpServletResponse response) throws ServletException, IOException {
> >> FacesContext facesContext = getFacesContext(request, response);
> >> // Here we get whatever we need from the faces context.
> >> SessionBean sessionBean = (SessionBean)
> >> getSessionBean(facesContext);
> >>
> >> if (null == sessionBean) {
> >> log.debug("Session bean is null.");
> >> } else {
> >> log.debug("Got session bean. Userid=" +
> >> sessionBean.getUserId());
> >> }
> >>
> >> // Get the chart type to process from the URL
> >> AbstractChart chart = null;
> >> Integer chartType =
> >> NumberUtils.createInteger(request.getParameter("type").trim());
> >>
> >> log.debug("Chart Type = '" + chartType + "'");
> >>
> >> // Here we do whatever needed to create the chart...
> >> String className = ChartType.getChartClassName(chartType);
> >> log.debug("Creating chart object for class '" + className +
> "'");
> >>
> >> String reportTitle = request.getParameter("til");
> >> log.debug("Chart Title = '" + reportTitle + "'");
> >>
> >> String reportSubTitle = request.getParameter("sub");
> >> log.debug("Chart Sub Title = '" + reportSubTitle + "'");
> >>
> >> // See if the chart is in the cache. The servlet is
> >> single-threaded
> >> and reuasable which means
> >> /// that a new chart map is created for each instance of a
> chart.
> >> Charts should be serializable
> >> // and not store state in any way
> >> if (map.containsKey(className)) {
> >> chart = (AbstractChart) map.get(className);
> >> } else {
> >> // Use reflection to get the chart. Note that is must have
> a
> >> no-args constructor
> >> try {
> >> chart = (AbstractChart)
> >> Class.forName(className).newInstance();
> >> // Push the chart to the map.
> >> map.put(className, chart);
> >> } catch (ClassNotFoundException e) {
> >> log.debug(e.getMessage());
> >> throw new IOException(e.getMessage());
> >> } catch (InstantiationException e) {
> >> log.debug(e.getMessage());
> >> throw new IOException(e.getMessage());
> >> } catch (IllegalAccessException e) {
> >> log.debug(e.getMessage());
> >> throw new IOException(e.getMessage());
> >> } catch (IllegalArgumentException e) {
> >> log.debug(e.getMessage());
> >> throw new IOException(e.getMessage());
> >> }
> >> }
> >>
> >> // Chart setup here
> >> if (null == chart) {
> >> log.error("Chart # " + chartType + " not found on
> server.");
> >> response.setContentType("text/html");
> >>
> >> ServletOutputStream outputStream =
> >> response.getOutputStream();
> >> outputStream.println("Chart not found on server.");
> >> outputStream.flush();
> >> outputStream.close();
> >> } else {
> >> // Populate the chart so it can access the params
> >> chart.setRequest(request);
> >> chart.setSessionBean(sessionBean);
> >>
> >> // Process the chart
> >> JFreeChart jfreeChart = chart.getChart();
> >> // get the image
> >> response.setContentType("image/png");
> >> ChartUtilities.writeChartAsPNG(response.getOutputStream(),
> >> jfreeChart, chart.getChartWidth(), chart.getChartHeight());
> >> }
> >> }
> >>
> >> Our JSF backing bean constructs a url based on options selected by the
> >> user.
> >> This url is passed to the <h:graphicImage> tag. The form of the URL is
> >> /myContextRoot/myChartServlet?param1=value1¶m2=value2 etc.
> >>
> >> In the backing bean we have
> >>
> >> Public String getChartURL() {
> >> StringBuffer sb = new StringBuffer();
> >> sb.append("/myContextRoot/myChartServlet");
> >> //sb.append(.....) etc...
> >> return sb.toString();
> >> }
> >> and in the JSF we have
> >>
> >> <h:graphicImage styleClass="whatever..."
> >> value="#{myBackingBean.chartURL}"
> >> />
> >>
> >> Our servlet implementation loads chart classes as the servlet is
> >> initialized
> >> which speeds up the servlet once it is "warmed up".
> >>
> >> One more note, While this approach works well found that it is more
> >> convienient to create a custom component to provide the JSF interface
> as
> >> we
> >> use charts on a large number of our pages and it fits the MVC model
> >> better
> >> to let the JSF fuly control the format and display of the greaphic.
> >>
> >> I hope this helps.
> >>
> >>
> >>
> >> Example
> >>
> >> -----Original Message-----
> >> From: Paul Spencer [mailto:paulsp@apache.org]
> >> Sent: Thursday, June 29, 2006 7:55 AM
> >> To: MyFaces Discussion
> >> Subject: Re: How to displayi an images generated by a managed bean?
> >>
> >> Julian,
> >> Can you explain the code and configuration behind
> >> #{myBackingBean.chartURL
> >> }
> >> and the servlet you have created?
> >>
> >> I need to understand where the graphic is created, i.e. in the managed
> >> bean
> >> or servlet, how the servlet gets what is needs, and any housekeeping
> that
> >> is
> >> performed.
> >>
> >>
> >> Paul Spencer
> >>
> >> Julian Ray wrote:
> >> > Hi Paul,
> >> >
> >> > We use JFreeCharts by creating a servlet which serves up the charts
> >> > and then creating a URL in the backingbean which is set as the value
> >> > for the graphicImage
> >> >
> >> > Eg <h:graphicImage styleClass="..." value="#{myBackingBean.chartURL}"
> >> > />
> >> >
> >> > -----Original Message-----
> >> > From: Paul Spencer [mailto:paulsp@apache.org]
> >> > Sent: Wednesday, June 28, 2006 11:14 PM
> >> > To: MyFaces Discussion
> >> > Subject: How to displayi an images generated by a managed bean?
> >> >
> >> > I need to include a graphic image generated by
> JFreeCharts. Currently
> >> > the generation of the image is in an action of a managed bean and is
> >> > displayed in a separate windows via <h:commandButton>. How should I
> >> > convert this to a <h:graphicImage> so it can be displayed in an
> >> existing
> >> page?
> >> >
> >> > Paul Spencer
> >> >
> >> >
> >>
> >>
> >>
> >
>
>
Re: How to displayi an images generated by a managed bean?
Posted by Paul Spencer <pa...@mindspring.com>.
Cagatay,
I have not ruled anything out. I am still looking at the options. In
general I like what I see in JSF-COMP.
JSF_COMP appears to be a very simple way to embed charts!
Paul Spencer
Cagatay Civici wrote:
> Hi,
>
> Why not use the JSF Chart Creator of JSF-COMP?
>
> It does not use a servlet, instead a phaselistener to render the charts so
> there is no need to to do any configuration in the application.
>
> Cagatay
>
> On 6/29/06, Julian Ray <ju...@yahoo.com> wrote:
>>
>> Sure.
>>
>> First you need to be able to access the faces context from a non-faces
>> servlet. I posted some info on the WIKI about how to get the faces
>> context
>> (and therefor all session values and access to managed beans) within a
>> servlet. Take a look at
>> http://wiki.apache.org/myfaces/AccessFacesContextFromServlet.
>>
>> Our charting servlet uses simple get params to control the chart -- as
>> this
>> is "visible" to the outside world we only pass params which allow us to
>> get
>> the appropriate managed bean and session properties from within faces
>> context. So in the doGet() we call appropriate managed beans and
>> JFreeChart
>> methods to creat and return the chart. This is standard servlet stuff
>> (with
>> the myfaces context added). Included code below
>>
>> protected void processRequest(HttpServletRequest request,
>> HttpServletResponse response) throws ServletException, IOException {
>> FacesContext facesContext = getFacesContext(request, response);
>> // Here we get whatever we need from the faces context.
>> SessionBean sessionBean = (SessionBean)
>> getSessionBean(facesContext);
>>
>> if (null == sessionBean) {
>> log.debug("Session bean is null.");
>> } else {
>> log.debug("Got session bean. Userid=" +
>> sessionBean.getUserId());
>> }
>>
>> // Get the chart type to process from the URL
>> AbstractChart chart = null;
>> Integer chartType =
>> NumberUtils.createInteger(request.getParameter("type").trim());
>>
>> log.debug("Chart Type = '" + chartType + "'");
>>
>> // Here we do whatever needed to create the chart...
>> String className = ChartType.getChartClassName(chartType);
>> log.debug("Creating chart object for class '" + className + "'");
>>
>> String reportTitle = request.getParameter("til");
>> log.debug("Chart Title = '" + reportTitle + "'");
>>
>> String reportSubTitle = request.getParameter("sub");
>> log.debug("Chart Sub Title = '" + reportSubTitle + "'");
>>
>> // See if the chart is in the cache. The servlet is
>> single-threaded
>> and reuasable which means
>> /// that a new chart map is created for each instance of a chart.
>> Charts should be serializable
>> // and not store state in any way
>> if (map.containsKey(className)) {
>> chart = (AbstractChart) map.get(className);
>> } else {
>> // Use reflection to get the chart. Note that is must have a
>> no-args constructor
>> try {
>> chart = (AbstractChart)
>> Class.forName(className).newInstance();
>> // Push the chart to the map.
>> map.put(className, chart);
>> } catch (ClassNotFoundException e) {
>> log.debug(e.getMessage());
>> throw new IOException(e.getMessage());
>> } catch (InstantiationException e) {
>> log.debug(e.getMessage());
>> throw new IOException(e.getMessage());
>> } catch (IllegalAccessException e) {
>> log.debug(e.getMessage());
>> throw new IOException(e.getMessage());
>> } catch (IllegalArgumentException e) {
>> log.debug(e.getMessage());
>> throw new IOException(e.getMessage());
>> }
>> }
>>
>> // Chart setup here
>> if (null == chart) {
>> log.error("Chart # " + chartType + " not found on server.");
>> response.setContentType("text/html");
>>
>> ServletOutputStream outputStream =
>> response.getOutputStream();
>> outputStream.println("Chart not found on server.");
>> outputStream.flush();
>> outputStream.close();
>> } else {
>> // Populate the chart so it can access the params
>> chart.setRequest(request);
>> chart.setSessionBean(sessionBean);
>>
>> // Process the chart
>> JFreeChart jfreeChart = chart.getChart();
>> // get the image
>> response.setContentType("image/png");
>> ChartUtilities.writeChartAsPNG(response.getOutputStream(),
>> jfreeChart, chart.getChartWidth(), chart.getChartHeight());
>> }
>> }
>>
>> Our JSF backing bean constructs a url based on options selected by the
>> user.
>> This url is passed to the <h:graphicImage> tag. The form of the URL is
>> /myContextRoot/myChartServlet?param1=value1¶m2=value2 etc.
>>
>> In the backing bean we have
>>
>> Public String getChartURL() {
>> StringBuffer sb = new StringBuffer();
>> sb.append("/myContextRoot/myChartServlet");
>> //sb.append(.....) etc...
>> return sb.toString();
>> }
>> and in the JSF we have
>>
>> <h:graphicImage styleClass="whatever..."
>> value="#{myBackingBean.chartURL}"
>> />
>>
>> Our servlet implementation loads chart classes as the servlet is
>> initialized
>> which speeds up the servlet once it is "warmed up".
>>
>> One more note, While this approach works well found that it is more
>> convienient to create a custom component to provide the JSF interface as
>> we
>> use charts on a large number of our pages and it fits the MVC model
>> better
>> to let the JSF fuly control the format and display of the greaphic.
>>
>> I hope this helps.
>>
>>
>>
>> Example
>>
>> -----Original Message-----
>> From: Paul Spencer [mailto:paulsp@apache.org]
>> Sent: Thursday, June 29, 2006 7:55 AM
>> To: MyFaces Discussion
>> Subject: Re: How to displayi an images generated by a managed bean?
>>
>> Julian,
>> Can you explain the code and configuration behind
>> #{myBackingBean.chartURL
>> }
>> and the servlet you have created?
>>
>> I need to understand where the graphic is created, i.e. in the managed
>> bean
>> or servlet, how the servlet gets what is needs, and any housekeeping that
>> is
>> performed.
>>
>>
>> Paul Spencer
>>
>> Julian Ray wrote:
>> > Hi Paul,
>> >
>> > We use JFreeCharts by creating a servlet which serves up the charts
>> > and then creating a URL in the backingbean which is set as the value
>> > for the graphicImage
>> >
>> > Eg <h:graphicImage styleClass="..." value="#{myBackingBean.chartURL}"
>> > />
>> >
>> > -----Original Message-----
>> > From: Paul Spencer [mailto:paulsp@apache.org]
>> > Sent: Wednesday, June 28, 2006 11:14 PM
>> > To: MyFaces Discussion
>> > Subject: How to displayi an images generated by a managed bean?
>> >
>> > I need to include a graphic image generated by JFreeCharts. Currently
>> > the generation of the image is in an action of a managed bean and is
>> > displayed in a separate windows via <h:commandButton>. How should I
>> > convert this to a <h:graphicImage> so it can be displayed in an
>> existing
>> page?
>> >
>> > Paul Spencer
>> >
>> >
>>
>>
>>
>
Re: How to displayi an images generated by a managed bean?
Posted by Cagatay Civici <ca...@gmail.com>.
Hi,
Why not use the JSF Chart Creator of JSF-COMP?
It does not use a servlet, instead a phaselistener to render the charts so
there is no need to to do any configuration in the application.
Cagatay
On 6/29/06, Julian Ray <ju...@yahoo.com> wrote:
>
> Sure.
>
> First you need to be able to access the faces context from a non-faces
> servlet. I posted some info on the WIKI about how to get the faces context
> (and therefor all session values and access to managed beans) within a
> servlet. Take a look at
> http://wiki.apache.org/myfaces/AccessFacesContextFromServlet.
>
> Our charting servlet uses simple get params to control the chart -- as
> this
> is "visible" to the outside world we only pass params which allow us to
> get
> the appropriate managed bean and session properties from within faces
> context. So in the doGet() we call appropriate managed beans and
> JFreeChart
> methods to creat and return the chart. This is standard servlet stuff
> (with
> the myfaces context added). Included code below
>
> protected void processRequest(HttpServletRequest request,
> HttpServletResponse response) throws ServletException, IOException {
> FacesContext facesContext = getFacesContext(request, response);
> // Here we get whatever we need from the faces context.
> SessionBean sessionBean = (SessionBean)
> getSessionBean(facesContext);
>
> if (null == sessionBean) {
> log.debug("Session bean is null.");
> } else {
> log.debug("Got session bean. Userid=" +
> sessionBean.getUserId());
> }
>
> // Get the chart type to process from the URL
> AbstractChart chart = null;
> Integer chartType =
> NumberUtils.createInteger(request.getParameter("type").trim());
>
> log.debug("Chart Type = '" + chartType + "'");
>
> // Here we do whatever needed to create the chart...
> String className = ChartType.getChartClassName(chartType);
> log.debug("Creating chart object for class '" + className + "'");
>
> String reportTitle = request.getParameter("til");
> log.debug("Chart Title = '" + reportTitle + "'");
>
> String reportSubTitle = request.getParameter("sub");
> log.debug("Chart Sub Title = '" + reportSubTitle + "'");
>
> // See if the chart is in the cache. The servlet is
> single-threaded
> and reuasable which means
> /// that a new chart map is created for each instance of a chart.
> Charts should be serializable
> // and not store state in any way
> if (map.containsKey(className)) {
> chart = (AbstractChart) map.get(className);
> } else {
> // Use reflection to get the chart. Note that is must have a
> no-args constructor
> try {
> chart = (AbstractChart)
> Class.forName(className).newInstance();
> // Push the chart to the map.
> map.put(className, chart);
> } catch (ClassNotFoundException e) {
> log.debug(e.getMessage());
> throw new IOException(e.getMessage());
> } catch (InstantiationException e) {
> log.debug(e.getMessage());
> throw new IOException(e.getMessage());
> } catch (IllegalAccessException e) {
> log.debug(e.getMessage());
> throw new IOException(e.getMessage());
> } catch (IllegalArgumentException e) {
> log.debug(e.getMessage());
> throw new IOException(e.getMessage());
> }
> }
>
> // Chart setup here
> if (null == chart) {
> log.error("Chart # " + chartType + " not found on server.");
> response.setContentType("text/html");
>
> ServletOutputStream outputStream = response.getOutputStream();
> outputStream.println("Chart not found on server.");
> outputStream.flush();
> outputStream.close();
> } else {
> // Populate the chart so it can access the params
> chart.setRequest(request);
> chart.setSessionBean(sessionBean);
>
> // Process the chart
> JFreeChart jfreeChart = chart.getChart();
> // get the image
> response.setContentType("image/png");
> ChartUtilities.writeChartAsPNG(response.getOutputStream(),
> jfreeChart, chart.getChartWidth(), chart.getChartHeight());
> }
> }
>
> Our JSF backing bean constructs a url based on options selected by the
> user.
> This url is passed to the <h:graphicImage> tag. The form of the URL is
> /myContextRoot/myChartServlet?param1=value1¶m2=value2 etc.
>
> In the backing bean we have
>
> Public String getChartURL() {
> StringBuffer sb = new StringBuffer();
> sb.append("/myContextRoot/myChartServlet");
> //sb.append(.....) etc...
> return sb.toString();
> }
> and in the JSF we have
>
> <h:graphicImage styleClass="whatever..." value="#{myBackingBean.chartURL}"
> />
>
> Our servlet implementation loads chart classes as the servlet is
> initialized
> which speeds up the servlet once it is "warmed up".
>
> One more note, While this approach works well found that it is more
> convienient to create a custom component to provide the JSF interface as
> we
> use charts on a large number of our pages and it fits the MVC model better
> to let the JSF fuly control the format and display of the greaphic.
>
> I hope this helps.
>
>
>
> Example
>
> -----Original Message-----
> From: Paul Spencer [mailto:paulsp@apache.org]
> Sent: Thursday, June 29, 2006 7:55 AM
> To: MyFaces Discussion
> Subject: Re: How to displayi an images generated by a managed bean?
>
> Julian,
> Can you explain the code and configuration behind #{myBackingBean.chartURL
> }
> and the servlet you have created?
>
> I need to understand where the graphic is created, i.e. in the managed
> bean
> or servlet, how the servlet gets what is needs, and any housekeeping that
> is
> performed.
>
>
> Paul Spencer
>
> Julian Ray wrote:
> > Hi Paul,
> >
> > We use JFreeCharts by creating a servlet which serves up the charts
> > and then creating a URL in the backingbean which is set as the value
> > for the graphicImage
> >
> > Eg <h:graphicImage styleClass="..." value="#{myBackingBean.chartURL}"
> > />
> >
> > -----Original Message-----
> > From: Paul Spencer [mailto:paulsp@apache.org]
> > Sent: Wednesday, June 28, 2006 11:14 PM
> > To: MyFaces Discussion
> > Subject: How to displayi an images generated by a managed bean?
> >
> > I need to include a graphic image generated by JFreeCharts. Currently
> > the generation of the image is in an action of a managed bean and is
> > displayed in a separate windows via <h:commandButton>. How should I
> > convert this to a <h:graphicImage> so it can be displayed in an existing
> page?
> >
> > Paul Spencer
> >
> >
>
>
>
Re: How to displayi an images generated by a managed bean?
Posted by Paul Spencer <pa...@apache.org>.
Julian,
Although my question was about images, I am looking for a generalized
solution that can be used with many type of files, so your "custom
component api" sounds very interesting.
Can you expand on the "custom component api"?
If you would rather continue this offline, you may email me directly.
Paul Spencer
Julian Ray wrote:
> Hmmm, let see...
> as many of our charts require different params, as well as some core params
> to be sent, we naturally end up duplicating a lot of code in the backing
> beans. Even refactoring this code into base classes did little to help. This
> duplicated code has as couple of negatives: [1] its duplicated, therefore,
> inefficient for many reasons, maintenance not the least among them, [2] the
> URLs and servlets we create need to be on the same lifecycle which is not
> necessarily the same as the JSF pages and backing beans -- we reuse the
> servlet across several JSF services, and [3] as we extend the charting
> functionality there is a lot of unnecessary testing and code changes which
> is minimized if the backing beans work against the custom component api.
>
> Lastly, its cleaner and much more JSF-ish :)
>
> <professorial soap-box rant>
>
> Typically, as in most designs, if your problem is not too complex, a simple
> solution will work fine (eg servlet and URL). As the problem complexity
> increases, however, more elegant solutions are necessary hence the custom
> component. Indeed we started with backing bean URLs then migrated to a
> custom component during the course of several code refactoring phases - a
> pattern we repeated for JasperReports and other functionality as well.
>
> To address Cagatay's comments:
>
> [1] A phase listener would probably be preferable to a servlet and I intend
> to migrate (or at least benchmark) a phase listener to replace our servlet.
> However, often when beginning with a new technology staying within the
> bounds of our experience at, least initially, can be beneficial, especially
> when using an agile approach to development where speed of development and
> delivered functionality are driving forces. Basically, if you know how to
> code servlets and are comfortable coding servlets, use a servlet and when it
> all works migrate to something better but get it to work first.
>
> [2] I have not yet tried the JSF-Chart Creator in JSF-COMP although I have
> heard good things about it and fully intend to take a look. However, this
> goes back to my initial rant and if JSF-COMP does everything you need (at
> least for now) why not use it -- it was not available when we started our
> current build cycle :).
> </professorial soap-box rant>
>
>
> -----Original Message-----
> From: Paul Spencer [mailto:paulsp@apache.org]
> Sent: Thursday, June 29, 2006 9:51 AM
> To: MyFaces Discussion
> Subject: Re: How to displayi an images generated by a managed bean?
>
> Julian,
> Can you expand on this?
>
>> One more note, While this approach works well found that it is more
>> convienient to create a custom component to provide the JSF interface
>> as we use charts on a large number of our pages and it fits the MVC
>> model better to let the JSF fuly control the format and display of the
> greaphic.
>
>
> Your responses have been very helpful.
>
> Paul Spencer
>
>
RE: How to displayi an images generated by a managed bean?
Posted by Julian Ray <ju...@yahoo.com>.
Hmmm, let see...
as many of our charts require different params, as well as some core params
to be sent, we naturally end up duplicating a lot of code in the backing
beans. Even refactoring this code into base classes did little to help. This
duplicated code has as couple of negatives: [1] its duplicated, therefore,
inefficient for many reasons, maintenance not the least among them, [2] the
URLs and servlets we create need to be on the same lifecycle which is not
necessarily the same as the JSF pages and backing beans -- we reuse the
servlet across several JSF services, and [3] as we extend the charting
functionality there is a lot of unnecessary testing and code changes which
is minimized if the backing beans work against the custom component api.
Lastly, its cleaner and much more JSF-ish :)
<professorial soap-box rant>
Typically, as in most designs, if your problem is not too complex, a simple
solution will work fine (eg servlet and URL). As the problem complexity
increases, however, more elegant solutions are necessary hence the custom
component. Indeed we started with backing bean URLs then migrated to a
custom component during the course of several code refactoring phases - a
pattern we repeated for JasperReports and other functionality as well.
To address Cagatay's comments:
[1] A phase listener would probably be preferable to a servlet and I intend
to migrate (or at least benchmark) a phase listener to replace our servlet.
However, often when beginning with a new technology staying within the
bounds of our experience at, least initially, can be beneficial, especially
when using an agile approach to development where speed of development and
delivered functionality are driving forces. Basically, if you know how to
code servlets and are comfortable coding servlets, use a servlet and when it
all works migrate to something better but get it to work first.
[2] I have not yet tried the JSF-Chart Creator in JSF-COMP although I have
heard good things about it and fully intend to take a look. However, this
goes back to my initial rant and if JSF-COMP does everything you need (at
least for now) why not use it -- it was not available when we started our
current build cycle :).
</professorial soap-box rant>
-----Original Message-----
From: Paul Spencer [mailto:paulsp@apache.org]
Sent: Thursday, June 29, 2006 9:51 AM
To: MyFaces Discussion
Subject: Re: How to displayi an images generated by a managed bean?
Julian,
Can you expand on this?
> One more note, While this approach works well found that it is more
> convienient to create a custom component to provide the JSF interface
> as we use charts on a large number of our pages and it fits the MVC
> model better to let the JSF fuly control the format and display of the
greaphic.
>
Your responses have been very helpful.
Paul Spencer
Re: How to displayi an images generated by a managed bean?
Posted by Paul Spencer <pa...@apache.org>.
Julian,
Can you expand on this?
> One more note, While this approach works well found that it is more
> convienient to create a custom component to provide the JSF interface as we
> use charts on a large number of our pages and it fits the MVC model better
> to let the JSF fuly control the format and display of the greaphic.
>
Your responses have been very helpful.
Paul Spencer
RE: How to displayi an images generated by a managed bean?
Posted by Julian Ray <ju...@yahoo.com>.
Sure.
First you need to be able to access the faces context from a non-faces
servlet. I posted some info on the WIKI about how to get the faces context
(and therefor all session values and access to managed beans) within a
servlet. Take a look at
http://wiki.apache.org/myfaces/AccessFacesContextFromServlet.
Our charting servlet uses simple get params to control the chart -- as this
is "visible" to the outside world we only pass params which allow us to get
the appropriate managed bean and session properties from within faces
context. So in the doGet() we call appropriate managed beans and JFreeChart
methods to creat and return the chart. This is standard servlet stuff (with
the myfaces context added). Included code below
protected void processRequest(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
FacesContext facesContext = getFacesContext(request, response);
// Here we get whatever we need from the faces context.
SessionBean sessionBean = (SessionBean)
getSessionBean(facesContext);
if (null == sessionBean) {
log.debug("Session bean is null.");
} else {
log.debug("Got session bean. Userid=" +
sessionBean.getUserId());
}
// Get the chart type to process from the URL
AbstractChart chart = null;
Integer chartType =
NumberUtils.createInteger(request.getParameter("type").trim());
log.debug("Chart Type = '" + chartType + "'");
// Here we do whatever needed to create the chart...
String className = ChartType.getChartClassName(chartType);
log.debug("Creating chart object for class '" + className + "'");
String reportTitle = request.getParameter("til");
log.debug("Chart Title = '" + reportTitle + "'");
String reportSubTitle = request.getParameter("sub");
log.debug("Chart Sub Title = '" + reportSubTitle + "'");
// See if the chart is in the cache. The servlet is single-threaded
and reuasable which means
/// that a new chart map is created for each instance of a chart.
Charts should be serializable
// and not store state in any way
if (map.containsKey(className)) {
chart = (AbstractChart) map.get(className);
} else {
// Use reflection to get the chart. Note that is must have a
no-args constructor
try {
chart = (AbstractChart)
Class.forName(className).newInstance();
// Push the chart to the map.
map.put(className, chart);
} catch (ClassNotFoundException e) {
log.debug(e.getMessage());
throw new IOException(e.getMessage());
} catch (InstantiationException e) {
log.debug(e.getMessage());
throw new IOException(e.getMessage());
} catch (IllegalAccessException e) {
log.debug(e.getMessage());
throw new IOException(e.getMessage());
} catch (IllegalArgumentException e) {
log.debug(e.getMessage());
throw new IOException(e.getMessage());
}
}
// Chart setup here
if (null == chart) {
log.error("Chart # " + chartType + " not found on server.");
response.setContentType("text/html");
ServletOutputStream outputStream = response.getOutputStream();
outputStream.println("Chart not found on server.");
outputStream.flush();
outputStream.close();
} else {
// Populate the chart so it can access the params
chart.setRequest(request);
chart.setSessionBean(sessionBean);
// Process the chart
JFreeChart jfreeChart = chart.getChart();
// get the image
response.setContentType("image/png");
ChartUtilities.writeChartAsPNG(response.getOutputStream(),
jfreeChart, chart.getChartWidth(), chart.getChartHeight());
}
}
Our JSF backing bean constructs a url based on options selected by the user.
This url is passed to the <h:graphicImage> tag. The form of the URL is
/myContextRoot/myChartServlet?param1=value1¶m2=value2 etc.
In the backing bean we have
Public String getChartURL() {
StringBuffer sb = new StringBuffer();
sb.append("/myContextRoot/myChartServlet");
//sb.append(.....) etc...
return sb.toString();
}
and in the JSF we have
<h:graphicImage styleClass="whatever..." value="#{myBackingBean.chartURL}"
/>
Our servlet implementation loads chart classes as the servlet is initialized
which speeds up the servlet once it is "warmed up".
One more note, While this approach works well found that it is more
convienient to create a custom component to provide the JSF interface as we
use charts on a large number of our pages and it fits the MVC model better
to let the JSF fuly control the format and display of the greaphic.
I hope this helps.
Example
-----Original Message-----
From: Paul Spencer [mailto:paulsp@apache.org]
Sent: Thursday, June 29, 2006 7:55 AM
To: MyFaces Discussion
Subject: Re: How to displayi an images generated by a managed bean?
Julian,
Can you explain the code and configuration behind #{myBackingBean.chartURL}
and the servlet you have created?
I need to understand where the graphic is created, i.e. in the managed bean
or servlet, how the servlet gets what is needs, and any housekeeping that is
performed.
Paul Spencer
Julian Ray wrote:
> Hi Paul,
>
> We use JFreeCharts by creating a servlet which serves up the charts
> and then creating a URL in the backingbean which is set as the value
> for the graphicImage
>
> Eg <h:graphicImage styleClass="..." value="#{myBackingBean.chartURL}"
> />
>
> -----Original Message-----
> From: Paul Spencer [mailto:paulsp@apache.org]
> Sent: Wednesday, June 28, 2006 11:14 PM
> To: MyFaces Discussion
> Subject: How to displayi an images generated by a managed bean?
>
> I need to include a graphic image generated by JFreeCharts. Currently
> the generation of the image is in an action of a managed bean and is
> displayed in a separate windows via <h:commandButton>. How should I
> convert this to a <h:graphicImage> so it can be displayed in an existing
page?
>
> Paul Spencer
>
>
Re: How to displayi an images generated by a managed bean?
Posted by Paul Spencer <pa...@apache.org>.
Julian,
Can you explain the code and configuration behind
#{myBackingBean.chartURL} and the servlet you have created?
I need to understand where the graphic is created, i.e. in the managed
bean or servlet, how the servlet gets what is needs, and any
housekeeping that is performed.
Paul Spencer
Julian Ray wrote:
> Hi Paul,
>
> We use JFreeCharts by creating a servlet which serves up the charts and then
> creating a URL in the backingbean which is set as the value for the
> graphicImage
>
> Eg <h:graphicImage styleClass="..." value="#{myBackingBean.chartURL}" />
>
> -----Original Message-----
> From: Paul Spencer [mailto:paulsp@apache.org]
> Sent: Wednesday, June 28, 2006 11:14 PM
> To: MyFaces Discussion
> Subject: How to displayi an images generated by a managed bean?
>
> I need to include a graphic image generated by JFreeCharts. Currently the
> generation of the image is in an action of a managed bean and is displayed
> in a separate windows via <h:commandButton>. How should I convert this to a
> <h:graphicImage> so it can be displayed in an existing page?
>
> Paul Spencer
>
>
RE: How to displayi an images generated by a managed bean?
Posted by Julian Ray <ju...@yahoo.com>.
Hi Paul,
We use JFreeCharts by creating a servlet which serves up the charts and then
creating a URL in the backingbean which is set as the value for the
graphicImage
Eg <h:graphicImage styleClass="..." value="#{myBackingBean.chartURL}" />
-----Original Message-----
From: Paul Spencer [mailto:paulsp@apache.org]
Sent: Wednesday, June 28, 2006 11:14 PM
To: MyFaces Discussion
Subject: How to displayi an images generated by a managed bean?
I need to include a graphic image generated by JFreeCharts. Currently the
generation of the image is in an action of a managed bean and is displayed
in a separate windows via <h:commandButton>. How should I convert this to a
<h:graphicImage> so it can be displayed in an existing page?
Paul Spencer
RE: How to displayi an images generated by a managed bean?
Posted by L Frohman <lf...@gmail.com>.
Look at:
http://www.jroller.com/page/cagataycivici?entry=phaselistener_renders_an_ima
ge_no
there is also a chart creator:
http://www.jroller.com/page/cagataycivici?entry=jsf_chart_creator
Lance
-----Message d'origine-----
De : Paul Spencer [mailto:paulsp@apache.org]
Envoyé : jeudi 29 juin 2006 05:14
À : MyFaces Discussion
Objet : How to displayi an images generated by a managed bean?
I need to include a graphic image generated by JFreeCharts. Currently the
generation of the image is in an action of a managed bean and is displayed
in a separate windows via <h:commandButton>. How should I convert this to a
<h:graphicImage> so it can be displayed in an existing page?
Paul Spencer