gpt4 book ai didi

java - Spring MVC 不选择 html 文件中提到的静态资源

转载 作者:行者123 更新时间:2023-11-29 04:21:16 24 4
gpt4 key购买 nike

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

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

以下组合工作得很好:Angular 应用程序使用“ng serv”运行部署在应用服务器中的 Spring MVC war。

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

为此,

  • 将 Angular 应用程序的 dist 复制到 ..src/main/webapp/ 中。 enter image description here
  • 覆盖了 org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter 的以下方法

    @Configuration
    @EnableWebMvc
    public class ServletContextConfiguration extends WebMvcConfigurerAdapter {

    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    configurer.favorParameter(false).favorPathExtension(true).ignoreAcceptHeader(false)
    .defaultContentType(MediaType.TEXT_HTML).mediaType("xml", MediaType.APPLICATION_XML)
    .mediaType("json", MediaType.APPLICATION_JSON).mediaType("html", MediaType.TEXT_HTML);
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
    configurer.enable();
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/**").addResourceLocations("/dist/");
    }

  • 更改 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>

部署并运行应用程序后,我在 Web 控制台中收到以下错误:

enter image description here

index.html

    <!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>StoreClientApp</title>
<base href="/">

<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
<app-root></app-root>
<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>

</html>

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

谢谢。

最佳答案

http://127.0.0.1:7001/inline.bundle.js它没有 contextPath,因此您需要更正它。

http://127.0.0.1:7001/store-server/inline.bundle.js

1.静态定义

<base href="/store-server">

2.动态定义

如果您使用纯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>

关于java - Spring MVC 不选择 html 文件中提到的静态资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49012561/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com