静态文件没有加载spring引导web, jsp, context-path



我使用Spring Boot 2.7.5, Java 8和Maven,并在pom.xml中包含以下jar:

  • spring-boot-devtools
  • spring-boot-starter-data-jpa
  • spring-boot-starter-test
  • spring-boot-starter-tomcat
  • spring-boot-starter-web
  • 瓦片核心
  • tiles-jsp
  • tomcat-embed-jasper

我想使用JQuery和数据表。我的静态内容在src/main/resource/static:

src/main/resources
bootstrap-5.2.2-dist/
DataTables/
jquery-ui-1.13.2.custom/
jquery-3.6.1.min.js
myapp.css
myapp.js

我有一个配置类,它有这个

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { ... })
public class ApplicationConfiguration implements WebMvcConfigurer
{
@Override
public void addResourceHandlers(
ResourceHandlerRegistry registry)
{
registry//
.addResourceHandler("/static/**")//
.addResourceLocations("/static/");
}
}

在我的JSP中,我尝试了以下操作,但都不起作用

  • <script type="text/javascript" src="jquery-ui-1.13.2.custom/jquery-ui.min.js"></script>
  • <script type="text/javascript" src="static/jquery-ui-1.13.2.custom/jquery-ui.min.js"></script>
  • <script type="text/javascript" src="/static/jquery-ui-1.13.2.custom/jquery-ui.min.js"></script>

在我的应用程序。属性,我有这个:

spring.mvc.view.prefix=/WEB-INF/content/
spring.mvc.view.suffix=.jsp
server.servlet.context-path=/myapp

我错过了什么或做错了什么?

我找到了一个方法让它工作。不是最好的答案,但它是一个答案(提示时间之轮):

我将静态文件夹直接移动到webapp文件夹下。然后我更改了脚本和链接标签,使src/href以"static/"开头。

我不知道为什么会这样,但我仍然愿意找到正确的方法来做到这一点。

最新更新