Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SpringMVC没有拾取html文件中提到的静态资源_Java_Spring_Angular - Fatal编程技术网

Java SpringMVC没有拾取html文件中提到的静态资源

Java SpringMVC没有拾取html文件中提到的静态资源,java,spring,angular,Java,Spring,Angular,我正在开发一个简单的SpringMVC(v4.1.2)和Angular4应用程序 基本上,该应用程序通过从angular客户端发出http请求来执行CRUD操作 以下组合效果非常好: Angular应用程序使用“ng serv”运行 Spring MVC war部署在应用服务器中 现在,我尝试将客户机和服务器合并到一个项目中。有了这个,我应该能够生成一个包含客户端和服务器端代码的war文件 为此, 已将angular应用程序的dist复制到.src/main/webapp/。 重写了org.

我正在开发一个简单的SpringMVC(v4.1.2)和Angular4应用程序

基本上,该应用程序通过从angular客户端发出http请求来执行CRUD操作

以下组合效果非常好: Angular应用程序使用“ng serv”运行 Spring MVC war部署在应用服务器中

现在,我尝试将客户机和服务器合并到一个项目中。有了这个,我应该能够生成一个包含客户端和服务器端代码的war文件

为此,

  • 已将angular应用程序的
    dist
    复制到
    .src/main/webapp/
  • 重写了org.springframework.web.servlet.config.annotation.WebMVCConfigureAdapter的以下方法

    }

  • 更改
    web.xml
    如下:

    <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
          http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">
    
    <display-name>Archetype Created Web Application</display-name>
    
    <servlet>
        <servlet-name>springDispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/applicationContext.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>springDispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    
    <welcome-file-list>
        <welcome-file>/index.html</welcome-file>
    </welcome-file-list>
    

    从控制台日志中可以明显看出,Spring正在触发对
    index.html
    中静态资源的http请求。这是预期的行为吗?为了使Spring认为静态资源实际上是静态的并从相对路径获取它们,应该做哪些更改


    谢谢。

    您的上下文路径是
    /store server
    ,对静态资源的请求是到根目录的。您可以执行以下操作之一来修复它

    • 更新为
    • 在根上下文路径(/)部署SpringWar
    • 更新单个脚本/css的路径以指向 /存储服务器/{resource name}

    在index.html中,尝试向正在使用的脚本文件的每个URL添加正斜杠。像这样

     <script type="text/javascript" src="/inline.bundle.js"></script><script 
    type="text/javascript" src="/polyfills.bundle.js"></script><script
     type="text/javascript" src="/styles.bundle.js"></script><script 
    type="text/javascript" src="/vendor.bundle.js"></script><script 
    type="text/javascript" src="/main.bundle.js"></script></body>
    
    
    
    它没有contextPath,因此您需要更正它

    1.静态定义

    <base href="/store-server">
    
    
    
    2.动态定义

    <base href="/store-server">
    
    如果您使用纯HTML,则可以通过以下方式获取contextPath

    <script type="text/javascript">
        var contextPath = "/"+window.location.pathname.substring(0, window.location.pathname.indexOf("/",2));
        document.head.innerHTML = document.head.innerHTML + "<base href='" + contextPath + "' />";
    </script>
    
    
    var contextPath=“/”+window.location.pathname.substring(0,window.location.pathname.indexOf(“/”,2));
    document.head.innerHTML=document.head.innerHTML+“”;
    
    完美!我不想让客户端代码在服务器上紧密耦合以获得上下文路径细节。我认为第二个解决方案将确保一次性更改,并且客户端可以自由地包含静态资源的相对路径。谢谢对于weblogic用户:在weblogic.xml中设置/将上下文根设置为/。
    <script type="text/javascript">
        var contextPath = "/"+window.location.pathname.substring(0, window.location.pathname.indexOf("/",2));
        document.head.innerHTML = document.head.innerHTML + "<base href='" + contextPath + "' />";
    </script>