如何在 Spring-boot 中添加静态 Web 内容 [英] How add Static Web Content in Spring-boot

查看:25
本文介绍了如何在 Spring-boot 中添加静态 Web 内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用信息在 spring-boot 服务器中添加静态 Web 内容

更新 来帮助您创建新项目>

另一种选择是使用 Spring Tool Suite 或 IntelliJ IDEA.他们可以帮助您创建具有正确配置的新项目(如上一个链接).

I am trying to add static web content in a spring-boot server using the info here

I have tried adding the .js and .css files I need in the several Folders that the previous link says but it doesn't work:

/META-INF/resources/
/resources/

Note: I didn't created the /static/ and /public/ folders because I don't know the absolute Path in my project.

I have added too the addResourceHandlers method:

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!registry.hasMappingForPattern("/webjars/**")) {
    registry.addResourceHandler("/webjars/**").addResourceLocations(
            "classpath:/META-INF/resources/webjars/");
}
if (!registry.hasMappingForPattern("/**")) {
    registry.addResourceHandler("/**").addResourceLocations(
            RESOURCE_LOCATIONS);
}
}

And the reference in the HTML file are like this:

<script src="bootstrap-switch.js"></script> Any idea how I can fix that?

UPDATED:

UPDATED 2: trying the @jfcorugedo

Doesnt work. Take a look, is that what was you saying?

UPDATED 3:trying the @jfcorugedo, second advice:

解决方案

I think you should access static contents using mydomain/<context>/<resource type>/resource

For instance:

http://mydomain/app/js/test.js
http://mydomain/app/img/test.jpg

You can write your resources using application context or simply using relative path:

<script src="js/bootstrap-switch.js"></script>

or

<script src="/<myapp>/js/bootstrap-switch.js"></script>

But if you are using bootstrap with a webjar you should write

<script src="/<myapp>/webjars/bootstrap/<version>/js/bootstrap-switch.js"></script>

There is a project on github to illustrate this https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-web-static

UPDATE 1

I created a test project with the follow structure:

I could access style.css on http://localhost:8080/css/style.css

You can use http://start.spring.io/ to help you create new projects

Another option is use Spring Tool Suite or IntelliJ IDEA. They can help you to create new project with right configurations (as the previous link).

这篇关于如何在 Spring-boot 中添加静态 Web 内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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