1

I am trying to retrieve csv file from JSF managed bean.

Right now I am doing it this way,

CsvController: Bean which responds output with content type text/csv.

samplecsv.xhtml: A webpage which contains the code:

#{csvController.generateReport()}

When I browse this page on browser, a download dialog opens which enables downloading the csv file.

report.xhtml:

Webpage which downloads that csv:

d3.csv('http://mysite/samplecsv.xhtml');

Is there a way to get rid of samplecsv.xhtml and directly retrieve csv file from bean in report.xhtml? Something like d3.csv('http://mysite/csvController/report') would be perfect

7
  • I have never used that! The thing is we are not really pro in java. We needed a quick web app and built one by integrating JSF(with primefaces and omnifaces) with spring (thought it would be best of all worlds). It did us good in short term but I am thinking about removing JSF completely and using pure spring mvc. Keeping that transition in mind, would it be good idea to use Spring servlet for csv files and leave the rest with Faces Servlet? Commented Oct 30, 2014 at 18:15
  • That is indeed what I am looking for. Now the new implementation will consist of CsvServlet which takes report name as parameter. I am still confused about how to resolve the report and make sure it is properly initialized. Each report is a managed bean accessing other beans with autowired DataSource and other services. I don't want to inject all the reports to CsvServlet. After reading report name from Csv request, how would I call a corresponding report bean which has been automatically initialized with datasource and services? Commented Oct 31, 2014 at 2:12
  • This is what I am thinking: 1. CsvServlet: For each request, responds with a csv file by using ReportResolver 2. ReportResolver: CsvServlet passes report name to it and ReportResolver should be able to initialize report and get csv string out of report. 3. Report: Fire sql query, and return csv string as output by calling a service which converts resultset to csv string. (@Autowired should still work for all of them) Commented Oct 31, 2014 at 2:17
  • I understood that CsvController would be a servlet which responds with csv file. What I also wanted was automatic mapping of reports. Like a request such as site/report/report1 should automatically produce csv for report1. I understand request to /report would be handled by CsvController. Then I would need to code the logic to figure out which report the request is asking for. There will be tons of report, so I was hoping for automated resolving of the report. May be I should use Java Reflection. Commented Oct 31, 2014 at 13:10
  • Ok so I will have to code an automated system for resolving reports properly right? Commented Oct 31, 2014 at 13:48

1 Answer 1

1
+50

The pattern you're asking about is not really native to the way JSF works. See JSF 2: invoking managed bean directly

If you still want to download the content directly from the managed bean, here is an example in how to do it. Anyway, the url to the download link in your report.xhtml should be generated with some jsf component like <f:commandLink action="#csvController.download()}" /> See How to provide a file download from a JSF backing bean?

My recommended approach is to create a new @WebServlet("/csvController") or JAX-RS @Path("/csvController") front-end and call your business logic from here. To access the managed bean from the servlet you could do it like this: Possible to inject @ManagedBean as a @ManagedProperty into @WebServlet?

Anyway, i recommed you to take a look at: Backing beans (@ManagedBean) or CDI Beans (@Named)? Are @ManagedBeans obsolete in JavaEE6 because of @Named in CDI/Weld?

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for the reply. We are using an integration between spring and primefaces: github.com/michail-nikolaev/primefaces-spring-scopes. We are trying to slowly remove the primefaces part. We use FacesServlet for all the pages. Would it be possible to use Spring's servlet for csv files instead?
Yes. You can. Here are 2 examples implementing a download controller with Spring: codejava.net/frameworks/spring/… codejava.net/frameworks/spring/… . Anyway, i recommend you to move your #generateRepòrt function to a spring managed bean.
Ok. Then I will try with using SpringServlet for csv but FacesServlet for rest of the application.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.