JSF:从Jar服务资源

JSF:从Jar服务资源,jsf,facelets,Jsf,Facelets,我正在创建两个装有facelets模板的罐子,供组织使用 在JSF1.2中,这个功能不是现成的。 堆栈: Jboss EAP 5.1 接缝2.2 Richfaces 3.3.3 在我看来,我主要需要两种资源: 查找Faclets资源的资源解析程序 从jar提供css和js资源的东西 资源解析器似乎是最简单的部分: 流式处理css/js的事情稍微复杂一些: JSF:从Jar服务资源 相位记录器 滤器 Servlet weblet 我非常想使用Weblet,因为它似乎是专门解决这个问题的

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

在JSF1.2中,这个功能不是现成的。 堆栈:

  • Jboss EAP 5.1
  • 接缝2.2
  • Richfaces 3.3.3
在我看来,我主要需要两种资源:

  • 查找Faclets资源的资源解析程序
  • 从jar提供css和js资源的东西
  • 资源解析器似乎是最简单的部分:

    流式处理css/js的事情稍微复杂一些: JSF:从Jar服务资源

    • 相位记录器
    • 滤器
    • Servlet
    • weblet
    我非常想使用Weblet,因为它似乎是专门解决这个问题的项目。此外,我买的这本很酷的JSF书中还推荐了它:“Pro JSF和Ajax 构建富Internet组件”

    问题是,我在任何Maven回购协议中都找不到稳定的版本,也没有文档。在本例中,没有使用faclets:

    在上面的文章中,过滤器是一个很好的解决方案。

    不幸的是,这不是一个简单的任务,钩到这个解决方案。我现在根本没有时间(或知识):(

    可能我可以使用此项目: Jawr的目标是捆绑js和css文件

    project wiki表明,这是可能的: 请参阅“类路径资源生成器”部分


    请注意:)

    您好,我想从jar加载css、js等,您可以使用以下命令

    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);
        }
    
    }
    
    如果您想在不编辑xhtml的情况下加载resourceResolver,可以这样做

    @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等打包到一个jar中。

    请注意,当您使用JSF2.0时,这一切都是不必要的。另请参见,在Servlet3.0环境中使用JSF1.x是一件很奇怪的事情。为什么不立即升级到JSF 2.x呢?正如我所说:“在JSF 1.2中,这个功能不是现成的。”这个堆栈是jboss(jboss eap)支持的边界。摆弄会破坏支持协议。所以我告诉过你,在版本2.3之前,jsf2是不兼容seam 2的。记住,你必须添加pages.xml和page.xml。如果你愿意,我也可以补充一下。
    @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");
    
    }