How to access static resources when mapping a global front controller servlet on /*

前端 未结 19 2145
轮回少年
轮回少年 2020-11-22 07:35

I\'ve mapped the Spring MVC dispatcher as a global front controller servlet on /*.

               


        
相关标签:
19条回答
  • 2020-11-22 08:18

    I'd recommend trying to use a Filter instead of a default servlet whenever possible.

    Other two possibilities:

    Write a FileServlet yourself. You'll find plenty examples, it should just open the file by URL and write its contents into output stream. Then, use it to serve static file request.

    Instantiate a FileServlet class used by Google App Engine and call service(request, response) on that FileServlet when you need to serve the static file at a given URL.

    You can map /res/* to YourFileServlet or whatever to exclude it from DispatcherServlets' handling, or call it directly from DispatcherServlet.

    And, I have to ask, what does Spring documentation say about this collision? I've never used it.

    0 讨论(0)
  • 2020-11-22 08:19

    In Embedded Jetty I managed to achieve something similar by adding a mapping for the "css" directory in web.xml. Explicitly telling it to use DefaultServlet:

    <servlet>
      <servlet-name>DefaultServlet</servlet-name>
      <servlet-class>org.eclipse.jetty.servlet.DefaultServlet</servlet-class>
    </servlet>
    
    <servlet-mapping>
      <servlet-name>DefaultServlet</servlet-name>
      <url-pattern>/css/*</url-pattern>
    </servlet-mapping>
    
    0 讨论(0)
  • 2020-11-22 08:21

    I found that using

    <mvc:default-servlet-handler />
    

    in the spring MVC servlet bean definition file works for me. It passes any request that isn't handled by a registered MVC controller on to the container's original default handler, which should serve it as static content. Just make sure you have no controller registered that handles everything, and it should work just fine. Not sure why @logixplayer suggests URL rewriting; you can achieve the effect he's looking for just adequately using Spring MVC alone.

    0 讨论(0)
  • 2020-11-22 08:23

    As of 3.0.4 you should be able to use mvc:resources in combination with mvc:default-servlet-handler as described in the spring documentation to achieve this.

    http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-static-resources

    0 讨论(0)
  • 2020-11-22 08:24

    The best way to handle this is using some kind of URL re-writing. In this way, you can have clean restful URLs, and NOT with any extensions i.e abc.com/welcom/register as opposed to abc.com/welcome/resister.html

    I use Tuckey URL which is pretty cool.

    It's got instructions on how to set up your web app.I have set it up with my Spring MVC web app. Of course, everything was fine until I wanted to use annotations for Spring 3 validations like @Email or @Null for domain objects.

    When I add the Spring mvc directives:

    < mvc:annotation-driven  /> 
    < mvc:default-servlet-handler />
    

    .. it breaks the good ol Tuckey code. Apparently, < mvc:default-servlet-handler /> replaces Tuckey, which I'm still trying to solve.

    0 讨论(0)
  • 2020-11-22 08:24

    After trying the filter approach without success (it did for some reason not enter the doFilter() function) I changed my setup a bit and found a very simple solution for the root serving problem:

    Instead of serving " / * " in my main Servlet, I now only listen to dedicated language prefixes "EN", "EN/ *", "DE", "DE/ *"

    Static content gets served by the default Servlet and the empty root requests go to the index.jsp which calls up my main Servlet with the default language:

    < jsp:include page="/EN/" /> (no other content on the index page.)

    0 讨论(0)
提交回复
热议问题