Spring MVC不会将CSS,JS文件加载到静态HTML页面 [英] Spring MVC doesn't load css, js files to static html pages

查看:84
本文介绍了Spring MVC不会将CSS,JS文件加载到静态HTML页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了访问某些静态页面( html 而不是 jsp 页面),我禁用了 InternalViewResolver 的角色,我进行了必要的更改,确实,我做了,可以访问我的html页面,但是当我看到浏览器的控制台时,它似乎没有带来我的本地 css js 文件.

I disabled the role of the InternalViewResolver in order to access some static pages, html not jsp pages, i made the required changed, indeed i made and i can access my html page, but when i see the browser's console it looks like it didn't bring my local css and js files.

这是我的项目结构.

这是我的标题页

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8" />
    <title>ECommerce view example</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script>
    <link rel="stylesheet"
        href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
    <script
        src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
    <link rel="stylesheet" type="text/css" href="/static/css/style.css"/>
    <link rel="stylesheet" type="text/css" href="/static/css/login.css"/>
    <script src="http://maps.googleapis.com/maps/api/js?key=AIzaSyBR8P_sULnn-egsyhOb6qLYG2oqFshTiwY"></script>
</head>

mvc-config.xml

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

        <annotation-driven></annotation-driven>
        <context:annotation-config></context:annotation-config>
        <context:component-scan base-package="ma.s2m.net"></context:component-scan>         
        <resources mapping="/static/**" location="/WEB-INF/static/"></resources>
    </beans:beans>

推荐答案

在浏览器控制台中,您的路径似乎无效.

From the browser console, it seems your path is invalid.

<link rel="stylesheet" type="text/css" href="/static/css/style.css"/>

已解析为URL: http://localhost:8080/static/css/style.css.

resolved to URL: http://localhost:8080/static/css/style.css.

此处缺少URL中的上下文根.理想情况下,URL应该是这样的. http://localhost:8080/<CONTEXT_ROOT>/static/css/style.css

Context root in URL is missing here. Ideally URL should have been something like this. http://localhost:8080/<CONTEXT_ROOT>/static/css/style.css

在您的情况下,因为这是静态html页面,所以只需要进行一次更正即可将URI与webapp目录相匹配.

In your case, as this is static html page, only one correction is required to match the URI with webapp directory.

entry.html中进行以下更改,它将起作用.

Make following changes in entry.html and it would work.

<link rel="stylesheet" type="text/css" href="../static/css/style.css"/> <link rel="stylesheet" type="text/css" href="../static/css/login.css"/>

<link rel="stylesheet" type="text/css" href="../static/css/style.css"/> <link rel="stylesheet" type="text/css" href="../static/css/login.css"/>

希望它可以解决您的错误.

Hope it solves your error.

这篇关于Spring MVC不会将CSS,JS文件加载到静态HTML页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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