Accessing resources in JSP page of Spring MVC app

前端 未结 5 1358
迷失自我
迷失自我 2020-12-16 05:31

I am fairly new to spring and I am having issues with accessing my resources in my Spring mvc app. I have tried Google and using stack overflow to find an answer (none of th

相关标签:
5条回答
  • 2020-12-16 05:50

    Set the resource order as below

        <mvc:resources mapping="/resources/**" location="/resources/" order="-1"/>
    
    0 讨论(0)
  • 2020-12-16 05:51

    Instead of mixing JSTL and JSP EL, you can configure spring like this :

    <mvc:resources mapping="/resources/**" location="/WEB-INF/pages/" />
    

    in your case,in the JSP use the following :

    <link href="<c:url value="/resources/My_CSS.css"/>" rel="stylesheet"/>
    
    0 讨论(0)
  • 2020-12-16 05:53

    You should place all of your static web content such as images, css and javascript in a resources directory under the webcontent (root directory) directory.

    enter image description here

    Then in myServlet-servlet.xml specify the directory as a resources directory. This will tell the dispatcher not to process requests for static resources.

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <mvc:resources mapping="/resources/**" location="/resources/" />
    

    In your jsp files you should then access these resources using a root relative url that relies upon the context path resolved by JSP EL.

    <link href="${pageContext.servletContext.contextPath}/resources/My_CSS.css" rel="stylesheet"/>
    
    0 讨论(0)
  • 2020-12-16 05:57

    In your jsp file you should set,

    <c:set var="context" value="${pageContext.request.contextPath}" />
    

    and your resource files js, css, img etc. include them with,

    <script src="${context}/resources/your.file.js" type="text/javascript"></script>
    <link href="${context}/resources/your.min.css" rel="stylesheet" type="text/css"/>
    

    Also you should use this before href to controller links, like

    <a href="${context}/yourPage">
    

    In this way you can use context path that is defined in servlet container or app server Tomcat, jetty etc.

    0 讨论(0)
  • 2020-12-16 05:59

    When using this:

    <mvc:resources mapping="/resources/**" location="/META-INF/RESOURCES/" />
    

    use this code in jsp :

    <link href="<c:url value="/"/>resources/My_CSS.css" rel="stylesheet"/>
    

    Look the difference. CAPS RESOURCES locates file path. and resources locates in web path

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