我正在使用Primefaces。对于某些页面,我想排除primefacs包含的资源,尤其是主题资源。
<link type="text/css" rel="stylesheet" href="/context/javax.faces.resource/theme.css?ln=primefaces-owntheme" />
我使用SystemEventListener尝试如下:
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);
}
}
并在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>
当我手动在页面上包含资源时,这种方法很有效,但那些不能与Primefaces包含的资源一起使用。我该如何删除这些资源?
答案 0 :(得分:3)
theme.css
未添加为动态组件资源,但它已在PrimeFaces'HeadRenderer
中进行了硬编码。 OmniFaces CombinedResourceHandler
也是struggling。
解决方案相当简单:使用自定义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>
答案 1 :(得分:2)
对于想要在所有页面上删除theme.css的所有人(这是我正在做的第一件事),并且仍然不知道这个技巧。
您需要在web.xml中删除以下几行:
<context-param>
<param-name>primefaces.THEME</param-name>
<param-value>none</param-value>
</context-param>