本文介绍了JSF:从罐子中提供资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建几个装有facelets模板的罐子,以供整个组织使用.

I´m in the process to create a couple of jars packed with facelets templates for usage through the organisation.

卡在JSF 1.2中的此功能并不是开箱即用的.堆栈:

Stuck in JSF 1.2 this functionality does not come out of the box.Stack:

  • Jboss EAP 5.1
  • 接缝2.2
  • Richfaces 3.3.3

在我看来,我主要需要两个资源:

It seems to me that I primarily need two resources:

  1. 找到Faclets资源的资源解析器
  2. 从jar服务CSS和js资源的东西

资源解析器似乎很简单: http://ocpsoft.org/opensource/create-common-facelets-jar/

Resource resolver seems to be the easy part:http://ocpsoft.org/opensource/create-common-facelets-jar/

流css/js的事情稍微复杂一些: JSF:从罐子中提供资源 http://cagataycivici.wordpress.com/2006/05/09/jsf_serving_resources_from_a/

The thing that streams css / js is slightly more complicated:JSF: Serving Resources From a Jarhttp://cagataycivici.wordpress.com/2006/05/09/jsf_serving_resources_from_a/

我非常想使用Weblet,因为它似乎是致力于解决该问题的项目.此外,在我买的这本很酷的JSF书中,推荐使用:"Pro JSF和Ajax构建富Internet组件"

I would very much like to use Weblets as it seems to be project dedicated to solve this problem. Furthermore it is recommended in this cool JSF book i bought: "Pro JSF and AjaxBuilding Rich Internet Components"

问题是我无法在任何Maven存储库中找到稳定的发行版,也没有文档.在此示例中,不使用小孔:

https://github.com/werpu/weblets

在上面的文章中,突出显示了筛选器是一个很好的解决方案. http://myfaces.apache .org/tomahawk-project/tomahawk/apidocs/org/apache/myfaces/webapp/filter/ExtensionsFilter.html

In the article above a Filter is highlighted as a good solution.http://myfaces.apache.org/tomahawk-project/tomahawk/apidocs/org/apache/myfaces/webapp/filter/ExtensionsFilter.html

