页面显示使用 Spring MVC 控制器应将端点附加到页面资源。

huangapple go评论124阅读模式
英文:

Page display using Spring mvc controller appends endpoint to page resources

问题

我有一个Spring MVC控制器,返回一个页面,像这样:

@Controller
@RequestMapping("/subscriber")
public class SubscriberWebController {
    @GetMapping("/edit/{id}")
    public String getSubscriber(@PathVariable("id") Long id, Model model) {
        model.addAttribute("id", id);
        return "subscriber/manage";
    }

    @GetMapping("/add")
    public String addSubscriber() {
        return "subscriber/manage";
    }
}

问题是,当manage.html加载时,资源是通过http://localhost:8081/subscriber/vendor/nucleo/css/nucleo.css来加载的,而不是http://localhost:8081/vendor/nucleo/css/nucleo.css。这导致加载所有页面资源时出现404错误。我似乎找不到我做错了什么。

编辑

我在我的ApplicationConfiguration中有这个配置,以帮助提供所有静态CSS和JS文件。

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/**")
            .addResourceLocations("classpath:/static/")
            .setCachePeriod(0);
}

然而,问题仍然存在。

英文:

I have a spring MVC controller that returns a page like this

@Controller
@RequestMapping("/subscriber")
public class SubscriberWebController {
    @GetMapping("/edit/{id}")
    public String getSubscriber(@PathVariable("id") Long id, Model model) {
        model.addAttribute("id", id);
        return "subscriber/manage";
    }

    @GetMapping("/add")
    public String addSubscriber() {
        return "subscriber/manage";
    }
}

The problem is that when manage.html loads, assets are loaded using http://localhost:8081/subscriber/vendor/nucleo/css/nucleo.css rather than http://localhost:8081/vendor/nucleo/css/nucleo.css. This results in a 404 while loading all page resources. I cannot seem to find what I'm doing wrong.

EDIT

I have this configuration in my ApplicationConfiguration so as to help serve all static CSS and JS files.

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/**")
            .addResourceLocations("classpath:/static/")
            .setCachePeriod(0);
}

Yet, the issue still persists

答案1

得分: 1

你没有说明在manage.html内部如何引用你的CSS。

如果你想让你的CSS从/vendor/nucleo/css/加载,那么请像这样设置你的CSS引用:

<link rel="stylesheet" href="vendor/nucleo/css/nucleo.css">

并且在你的厂商(vendor)包中添加映射:

<mvc:resources mapping="/vendor/**" location="/实际位置/" />

希望对你有所帮助。

英文:

You did not say how you reference your CSS inside your manage.html

In case you want your CSS to load from /vendor/nucleo/css/, then put your CSS reference like so:

&lt;link rel=&quot;stylesheet&quot; href=&quot;vendor/nucleo/css/nucleo.css&quot;&gt;

and put a mapping to your vendor package:

&lt;mvc:resources mapping=&quot;/vendor/**&quot; location=&quot;/ACTUAL LOCATION/&quot;/&gt;

Hope this helps

答案2

得分: 1

参考了@eugen的上面的答案,我修改了我的web配置类,如下:

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/static/**")
            .addResourceLocations("classpath:/static/")
            .setCachePeriod(0);
}

在我的HTML页面中,我这样引用静态资源:

<link rel="stylesheet" href="/vendor/nucleo/css/nucleo.css">

注意上面href属性中"vendor"之前的"/"。这解决了我的问题。

英文:

Taking a cue from @eugen's answer above, I modified my web configuration class thus:

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler(&quot;/static/**&quot;)
            .addResourceLocations(&quot;classpath:/static/&quot;)
            .setCachePeriod(0);
}

and in my HTML pages, I reference static resources thus:

&lt;link rel=&quot;stylesheet&quot; href=&quot;/vendor/nucleo/css/nucleo.css&quot;&gt;

Notice the "/" before vendor in the href attribute above. This resolves my issue

huangapple
  • 本文由 发表于 2020年3月16日 07:29:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/60698711.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定