简体   繁体   English

WebApplicationInitializer中的多个Servlet

[英]Multiple Servlets in WebApplicationInitializer

I was trying to set up multiple servlets in my WebApplicationInitializer , one for the default requests (that trigger the jsp and return the html, using a DispatcherServlet , and one for static resources, using a custom made StaticServlet . What confuses me is how to apply the routing to get the requests to the right servlet, and in fact one of the static servlet is never called to resolve the requests seems to confirm my suspicions. So far this is the code I have on the WebApplicationInitializer : 我试图在WebApplicationInitializer设置多个servlet,一个用于默认请求(使用DispatcherServlet触发jsp并返回html,另一个使用定制的StaticServlet来处理静态资源。令我困惑的是如何申请将请求发送到正确的servlet的路由,实际上从未调用过静态servlet之一来解析请求似乎证实了我的怀疑。到目前为止,这是我在WebApplicationInitializer上拥有的代码:

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    servletContext.setSessionTrackingModes(
        Collections.<SessionTrackingMode>emptySet());

    // Create the 'root' Spring application context
    // which will contain the application components
    AnnotationConfigWebApplicationContext rootContext
        = new AnnotationConfigWebApplicationContext();
    rootContext.register(RootConfig.class);

    // Manage the lifecycle of the root application context
    servletContext.addListener(new ContextLoaderListener(rootContext));

    // Create the dispatcher servlet's Spring application context
    // to contain dispatched beans such as controllers
    AnnotationConfigWebApplicationContext dispatcherContext
        = new AnnotationConfigWebApplicationContext();
    dispatcherContext.register(DispatcherConfig.class);

    // Register and map the dispatcher servlet under /
    ServletRegistration.Dynamic dispatcher = servletContext.addServlet(
        "dispatcher",
        new DispatcherServlet(dispatcherContext));
    dispatcher.setLoadOnStartup(1);
    dispatcher.addMapping("/");

    // Register and map the static dispatcher servlet under /static/*
    ServletRegistration.Dynamic staticDispatcher = servletContext.addServlet(
        "staticDispatcher",
        new FileServlet());

    staticDispatcher.setLoadOnStartup(1);
    staticDispatcher.setInitParameter("basePath", "/static/fonts/");
    staticDispatcher.addMapping("/static/*");

The static Servlet do not need a Configuration (Is a base HttpServlet ), but what bothers me is the fact that I am using two ServletRegistration in order to define the two different mappings. 静态Servlet不需要Configuration (是一个基本HttpServlet ),但是令我困扰的是我使用两个ServletRegistration来定义两个不同的映射。 Is there a way to use the same one and define the mapping to a specific servlet? 有没有一种方法可以使用相同的方法并定义到特定servlet的映射? Or should the mapping be done at another level (maybe the Listener of the rootContext )? 还是应该在另一个级别上完成映射(可能是rootContext的Listener)? I have tried to look around but it seems no one has solved or have any problem (probably) to set up multiple servlets programmatically. 我试图环顾四周,但似乎没有人解决或以编程方式设置多个servlet时遇到任何问题(可能)。

Any idea of why I'm not getting any hit on the static Servlet ? 我为什么对静态Servlet没有任何帮助?

Edit: 编辑:

This is a static file request in one of my .jsp files, that would be supposed to go through the FileServlet : 这是我的一个.jsp文件中的静态文件请求,应该通过FileServlet

<style type="text/css">
     @font-face {
        font-family: 'DinWeb';
        src: url(/static/fonts/DINWeb.eot?) format('eot'), url(/static/fonts/DINWeb.woff)  format('woff'), url(/static/fonts/DINComp.ttf) format('truetype');
        font-weight: normal;
    }
</style>

What I would expect is the request to be redirected to the FileServlet (since the url starts with /static/), and from there be manipulated/managed so that it would return the font (or a file or another media) 我希望将请求重定向到FileServlet (因为url以/ static /开头),然后从那里进行操作/管理,以便它返回字体(或文件或其他媒体)

Founded an alternative Solution: Instead of making two DispactherServlet (or one and another), just implement a servlet and a Filter that will listen to all requests, and take care of those URLs that starts with static. 创建了一个替代解决方案:无需实现两个DispactherServlet (或一个或另一个),只需实现一个Servlet和一个Filter侦听所有请求,并处理以静态开头的URL。

In my code I added this two lines: 在我的代码中,添加了以下两行:

FilterRegistration filterReg = servletContext.addFilter("staticFilter",     StaticFilesFilter.class);
filterReg.addMappingForUrlPatterns(null, false, "/*");

and removed completely the second custom made servlet. 并完全删除了第二个定制的servlet。 As per the filter, this link is pretty useful (Just click the download to see the java file of the filter implementation itself). 对于过滤器,此链接非常有用(只需单击下载即可查看过滤器实现本身的java文件)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM