【问题标题】:static content is not getting loaded in spring mvc resource tag静态内容未在 spring mvc 资源标签中加载
【发布时间】:2021-09-09 23:57:36
【问题描述】:

我正在使用 mvc:resources 来访问我的静态内容。我以正确的方式使用并且具有正确的项目结构仍然无法访问静态内容。我是 spring mvc 的新手,因此请帮助我解决这个问题。

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://JAVA.sun.com/xml/ns/javaee"
    xmlns:jsp="http://java.sun.com/xml/ns/javaee/jsp"
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>
</web-app>

index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="/resources/js/global.js"></script>
</head>
<body>
<h1 style="text-align:center;">Hello World!!</h1>
<button onclick="goToSecondPage()">Click Me</button>
</body>
</html>

调度程序-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/util  
        http://www.springframework.org/schema/util/spring-util-3.2.xsd">

    <mvc:resources mapping="/resources/**" location="/resources/" />
    <context:component-scan base-package="com.et.demo.Controllers" />
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>
    <mvc:annotation-driven />
</beans>

我已经尝试了所有方法,但无法加载静态内容。please click here to see my project structure.

【问题讨论】:

    标签: javascript java spring spring-boot spring-mvc


    【解决方案1】:

    您的项目结构中似乎有两个resources 文件夹。确保将资源放在正确的文件夹中(main 文件夹的直接子文件夹)。也无需将/resources/ 添加到路径中,因为它已经被您的mvc:resources 映射映射。

    【讨论】:

      猜你喜欢
      • 2019-01-31
      • 1970-01-01
      • 1970-01-01
      • 2022-09-30
      • 2014-10-15
      • 2017-01-14
      • 2017-07-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多