Skip to main content

How to set content type for a JSF page

3 replies [Last post]
mustangdolphin
Offline
Joined: 2006-03-24
Points: 0

Hi,
I want to know is there any way we can specify the content type of a JSF page, like in JSPs we have the page attribute <%@ page contentType="application/vnd.ms-excel" %>

In JSP we can create a html table with values and if we specify the contenType as application/vnd.ms-excel, we would get an excel file generated.

But do we have something similar to this is JSF, as I am using Facelets I cannot use page directive in the xhtml file.
I tried setting the content type in MangedBean's action as follows
((HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse()).setContentType("application/vnd.ms-excel"); but it did'nt work.

Thanks in advance.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
bojan_ribic_yms...
Offline
Joined: 2008-05-28
Points: 0

Try for example to add this to the head section of your xhtml file:

rlubke
Offline
Joined: 2003-08-21
Points: 0

In Facelets you can force the content type by setting the contentType attribute on f:view.

edburns
Offline
Joined: 2004-02-11
Points: 0

> Hi,
> I want to know is there any way we can specify the
> content type of a JSF page, like in JSPs we have the
> page attribute <%@ page
> contentType="application/vnd.ms-excel" %>
>
> In JSP we can create a html table with values and if
> we specify the contenType as
> application/vnd.ms-excel, we would get an excel file
> generated.
>
> But do we have something similar to this is JSF, as I
> am using Facelets I cannot use page directive in the
> xhtml file.
> I tried setting the content type in MangedBean's
> action as follows
> ((HttpServletResponse)FacesContext.getCurrentInstance(
> ).getExternalContext().getResponse()).setContentType("
> application/vnd.ms-excel"); but it did'nt work.

It matters at what point in the lifecycle you call it. If you call it too early, it might get reset
by something else. If you call it too late, it might not get set due to the response being comitted.

I've modified the facelets numberguess sample app to take advantage of the new groovy support in the latest release of Mojarra, available at:

https://javaserverfaces.dev.java.net/servlets/ProjectDocumentList?folder...

Instructions for upgrading your glassfish install with this JSF version are on the above page.

You can get the sample showing that this works at:

http://mediacast.sun.com/users/edburns00/media/jsf-numberguess.war

Deploy this in your Mojarra 1.2_09 Beta1 enabled container and take a look at the content type sent when you guess a number. It will be exactly what you set in the groovy managed bean:

public void beforePhase(PhaseEvent event) {
FacesContext context = FacesContext.getCurrentInstance();
ExternalContext extContext = context.getExternalContext();
HttpServletResponse response = extContext.getResponse();

if (event.getPhaseId() == PhaseId.RENDER_RESPONSE) {
String contentType = "text/html; charset=UTF-8";
extContext.getRequestMap().put("message",
"response is" +
response);
response.setContentType(contentType);

}

}

Let us know if this helps.

Ed