JSF 2.0中granule压缩、合并、加密javascript

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值