遗憾的是,要采用此解决方案并不是一件容易的事.我根本没有时间(或目前没有知识):(

Sadly it is not a simple task to hook in to this solution. I simply do not have the time (or knowledge at the moment) :(

可能可以使用此项目: http://jawr.java.net/introduction.htmlJawr旨在捆绑js和CSS文件.

Possible I can use this project: http://jawr.java.net/introduction.htmlJawr aims to bundle js and css files.

项目Wiki指示有可能: http://jawr.java.net/docs/generators.html 参见类路径资源生成器"部分.

The project wiki indicates that it could be possible:http://jawr.java.net/docs/generators.html see the "classpath resource generator" section.

请咨询:)

推荐答案

您好,我想从jar加载css,js等,您可以使用以下代码.

Hello i you would like to load css, js etc from a jar you could use the following.

package se.lu.ldc.core.servlet;


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;

import javax.faces.webapp.FacesServlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;


/**
 * Servlet för att hämta diverse filer från jar om de finns i jaren
 *  jpg, png, gif, css, xcss,xml,js
 * @author ldc-jha
 *
 */
@WebServlet(name="ldcServlet",loadOnStartup=1,urlPatterns={"*.jpg","*.png","*.gif","*.css","*.xcss","*.js"})
public class LDCFrameWorkServlet extends HttpServlet
{

    //where the files are in the jar.
    public final String BASE_PATH = "xhtml/framework";

    private Logger log = Logger.getLogger(getClass().getName());

    @Override
    public void init(ServletConfig config) throws ServletException
    {
        System.out.println("INIT()");
        super.init(config);
    }
    @Override
    public void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {


        /* if this servlet is not mapped to a path, use the request URI */
        String path = request.getPathInfo();
        if (path == null) {
            path = request.getRequestURI().substring(
                    request.getContextPath().length());
        }

        URL resource = Thread.currentThread().getContextClassLoader().
        getResource(BASE_PATH+"/"+path.substring(1));

        if (resource == null) {
            ServletContext sc = getServletContext();

            String filename = sc.getRealPath(path);
            log.info("During Load:"+resource+":"+path+":"+filename);
            try{
                resource = sc.getResource(path);
            }catch(Exception e){}
            if(resource == null)
            {
                response.sendError(404, path + " denied");

            }

        }
        /* failure conditions */
        if (path.endsWith(".seam")) {
            javax.faces.webapp.FacesServlet facesServlet = new FacesServlet();
            facesServlet.service(request, response);

            return;
        }
        if (path.endsWith(".class")) {
            response.sendError(403, path + " denied");
            return;
        }

        /* find the resource */
        log.info("Looking for " + path + " on the classpath");

        //response.sendError(404, path + " not found on classpath");

        log.info("found " + path + " on the classpath:"+resource.toString());

        /* check modification date */
        URLConnection connection = resource.openConnection();
        long lastModified = connection.getLastModified();
        long ifModifiedSince = request.getDateHeader("If-Modified-Since");
        if (ifModifiedSince != -1 && lastModified <= ifModifiedSince) {
            response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
            return;
        }

        /* write to response */
        response.setContentType(getServletContext().getMimeType(path));
        OutputStream out = new BufferedOutputStream(
                response.getOutputStream(), 512);
        InputStream in = new BufferedInputStream(
                resource.openStream(), 512);
        try {
            int len;
            byte[] data = new byte[512];
            while ((len = in.read(data)) != -1) {
                out.write(data, 0, len);
            }
        } finally {
            out.close();
            in.close();
            if (connection.getInputStream() != null) {
                connection.getInputStream().close();
            }
        }

    } /* doGet() */


    @Override
    public void service(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    {
        doGet(request, response);
    }

}

如果您想从罐子中装载滤饼,则可以使用Custom DefaultResourceResolver

if you want to load faclets from a jar you could use a Custom DefaultResourceResolver

/**
 * Används för hämta hämta xhtml filer från jar filen
 * @author ldc-jha
 *
 */
public class ClassletsResourceResolver extends DefaultResourceResolver implements ResourceResolver{

    public ClassletsResourceResolver() {
        super();
    }

    private static final String PREFIX = "/framework/";
    private static final String LAYOUT = "/layout/";

    public String getPrefix() {
        return PREFIX;
    }

    public URL resolveUrl(String path) {
        final String prefix = getPrefix();
        System.out.println("resolveUrl()"+path);
        URL url = null;
        if (path != null && path.startsWith(PREFIX)) {
            final String resource = path.substring(PREFIX.length());
            url = getClass().getClassLoader().getResource("xhtml/framework/"+resource);
        }

        if (path != null && path.startsWith(LAYOUT)) {
            System.out.println("LAYOUT:"+path);
            url = getClass().getClassLoader().getResource("xhtml/framework"+path);
            System.out.println(url);
        }

        if(url != null){
            return url;
        }
        else
        {
            return super.resolveUrl(path);
        }
    }
}

如果要在不编辑xhtml的情况下加载resourceResolver,则可以执行此操作.

if you want to load the resourceResolver wihtout editing your xhtml you can do this.

@WebListener
public class SimpleServletContextListener implements ServletContextListener{

    private static final LogProvider log = Logging.getLogProvider(SimpleServletContextListener.class);


    public void contextInitialized(ServletContextEvent event){


                      event.getServletContext().setAttribute("facelets.RESOURCE_RESOLVER","se.lu.ldc.core.reslover.ClassletsResourceResolver");

}

}

您可能还必须让Seam了解Faclets,如果有任何问题,请告诉我.我有相同的想法,并且可以将所有CSS,XHTML等打包到一个罐中.

You might have to let Seam know about the Faclets as well, let me know if there is any problem. i had the same idea, and got it working with packing all css, xhtml etc in a jar.

这篇关于JSF:从罐子中提供资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 03:01