3

I have this use case in a JSF application.

Supposed in a JSF web application, I have a button that calls an external service that returns a complete HTML response then how can I show that HTML response to my users browsers?

The sequence of events are like this.

  1. In user browser, my application is displayed. A button is there that user can click.
  2. Clicking the button will call an external service. The external service will return information about a certain HTML tags. The HTML is complete with both head/body and with javascript. Currently the service can be implemented thru REST service or a plain DB call then
  3. How can I display that HTML tag in my user browser?

Is this possible to write non-JSF output in a JSF web application?

Just to add, I think my problem is how to write an HTML in my backing bean and write it back to the users browser.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Mark Estrada
  • 9,013
  • 37
  • 119
  • 186
  • Sounds like a regular client-side javascript ajax call that fetches data from some external source and appends it to the body of the page? I wouldn't know why the server needs to be involved here. – Gimby Feb 16 '15 at 10:13
  • This is a 3rd party site so we cannot control what informations they would send. The return HTML contains custom javascript code so I just need to show the generated html markup to the user. – Mark Estrada Feb 16 '15 at 10:28
  • @MarkEstrada, Why don't you drop that html into a dialog and display to your user as a result of his button click – Daniel Feb 16 '15 at 10:34
  • 2
    In a JSF application (or any other), when a response is rendered to a client/browser, there are no JSF related artifacts anymore. The browser is completely unaware of JSF. Thus, contents returned by JSF are no longer different from those returned by a service. Why can't you display a response in the form of plain HTML returned by a service like REST? – Tiny Feb 16 '15 at 10:45
  • @Tiny I actually dont know how to do this in JSF. Can you elaborate more? Thanks – Mark Estrada Feb 18 '15 at 03:31
  • This is answered below. – Tiny Feb 18 '15 at 14:05

1 Answers1

2

Just write it outright to the HTTP response body whereafter you instruct JSF that the response is manually completed. The principle is not much different from How to provide a file download from a JSF backing bean?, except that you need to set content disposition to inline (which is already the default anyway).

public void writeHtmlResponse() throws IOException {
    FacesContext fc = FacesContext.getCurrentInstance();
    ExternalContext ec = fc.getExternalContext();

    ec.setResponseContentType("text/html;charset=UTF-8");
    ec.setResponseCharacterEncoding("UTF-8");
    ec.getResponseOutputStream().write(html.getBytes("UTF-8"));

    fc.responseComplete(); // Important! Otherwise JSF will attempt to render the response which obviously will fail since it's already written with a file and closed.
}
Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Will this work even if my my JSF application is a legacy 1.X app? – Mark Estrada Feb 18 '15 at 10:25
  • 1
    In the future please explicitly mention version in the question. No one would these days expect that you're working on an EOL version. JSF 1.x hints are given in the link behind " How to stream a file download in a JSF backing bean?". – BalusC Feb 18 '15 at 10:38