Spring Tomcat and static resources and mvc:resources

后端 未结 3 1622
一向
一向 2020-12-10 05:59

I started doing a web app from scratch. Before I\'ve always been working on apps that were already running for a long time, so I didn\'t have to deal with the full setup pha

相关标签:
3条回答
  • 2020-12-10 06:03

    <mvc:resources> plays well with annotated controllers, but may require some extra configuration with other kinds of controller mappings.

    I guess in your case you need to declare BeanNameUrlHandlerMapping manually (it's usually registered by default, but <mvc:resources> overrides defaults as a side-effect of applying its own configuration):

    <bean class = "org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
    
    0 讨论(0)
  • 2020-12-10 06:12

    I had the same problem, too. I solved it by adding the spring macro before the actual reference to the resource, in order to resolve the servlet path.

    It is not really nice in the code, but this solution makes you being independent from the actual context under which your application is deployed on the server and your servlet too. Usually, you don't want to have the actual servlet being mentioned in your url.

    For instance, refering to the answer of ColdStoneJava, that would be:

      <script type="text/javascript" src="<@spring.url '/static/js/jquery-1.4.4.min.js'/>"></script>
    

    You also have to reference it absolute in the url, so the slash '/...' is essential.

    This, won't work in my experience:

      <script type="text/javascript" src="<@spring.url 'static/js/jquery-1.4.4.min.js'/>"></script>
    

    Cheers.

    0 讨论(0)
  • 2020-12-10 06:15

    I had the same problem. I was able to fix this by using the full path like for

    CSS

    <link rel="stylesheet" type="text/css" media="screen" href="/my-web-app/resources/css/smoothness/jquery-ui-1.8.21.custom.css">
    

    and for javascript

    <script type="text/javascript" src="/my-web-app/static/js/jquery-1.4.4.min.js"></script?
    

    Let me know if this solves your problem before Spring comes up with some better approach.

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