laxman954 / granule

Automatically exported from code.google.com/p/granule
0 stars 0 forks source link

JSF 2.0 facelet support #10

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
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>

Original issue reported on code.google.com by rjjuurl...@gmail.com on 10 Jul 2011 at 9:15

GoogleCodeExporter commented 8 years ago
when xhtml contains <f:event listener="#{userBean.login}" type="preRenderView" 
/>,I cannot get combined.css.

Who can help me...

Original comment by leo.geng...@grapecity.com on 24 Oct 2012 at 10:42

GoogleCodeExporter commented 8 years ago
Great component. Thanks!

I had to add this:
    @Override
    public void encodeChildren(FacesContext context) throws IOException {

    }

otherwise I got both compressed script/css and original children on my pages.
This was for site with old jsf 1.1* so maybe there is some difference in it.

Original comment by cubro...@gmail.com on 16 Mar 2015 at 4:44