简体   繁体   English

Static 文件未加载 spring 启动 web、JSP、上下文路径

[英]Static files not loading with spring boot web, JSPs, context-path

I'm using Spring Boot 2.7.5, Java 8 and Maven and have the following jars included in pom.xml:我正在使用 Spring Boot 2.7.5、Java 8 和 Maven,并在 pom.xml 中包含以下 jars:

  • spring-boot-devtools spring-boot-devtools
  • spring-boot-starter-data-jpa spring-boot-starter-data-jpa
  • spring-boot-starter-test弹簧启动启动器测试
  • spring-boot-starter-tomcat spring-boot-starter-tomcat
  • spring-boot-starter-web spring-boot-starter-web
  • tiles-core瓷砖核心
  • tiles-jsp tiles-jsp
  • tomcat-embed-jasper tomcat-embed-jasper

I want to use JQuery and DataTables.我想使用 JQuery 和数据表。 My static content is in src/main/resource/static:我的static内容在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

I have a configuration class that has this我有一个配置 class 有这个

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

In my JSP, I have tried the following, none of which work在我的 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>

In my application.properties, I have this:在我的 application.properties 中,我有这个:

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

What am I missing or doing wrong?我错过了什么或做错了什么?

Found a way to make it work.找到了让它发挥作用的方法。 Not the best answer, but it is an answer (cue Wheel of Time quote):不是最好的答案,但它是一个答案(提示时间之轮引用):

I moved the static folder directly under the webapp folder.我把static文件夹直接移到了webapp文件夹下面。 I then changed the script and link tags to have the src/href start with "static/".然后我更改了脚本和链接标签,使 src/href 以“static/”开头。

I have no idea why this works but am still open to finding the right way to do it.我不知道为什么会这样,但仍然愿意寻找正确的方法来做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM