1

I have many JSF pages which uses <ui:composition>. Say I have following pages

  • common.xhtml (here define head and body. It is parent/main/top page of all pages)
  • p1.xhtml (included in common.xhtml using <ui:composition>)
  • p2.xhtml (included in common.xhtml using <ui:composition>)
  • p3.xhtml (included in common.xhtml using <ui:composition> and I want to reload JF and css files when this page hits)

I go through this link, but it will not gonna help me because I have to put the header in the common.xhtml (this will stop caching all JF and CSS files, and I donot want this.)

How can I reload js and css files only when p3.xhtml hits ?

EDIT

I added following in common.xhtml

<h:head>
   ....
   <ui:insert name="metaTags" />
</h:head>

I added following in p3.xhtml

<ui:define name="metaTags">
    <meta http-equiv="cache-control" content="max-age=0" />
    <meta http-equiv="cache-control" content="no-cache" />
    <meta http-equiv="expires" content="0" />
    <meta http-equiv="pragma" content="no-cache" />
</ui:define>

But still it didn't pick my updated js file.

Community
  • 1
  • 1
Junaid
  • 2,572
  • 6
  • 41
  • 77

1 Answers1

3

Based on @BalusC comment (thanks for the comment) I will totally rewrite my answer:

You don't need any Servlet, you can include your logic in any of the JSF lifecycle phases implementing a JSF phase listener. I have adapted the content here to your question.

You only have to implement a phase listener wich triggers before render phase and declare it inside the faces-config.xml file.

The declaration:

<lifecycle>
    <phase-listener id="nocache">com.company.package.CacheControlPhaseListener</phase-listener>
</lifecycle>

The phase listener code:

import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
import javax.servlet.http.HttpServletResponse;

public class CacheControlPhaseListener implements PhaseListener
{
    private static final long serialVersionUID = 1L;

    @Override
    public PhaseId getPhaseId()
    {
        return PhaseId.RENDER_RESPONSE;
    }

    @Override
    public void afterPhase(PhaseEvent event)
    {
    }

    @Override
    public void beforePhase(PhaseEvent event)
    {
        FacesContext facesContext = event.getFacesContext();
        if("/p3.xhtml".equals(facesContext.getExternalContext().getRequestServletPath())){
            HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
            response.addHeader("Pragma", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
            // Stronger according to blog comment below that references HTTP spec
            response.addHeader("Cache-Control", "no-store");
            response.addHeader("Cache-Control", "must-revalidate");
            // some date in the past
            response.addHeader("Expires", "Mon, 8 Aug 2006 10:00:00 GMT");
        }
    }
}

The incorrect initial answer:

You could write a define similar to this in p3.xhtml:

<ui:define name="nocache">
    <meta http-equiv=... />
    ...
</ui:define>

and write an insert in the head section of common.xhtml:

<ui:insert name="nocache" />*
Javier Haro
  • 1,255
  • 9
  • 14
  • You say in your question: "this will stop caching all JF and CSS files, and I donot want this..". Did you check it? I asumed you had chrcked the answer you cite in your question, dio you? – Javier Haro Sep 02 '15 at 06:56
  • @BalusC is there any tag is JSF that can help me or I have to implement a servlet ? – Junaid Sep 02 '15 at 07:32