how to map css and js file from jsp file in spring mvc

前端 未结 2 1352
夕颜
夕颜 2021-01-26 01:59

I am new to Spring mvc. I want to add css and js files in jsp. It\'s a spring mvc framework. This is my spring-dispatcher-servlet.xml code



        
相关标签:
2条回答
  • 2021-01-26 02:35

    Well it looks like your resource mapping is not correct, your css and js files are under "/resources" folder right? then the mapping should be

    <mvc:resources mapping="/resources/**" location="/resources/"/>
    

    The inclusion of the elements in the view have been answered multiple times in stackoverflow itself. You can refer unable to use resources like css/js in spring mvc or How to include js and CSS in JSP with spring MVC

    0 讨论(0)
  • 2021-01-26 02:52

    In case you are still looking:

    Resource Mapping:

    <mvc:resources location="/resources/" mapping="/resources/**"/>
    

    Adding JS/CSS in jsp pages:

    <link rel="stylesheet" type="text/css" href="<c:url value="/resources/css/main.css"/>">
    <script src="<c:url value="/resources/js/app.min.js"/>"></script>
    

    Note: All of the JS/CSS files must be under src\main\webapp\resources directory.

    And if you have added Security, then you need to add this:

    <http pattern="/resources/**" security="none"/>
    
    0 讨论(0)
提交回复
热议问题