静态内容未加载到 spring mvc 资源标签中 [英] static content is not getting loaded in spring mvc resource tag

查看:55
本文介绍了静态内容未加载到 spring mvc 资源标签中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using mvc:resources to access my static content . I am using in a proper way and having proper project structure still not able to access the static content.I am new in spring mvc therefore please help me to solve this.

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>

dispatcher-servlet.xml

dispatcher-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>

我尝试了所有方法,但无法加载静态内容.请单击此处查看我的项目结构.

I have tried everything but failed to load static content.please click here to see my project structure.

推荐答案

看起来您的项目结构中有两个 resources 文件夹.确保将资源放在正确的文件夹中(main 文件夹的直接子文件夹).也不需要将 /resources/ 添加到路径中,因为它已经由您的 mvc:resources 映射映射.

It looks like there are two resources folders in your project structure. Make sure you are putting your resources in the right folder (the direct child of main folder). There is no need to add /resources/ to the path as well, since it is already mapped by your mvc:resources mapping.

这篇关于静态内容未加载到 spring mvc 资源标签中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