JSF 2.0 facelets are currently not supported.
To workaround this, the are the five steps to execute for JSF 2.0.
1). Add new component to faces-config.xml:
<component>
<component-type>package.CompressComponent</component-type>
<component-class>package.component.CompressComponent</component-class>
</component>
2). Create new taglib file named "granule.taglib.xml" and save it to WEB-INF:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE facelet-taglib PUBLIC
"-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN"
"http://java.sun.com/dtd/facelet-taglib_1_0.dtd">
<facelet-taglib>
<namespace>http://package/granule</namespace>
<tag>
<tag-name>compress</tag-name>
<component>
<component-type>package.CompressComponent</component-type>
</component>
</tag>
</facelet-taglib>
3). Add reference to new taglig in web.xml:
<context-param>
<param-name>javax.faces.FACELETS_LIBRARIES</param-name>
<param-value>/WEB-INF/granule.taglib.xml</param-value>
</context-param>
4). Create new Java class for CompressComponent:
import java.io.IOException;
import java.io.StringWriter;
import java.util.Map;
import javax.faces.component.UIComponent;
import javax.faces.component.UIOutput;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.servlet.http.HttpServletRequest;
import com.granule.CompressTagHandler;
import com.granule.JSCompileException;
import com.granule.RealRequestProxy;
/**
* JSF 2.0 Granule compress component.
*/
public class CompressComponent extends UIOutput {
@Override
public void encodeBegin(FacesContext context) throws IOException {
// Pass through attributes.
Map<String, Object> attributes = getAttributes();
String method = (String) attributes.get("method");
String options = (String) attributes.get("options");
String basepath = (String) attributes.get("basepath");
String enabledString = (String) attributes.get("enabled");
boolean enabled = enabledString == null || Boolean.parseBoolean(enabledString);
try {
String oldTagBody = renderTagBody(context);
String newTagBody;
if (enabled) {
HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
RealRequestProxy runtimeRequest = new RealRequestProxy(request);
CompressTagHandler compressor = new CompressTagHandler(getId(), method, options, basepath);
newTagBody = compressor.handleTag(runtimeRequest, runtimeRequest, oldTagBody);
} else {
newTagBody = oldTagBody;
}
context.getResponseWriter().write(newTagBody);
} catch (JSCompileException e) {
throw new IOException(e);
}
}
/**
* Render children.
*
* @param context FacesContext
* @return Tag body (children) as String
* @throws IOException on write error
*/
private String renderTagBody(FacesContext context) throws IOException {
ResponseWriter originalResponseWriter = context.getResponseWriter();
StringWriter tmpStringWriter = new StringWriter();
String oldTagBody = "";
try {
context.setResponseWriter(originalResponseWriter.cloneWithWriter(tmpStringWriter));
for (UIComponent comp : getChildren()) {
comp.encodeAll(context);
}
oldTagBody = tmpStringWriter.toString();
} finally {
context.setResponseWriter(originalResponseWriter);
}
return oldTagBody;
}
@Override
public void encodeEnd(FacesContext context) throws IOException {
// NOP
}
@Override
public boolean getRendersChildren() {
return true;
}
}
5). Add granule namespace to xhtml facelet end use new tag:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:granule="http://nl.agisweb/granule">
<granule:compress>
<link type="text/css" rel="stylesheet" href="#{request.contextPath}/resources/css/style1.css/>
<link type="text/css" rel="stylesheet" href="#{request.contextPath}/resources/css/style2.css/>
<link type="text/css" rel="stylesheet" href="#{request.contextPath}/resources/css/style3.css/>
</granule:compress>
</html>
来自:
http://code.google.com/p/granule/issues/detail?can=2&start=0&num=100&q=&colspec=ID%20Type%20Status%20Priority%20Milestone%20Owner%20Summary&groupby=&sort=&id=10