Exclude Resources in JSF

I am using Primefaces. For some pages, I want to exclude resources that are part of the pre-facts, especially the theme resource.

<link type="text/css" rel="stylesheet" href="/context/javax.faces.resource/theme.css?ln=primefaces-owntheme" />    

I am trying to do this using SystemEventListener as follows:

public class PrimeFacesResourceRemover implements javax.faces.event.SystemEventListener {


  @Override
  public void processEvent(SystemEvent event) throws AbortProcessingException {
    FacesContext context = FacesContext.getCurrentInstance();

    UIViewRoot viewRoot = context.getViewRoot();
    for (UIComponent resource : viewRoot.getComponentResources(context, "head")) {

        Map<String, Object> attrs = resource.getAttributes();
        String resourceLibrary = (String) attrs.get("library");
        String resourceName = (String) attrs.get("name");

        if ("primefaces-owntheme".equals(resourceLibrary) && "theme.css".equals(resourceName)) {

            // Remove resource from view
            context.getViewRoot().removeComponentResource(context, resource, HEAD);
        }
    }
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public boolean isListenerForSource(Object source) {
    return (source instanceof UIViewRoot);
  }
}

and in faces-config.xml

    <application>
      <system-event-listener>
        <system-event-listener-class>
          com.mycompany.mavenproject1.PrimeFacesResourceRemover
        </system-event-listener-class>
        <system-event-class>
          javax.faces.event.PostAddToViewEvent
        </system-event-class>
      </system-event-listener>
    </application>

This works well when I enable resouce on the page manually, but they do not work with the resources that are part of Primefaces. How to remove these resources?

+4
source share
2 answers

theme.cssnot added as a resource of dynamic components, but it was hard-coded in PrimeFaces HeadRenderer. OmniFaces has CombinedResourceHandleralso been fighting this.

: HeadRenderer ( @ResourceDependency theme.css, !):

public class HeadRenderer extends Renderer {

    @Override
    public void encodeBegin(FacesContext context, UIComponent component) throws IOException {
        context.getResponseWriter().startElement("head", component);
    }

    @Override
    public void encodeChildren(FacesContext context, UIComponent component) throws IOException {
        // NOOP.
    }

    @Override
    public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
        for (UIComponent resource : context.getViewRoot().getComponentResources(context, "head")) {
            resource.encodeAll(context);
        }

        context.getResponseWriter().endElement("head");
    }

}

webapp faces-config.xml:

<render-kit>
    <renderer>
        <component-family>javax.faces.Output</component-family>
        <renderer-type>javax.faces.Head</renderer-type>
        <renderer-class>com.example.HeadRenderer</renderer-class>
    </renderer>
</render-kit>
+3

, theme.css ( , ), .

web.xml:

<context-param>
    <param-name>primefaces.THEME</param-name>
    <param-value>none</param-value>
</context-param>
+1

All Articles